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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3.2 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Git::HookEnv do
2018-05-09 12:01:36 +05:30
let(:gl_repository) { 'project-123' }
2018-03-17 18:26:18 +05:30
describe ".set" do
2018-12-05 23:21:45 +05:30
context 'with RequestStore disabled' do
2017-08-17 22:00:37 +05:30
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
2018-12-05 23:21:45 +05:30
context 'with RequestStore enabled', :request_store do
2017-08-17 22:00:37 +05:30
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
2018-12-05 23:21:45 +05:30
context 'with RequestStore enabled', :request_store do
2017-08-17 22:00:37 +05:30
before 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
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
2018-12-05 23:21:45 +05:30
context 'with RequestStore enabled', :request_store do
2018-03-17 18:26:18 +05:30
using RSpec::Parameterized::TableSyntax
2018-05-09 12:01:36 +05:30
let(:key) { 'GIT_OBJECT_DIRECTORY_RELATIVE' }
2020-01-01 13:55:28 +05:30
2018-05-09 12:01:36 +05:30
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
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
2018-12-05 23:21:45 +05:30
context 'with RequestStore enabled', :request_store do
2017-08-17 22:00:37 +05:30
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