summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/file_hook_spec.rb
blob: 4fc55f7ad7e6e3b02e4236c322d516eddc78c97d (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::FileHook do
  let(:file_hook) { Rails.root.join('file_hooks', 'test.rb') }
  let(:tmp_file) { Tempfile.new('file_hook-dump') }

  let(:file_hook_source) do
    <<~EOS
      #!/usr/bin/env ruby
      x = $stdin.read
      File.write('#{tmp_file.path}', x)
    EOS
  end

  context 'with file_hooks present' do
    before do
      File.write(file_hook, file_hook_source)
    end

    after do
      FileUtils.rm(file_hook)
    end

    describe '.any?' do
      it 'returns true' do
        expect(described_class.any?).to be true
      end
    end

    describe '.files?' do
      it 'returns a list of file_hooks' do
        expect(described_class.files).to match_array([file_hook.to_s])
      end
    end
  end

  context 'without any file_hooks' do
    describe '.any?' do
      it 'returns false' do
        expect(described_class.any?).to be false
      end
    end

    describe '.files' do
      it 'returns an empty list' do
        expect(described_class.files).to be_empty
      end
    end
  end

  describe '.execute' do
    let(:data) { Gitlab::DataBuilder::Push::SAMPLE_DATA }
    let(:result) { described_class.execute(file_hook.to_s, data) }
    let(:success) { result.first }
    let(:message) { result.last }

    before do
      File.write(file_hook, file_hook_source)
    end

    after do
      FileUtils.rm(file_hook)
    end

    context 'successful execution' do
      before do
        File.chmod(0o777, file_hook)
      end

      after do
        tmp_file.close!
      end

      it { expect(success).to be true }
      it { expect(message).to be_empty }

      it 'ensures file_hook received data via stdin' do
        result

        expect(File.read(tmp_file.path)).to eq(data.to_json)
      end
    end

    context 'non-executable' do
      it { expect(success).to be false }
      it { expect(message).to include('Permission denied') }
    end

    context 'non-zero exit' do
      let(:file_hook_source) do
        <<~EOS
          #!/usr/bin/env ruby
          exit 1
        EOS
      end

      before do
        File.chmod(0o777, file_hook)
      end

      it { expect(success).to be false }
      it { expect(message).to be_empty }
    end
  end
end