summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/hook_spec.rb
blob: a15aa173fbd314baf0435024a1ef9e6cfabcc12e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'spec_helper'
require 'fileutils'

describe Gitlab::Git::Hook, lib: true do
  describe "#trigger" do
    let(:project) { create(:project) }
    let(:user) { create(:user) }

    def create_hook(name)
      FileUtils.mkdir_p(File.join(project.repository.path, 'hooks'))
      File.open(File.join(project.repository.path, 'hooks', name), 'w', 0755) do |f|
        f.write('exit 0')
      end
    end

    def create_failing_hook(name)
      FileUtils.mkdir_p(File.join(project.repository.path, 'hooks'))
      File.open(File.join(project.repository.path, 'hooks', name), 'w', 0755) do |f|
        f.write(<<-HOOK)
          echo 'regular message from the hook'
          echo 'error message from the hook' 1>&2
          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
          it "returns success with no errors" do
            create_hook(hook_name)
            hook = Gitlab::Git::Hook.new(hook_name, project.repository.path)
            blank = Gitlab::Git::BLANK_SHA
            ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'

            status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref)
            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)
            hook = Gitlab::Git::Hook.new(hook_name, project.repository.path)
            blank = Gitlab::Git::BLANK_SHA
            ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'

            status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref)
            expect(status).to be false
            expect(errors).to eq("error message from the hook\n")
          end
        end
      end
    end

    context "when the hook doesn't exist" do
      it "returns success with no errors" do
        hook = Gitlab::Git::Hook.new('unknown_hook', project.repository.path)
        blank = Gitlab::Git::BLANK_SHA
        ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'

        status, errors = hook.trigger(Gitlab::GlId.gl_id(user), blank, blank, ref)
        expect(status).to be true
        expect(errors).to be_nil
      end
    end
  end
end