debian-mirror-gitlab/spec/lib/gitlab/git/hook_spec.rb

98 lines
3.4 KiB
Ruby
Raw Normal View History

2016-08-24 12:49:21 +05:30
require 'spec_helper'
require 'fileutils'
2017-09-10 17:25:29 +05:30
describe Gitlab::Git::Hook do
before do
# We need this because in the spec/spec_helper.rb we define it like this:
# allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([true, nil])
allow_any_instance_of(described_class).to receive(:trigger).and_call_original
end
2016-08-24 12:49:21 +05:30
describe "#trigger" do
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2018-03-17 18:26:18 +05:30
let(:repository) { project.repository.raw_repository }
let(:repo_path) { repository.path }
2016-08-24 12:49:21 +05:30
let(:user) { create(:user) }
2017-09-10 17:25:29 +05:30
let(:gl_id) { Gitlab::GlId.gl_id(user) }
2018-03-17 18:26:18 +05:30
let(:gl_username) { user.username }
2016-08-24 12:49:21 +05:30
def create_hook(name)
2017-09-10 17:25:29 +05:30
FileUtils.mkdir_p(File.join(repo_path, 'hooks'))
File.open(File.join(repo_path, 'hooks', name), 'w', 0755) do |f|
2016-08-24 12:49:21 +05:30
f.write('exit 0')
end
end
def create_failing_hook(name)
2017-09-10 17:25:29 +05:30
FileUtils.mkdir_p(File.join(repo_path, 'hooks'))
File.open(File.join(repo_path, 'hooks', name), 'w', 0755) do |f|
2016-08-24 12:49:21 +05:30
f.write(<<-HOOK)
echo 'regular message from the hook'
echo 'error message from the hook' 1>&2
2018-03-17 18:26:18 +05:30
echo 'error message from the hook line 2' 1>&2
2016-08-24 12:49:21 +05:30
exit 1
HOOK
end
end
['pre-receive', 'post-receive', 'update'].each do |hook_name|
context "when triggering a #{hook_name} hook" do
context "when the hook is successful" do
2017-09-10 17:25:29 +05:30
let(:hook_path) { File.join(repo_path, 'hooks', hook_name) }
let(:gl_repository) { Gitlab::GlRepository.gl_repository(project, false) }
let(:env) do
{
'GL_ID' => gl_id,
2018-03-17 18:26:18 +05:30
'GL_USERNAME' => gl_username,
2017-09-10 17:25:29 +05:30
'PWD' => repo_path,
'GL_PROTOCOL' => 'web',
'GL_REPOSITORY' => gl_repository
}
end
2016-08-24 12:49:21 +05:30
it "returns success with no errors" do
create_hook(hook_name)
2018-03-17 18:26:18 +05:30
hook = described_class.new(hook_name, repository)
2016-08-24 12:49:21 +05:30
blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
2017-09-10 17:25:29 +05:30
if hook_name != 'update'
expect(Open3).to receive(:popen3)
.with(env, hook_path, chdir: repo_path).and_call_original
end
2018-03-17 18:26:18 +05:30
status, errors = hook.trigger(gl_id, gl_username, blank, blank, ref)
2016-08-24 12:49:21 +05:30
expect(status).to be true
expect(errors).to be_blank
end
end
context "when the hook is unsuccessful" do
it "returns failure with errors" do
create_failing_hook(hook_name)
2018-03-17 18:26:18 +05:30
hook = described_class.new(hook_name, repository)
2016-08-24 12:49:21 +05:30
blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
2018-03-17 18:26:18 +05:30
status, errors = hook.trigger(gl_id, gl_username, blank, blank, ref)
2016-08-24 12:49:21 +05:30
expect(status).to be false
2018-03-17 18:26:18 +05:30
expect(errors).to eq("error message from the hook<br>error message from the hook line 2<br>")
2016-08-24 12:49:21 +05:30
end
end
end
end
context "when the hook doesn't exist" do
it "returns success with no errors" do
2018-03-17 18:26:18 +05:30
hook = described_class.new('unknown_hook', repository)
2016-08-24 12:49:21 +05:30
blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
2018-03-17 18:26:18 +05:30
status, errors = hook.trigger(gl_id, gl_username, blank, blank, ref)
2016-08-24 12:49:21 +05:30
expect(status).to be true
expect(errors).to be_nil
end
end
end
end