2019-11-21 04:06:16 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-05 03:25:54 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Git::HookEnv do
|
2018-03-30 05:19:46 -04:00
|
|
|
let(:gl_repository) { 'project-123' }
|
|
|
|
|
2017-10-12 07:54:59 -04:00
|
|
|
describe ".set" do
|
2018-09-20 18:40:15 -04:00
|
|
|
context 'with RequestStore disabled' do
|
2017-04-05 03:25:54 -04:00
|
|
|
it 'does not store anything' do
|
2018-03-30 05:19:46 -04:00
|
|
|
described_class.set(gl_repository, GIT_OBJECT_DIRECTORY_RELATIVE: 'foo')
|
2017-04-05 03:25:54 -04:00
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
expect(described_class.all(gl_repository)).to be_empty
|
2017-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-20 18:40:15 -04:00
|
|
|
context 'with RequestStore enabled', :request_store do
|
2017-04-05 03:25:54 -04:00
|
|
|
it 'whitelist some `GIT_*` variables and stores them using RequestStore' do
|
|
|
|
described_class.set(
|
2018-03-30 05:19:46 -04:00
|
|
|
gl_repository,
|
|
|
|
GIT_OBJECT_DIRECTORY_RELATIVE: 'foo',
|
|
|
|
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE: 'bar',
|
2017-04-05 03:25:54 -04:00
|
|
|
GIT_EXEC_PATH: 'baz',
|
|
|
|
PATH: '~/.bin:/bin')
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
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-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-12 07:54:59 -04:00
|
|
|
describe ".all" do
|
2018-09-20 18:40:15 -04:00
|
|
|
context 'with RequestStore enabled', :request_store do
|
2017-04-05 03:25:54 -04:00
|
|
|
before do
|
|
|
|
described_class.set(
|
2018-03-30 05:19:46 -04:00
|
|
|
gl_repository,
|
|
|
|
GIT_OBJECT_DIRECTORY_RELATIVE: 'foo',
|
|
|
|
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE: ['bar'])
|
2017-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an env hash' do
|
2018-03-30 05:19:46 -04:00
|
|
|
expect(described_class.all(gl_repository)).to eq({
|
|
|
|
'GIT_OBJECT_DIRECTORY_RELATIVE' => 'foo',
|
|
|
|
'GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE' => ['bar']
|
2017-04-05 03:25:54 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-12 07:54:59 -04:00
|
|
|
describe ".to_env_hash" do
|
2018-09-20 18:40:15 -04:00
|
|
|
context 'with RequestStore enabled', :request_store do
|
2017-10-12 07:54:59 -04:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
let(:key) { 'GIT_OBJECT_DIRECTORY_RELATIVE' }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
subject { described_class.to_env_hash(gl_repository) }
|
2017-10-12 07:54:59 -04:00
|
|
|
|
|
|
|
where(:input, :output) do
|
|
|
|
nil | nil
|
|
|
|
'foo' | 'foo'
|
|
|
|
[] | ''
|
|
|
|
['foo'] | 'foo'
|
|
|
|
%w[foo bar] | 'foo:bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
2018-03-30 05:19:46 -04:00
|
|
|
described_class.set(gl_repository, key.to_sym => input)
|
2017-10-12 07:54:59 -04:00
|
|
|
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-04-05 03:25:54 -04:00
|
|
|
describe 'thread-safety' do
|
2018-09-20 18:40:15 -04:00
|
|
|
context 'with RequestStore enabled', :request_store do
|
2017-04-05 03:25:54 -04:00
|
|
|
before do
|
|
|
|
allow(RequestStore).to receive(:active?).and_return(true)
|
2018-03-30 05:19:46 -04:00
|
|
|
described_class.set(gl_repository, GIT_OBJECT_DIRECTORY_RELATIVE: 'foo')
|
2017-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is thread-safe' do
|
|
|
|
another_thread = Thread.new do
|
2018-03-30 05:19:46 -04:00
|
|
|
described_class.set(gl_repository, GIT_OBJECT_DIRECTORY_RELATIVE: 'bar')
|
2017-04-05 03:25:54 -04:00
|
|
|
|
|
|
|
Thread.stop
|
2018-03-30 05:19:46 -04:00
|
|
|
described_class.all(gl_repository)[:GIT_OBJECT_DIRECTORY_RELATIVE]
|
2017-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure another_thread runs first
|
|
|
|
sleep 0.1 until another_thread.stop?
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
expect(described_class.all(gl_repository)[:GIT_OBJECT_DIRECTORY_RELATIVE]).to eq('foo')
|
2017-04-05 03:25:54 -04:00
|
|
|
|
|
|
|
another_thread.run
|
|
|
|
expect(another_thread.value).to eq('bar')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|