debian-mirror-gitlab/spec/lib/gitlab/file_hook_spec.rb

108 lines
2.2 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
require 'spec_helper'
2020-03-09 13:42:32 +05:30
describe Gitlab::FileHook do
let(:file_hook) { Rails.root.join('plugins', 'test.rb') }
let(:tmp_file) { Tempfile.new('file_hook-dump') }
2020-01-12 00:16:45 +05:30
2020-03-09 13:42:32 +05:30
let(:file_hook_source) do
2020-01-12 00:16:45 +05:30
<<~EOS
#!/usr/bin/env ruby
x = STDIN.read
File.write('#{tmp_file.path}', x)
EOS
end
2020-03-09 13:42:32 +05:30
context 'with file_hooks present' do
2020-01-12 00:16:45 +05:30
before do
2020-03-09 13:42:32 +05:30
File.write(file_hook, file_hook_source)
2020-01-12 00:16:45 +05:30
end
after do
2020-03-09 13:42:32 +05:30
FileUtils.rm(file_hook)
2020-01-12 00:16:45 +05:30
end
describe '.any?' do
it 'returns true' do
expect(described_class.any?).to be true
end
end
describe '.files?' do
2020-03-09 13:42:32 +05:30
it 'returns a list of file_hooks' do
expect(described_class.files).to match_array([file_hook.to_s])
2020-01-12 00:16:45 +05:30
end
end
end
2020-03-09 13:42:32 +05:30
context 'without any file_hooks' do
2020-01-12 00:16:45 +05:30
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-03-27 19:54:05 +05:30
describe '.execute' do
let(:data) { Gitlab::DataBuilder::Push::SAMPLE_DATA }
2020-03-09 13:42:32 +05:30
let(:result) { described_class.execute(file_hook.to_s, data) }
2018-03-27 19:54:05 +05:30
let(:success) { result.first }
let(:message) { result.last }
before do
2020-03-09 13:42:32 +05:30
File.write(file_hook, file_hook_source)
2018-03-27 19:54:05 +05:30
end
after do
2020-03-09 13:42:32 +05:30
FileUtils.rm(file_hook)
2018-03-27 19:54:05 +05:30
end
context 'successful execution' do
before do
2020-03-09 13:42:32 +05:30
File.chmod(0o777, file_hook)
2018-03-27 19:54:05 +05:30
end
after do
tmp_file.close!
end
it { expect(success).to be true }
it { expect(message).to be_empty }
2020-03-09 13:42:32 +05:30
it 'ensures file_hook received data via stdin' do
2018-03-27 19:54:05 +05:30
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
2020-03-09 13:42:32 +05:30
let(:file_hook_source) do
2018-03-27 19:54:05 +05:30
<<~EOS
#!/usr/bin/env ruby
exit 1
EOS
end
before do
2020-03-09 13:42:32 +05:30
File.chmod(0o777, file_hook)
2018-03-27 19:54:05 +05:30
end
it { expect(success).to be false }
it { expect(message).to be_empty }
end
end
end