2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-26 15:33:19 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
describe Gitlab::FileHook do
|
|
|
|
let(:file_hook) { Rails.root.join('plugins', 'test.rb') }
|
|
|
|
let(:tmp_file) { Tempfile.new('file_hook-dump') }
|
2020-01-06 09:07:42 +00:00
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
let(:file_hook_source) do
|
2020-01-06 09:07:42 +00:00
|
|
|
<<~EOS
|
|
|
|
#!/usr/bin/env ruby
|
|
|
|
x = STDIN.read
|
|
|
|
File.write('#{tmp_file.path}', x)
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
context 'with file_hooks present' do
|
2020-01-06 09:07:42 +00:00
|
|
|
before do
|
2020-01-16 15:08:41 +00:00
|
|
|
File.write(file_hook, file_hook_source)
|
2020-01-06 09:07:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2020-01-16 15:08:41 +00:00
|
|
|
FileUtils.rm(file_hook)
|
2020-01-06 09:07:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.any?' do
|
|
|
|
it 'returns true' do
|
|
|
|
expect(described_class.any?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.files?' do
|
2020-01-16 15:08:41 +00:00
|
|
|
it 'returns a list of file_hooks' do
|
|
|
|
expect(described_class.files).to match_array([file_hook.to_s])
|
2020-01-06 09:07:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
context 'without any file_hooks' do
|
2020-01-06 09:07:42 +00:00
|
|
|
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
|
|
|
|
|
2018-02-26 15:33:19 +00:00
|
|
|
describe '.execute' do
|
|
|
|
let(:data) { Gitlab::DataBuilder::Push::SAMPLE_DATA }
|
2020-01-16 15:08:41 +00:00
|
|
|
let(:result) { described_class.execute(file_hook.to_s, data) }
|
2018-02-28 10:16:23 +00:00
|
|
|
let(:success) { result.first }
|
|
|
|
let(:message) { result.last }
|
2018-02-26 15:33:19 +00:00
|
|
|
|
|
|
|
before do
|
2020-01-16 15:08:41 +00:00
|
|
|
File.write(file_hook, file_hook_source)
|
2018-02-26 15:33:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2020-01-16 15:08:41 +00:00
|
|
|
FileUtils.rm(file_hook)
|
2018-02-26 15:33:19 +00:00
|
|
|
end
|
|
|
|
|
2018-02-27 16:08:38 +00:00
|
|
|
context 'successful execution' do
|
|
|
|
before do
|
2020-01-16 15:08:41 +00:00
|
|
|
File.chmod(0o777, file_hook)
|
2018-02-27 16:08:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
tmp_file.close!
|
|
|
|
end
|
2018-02-26 15:33:19 +00:00
|
|
|
|
2018-02-28 10:16:23 +00:00
|
|
|
it { expect(success).to be true }
|
|
|
|
it { expect(message).to be_empty }
|
2018-02-26 15:33:19 +00:00
|
|
|
|
2020-01-16 15:08:41 +00:00
|
|
|
it 'ensures file_hook received data via stdin' do
|
2018-02-28 10:16:23 +00:00
|
|
|
result
|
2018-02-27 16:08:38 +00:00
|
|
|
|
|
|
|
expect(File.read(tmp_file.path)).to eq(data.to_json)
|
|
|
|
end
|
2018-02-26 15:33:19 +00:00
|
|
|
end
|
|
|
|
|
2018-02-27 16:08:38 +00:00
|
|
|
context 'non-executable' do
|
2018-02-28 10:16:23 +00:00
|
|
|
it { expect(success).to be false }
|
|
|
|
it { expect(message).to include('Permission denied') }
|
2018-02-27 16:08:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'non-zero exit' do
|
2020-01-16 15:08:41 +00:00
|
|
|
let(:file_hook_source) do
|
2018-02-27 16:08:38 +00:00
|
|
|
<<~EOS
|
|
|
|
#!/usr/bin/env ruby
|
|
|
|
exit 1
|
|
|
|
EOS
|
|
|
|
end
|
2018-02-26 15:33:19 +00:00
|
|
|
|
2018-02-27 16:08:38 +00:00
|
|
|
before do
|
2020-01-16 15:08:41 +00:00
|
|
|
File.chmod(0o777, file_hook)
|
2018-02-27 16:08:38 +00:00
|
|
|
end
|
|
|
|
|
2018-02-28 10:16:23 +00:00
|
|
|
it { expect(success).to be false }
|
|
|
|
it { expect(message).to be_empty }
|
2018-02-27 16:08:38 +00:00
|
|
|
end
|
2018-02-26 15:33:19 +00:00
|
|
|
end
|
|
|
|
end
|