debian-mirror-gitlab/spec/lib/gitlab/git/hook_env_spec.rb

120 lines
3.4 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
require 'spec_helper'
2018-05-09 12:01:36 +05:30
describe Gitlab::Git::HookEnv do
let(:gl_repository) { 'project-123' }
2018-03-17 18:26:18 +05:30
describe ".set" do
2017-08-17 22:00:37 +05:30
context 'with RequestStore.store disabled' do
before do
allow(RequestStore).to receive(:active?).and_return(false)
end
it 'does not store anything' do
2018-05-09 12:01:36 +05:30
described_class.set(gl_repository, GIT_OBJECT_DIRECTORY_RELATIVE: 'foo')
2017-08-17 22:00:37 +05:30
2018-05-09 12:01:36 +05:30
expect(described_class.all(gl_repository)).to be_empty
2017-08-17 22:00:37 +05:30
end
end
context 'with RequestStore.store enabled' do
before do
allow(RequestStore).to receive(:active?).and_return(true)
end
it 'whitelist some `GIT_*` variables and stores them using RequestStore' do
described_class.set(
2018-05-09 12:01:36 +05:30
gl_repository,
GIT_OBJECT_DIRECTORY_RELATIVE: 'foo',
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE: 'bar',
2017-08-17 22:00:37 +05:30
GIT_EXEC_PATH: 'baz',
PATH: '~/.bin:/bin')
2018-05-09 12:01:36 +05:30
git_env = described_class.all(gl_repository)
expect(git_env[:GIT_OBJECT_DIRECTORY_RELATIVE]).to eq('foo')
expect(git_env[:GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE]).to eq('bar')
expect(git_env[:GIT_EXEC_PATH]).to be_nil
expect(git_env[:PATH]).to be_nil
expect(git_env[:bar]).to be_nil
2017-08-17 22:00:37 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
describe ".all" do
2017-08-17 22:00:37 +05:30
context 'with RequestStore.store enabled' do
before do
allow(RequestStore).to receive(:active?).and_return(true)
described_class.set(
2018-05-09 12:01:36 +05:30
gl_repository,
GIT_OBJECT_DIRECTORY_RELATIVE: 'foo',
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE: ['bar'])
2017-08-17 22:00:37 +05:30
end
it 'returns an env hash' do
2018-05-09 12:01:36 +05:30
expect(described_class.all(gl_repository)).to eq({
'GIT_OBJECT_DIRECTORY_RELATIVE' => 'foo',
'GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE' => ['bar']
2017-08-17 22:00:37 +05:30
})
end
end
end
2018-03-17 18:26:18 +05:30
describe ".to_env_hash" do
context 'with RequestStore.store enabled' do
using RSpec::Parameterized::TableSyntax
2018-05-09 12:01:36 +05:30
let(:key) { 'GIT_OBJECT_DIRECTORY_RELATIVE' }
subject { described_class.to_env_hash(gl_repository) }
2018-03-17 18:26:18 +05:30
where(:input, :output) do
nil | nil
'foo' | 'foo'
[] | ''
['foo'] | 'foo'
%w[foo bar] | 'foo:bar'
end
with_them do
before do
allow(RequestStore).to receive(:active?).and_return(true)
2018-05-09 12:01:36 +05:30
described_class.set(gl_repository, key.to_sym => input)
2018-03-17 18:26:18 +05:30
end
it 'puts the right value in the hash' do
if output
expect(subject.fetch(key)).to eq(output)
else
expect(subject.has_key?(key)).to eq(false)
end
end
end
end
end
2017-08-17 22:00:37 +05:30
describe 'thread-safety' do
context 'with RequestStore.store enabled' do
before do
allow(RequestStore).to receive(:active?).and_return(true)
2018-05-09 12:01:36 +05:30
described_class.set(gl_repository, GIT_OBJECT_DIRECTORY_RELATIVE: 'foo')
2017-08-17 22:00:37 +05:30
end
it 'is thread-safe' do
another_thread = Thread.new do
2018-05-09 12:01:36 +05:30
described_class.set(gl_repository, GIT_OBJECT_DIRECTORY_RELATIVE: 'bar')
2017-08-17 22:00:37 +05:30
Thread.stop
2018-05-09 12:01:36 +05:30
described_class.all(gl_repository)[:GIT_OBJECT_DIRECTORY_RELATIVE]
2017-08-17 22:00:37 +05:30
end
# Ensure another_thread runs first
sleep 0.1 until another_thread.stop?
2018-05-09 12:01:36 +05:30
expect(described_class.all(gl_repository)[:GIT_OBJECT_DIRECTORY_RELATIVE]).to eq('foo')
2017-08-17 22:00:37 +05:30
another_thread.run
expect(another_thread.value).to eq('bar')
end
end
end
end