summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/file_hook_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-16 15:08:41 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-16 15:08:41 +0000
commitd47f9d2304dbc3a23bba7fe7a5cd07218eeb41cd (patch)
tree4b4efa1ccd8246fba2dc9f8816d9d2c0268e9818 /spec/lib/gitlab/file_hook_spec.rb
parentc158fa8d69c704663d289341a014c44c062cda88 (diff)
downloadgitlab-ce-d47f9d2304dbc3a23bba7fe7a5cd07218eeb41cd.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/file_hook_spec.rb')
-rw-r--r--spec/lib/gitlab/file_hook_spec.rb107
1 files changed, 107 insertions, 0 deletions
diff --git a/spec/lib/gitlab/file_hook_spec.rb b/spec/lib/gitlab/file_hook_spec.rb
new file mode 100644
index 00000000000..d184eb483d4
--- /dev/null
+++ b/spec/lib/gitlab/file_hook_spec.rb
@@ -0,0 +1,107 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::FileHook do
+ let(:file_hook) { Rails.root.join('plugins', '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