2017-09-22 06:17:01 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Ci::Variables::Collection do
|
|
|
|
describe '.new' do
|
|
|
|
it 'can be initialized with an array' do
|
2019-02-26 19:22:51 -05:00
|
|
|
variable = { key: 'VAR', value: 'value', public: true, masked: false }
|
2018-03-12 08:58:54 -04:00
|
|
|
|
2017-09-22 06:17:01 -04:00
|
|
|
collection = described_class.new([variable])
|
|
|
|
|
2018-03-23 08:44:12 -04:00
|
|
|
expect(collection.first.to_runner_variable).to eq variable
|
2017-09-22 06:17:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be initialized without an argument' do
|
|
|
|
expect(subject).to be_none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#append' do
|
|
|
|
it 'appends a hash' do
|
|
|
|
subject.append(key: 'VARIABLE', value: 'something')
|
|
|
|
|
|
|
|
expect(subject).to be_one
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends a Ci::Variable' do
|
|
|
|
subject.append(build(:ci_variable))
|
|
|
|
|
|
|
|
expect(subject).to be_one
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends an internal resource' do
|
2018-06-21 07:17:35 -04:00
|
|
|
collection = described_class.new([{ key: 'TEST', value: '1' }])
|
2017-09-22 06:17:01 -04:00
|
|
|
|
|
|
|
subject.append(collection.first)
|
|
|
|
|
|
|
|
expect(subject).to be_one
|
|
|
|
end
|
2018-03-14 06:04:42 -04:00
|
|
|
|
|
|
|
it 'returns self' do
|
|
|
|
expect(subject.append(key: 'VAR', value: 'test'))
|
|
|
|
.to eq subject
|
|
|
|
end
|
2017-09-22 06:17:01 -04:00
|
|
|
end
|
|
|
|
|
2018-03-13 09:29:54 -04:00
|
|
|
describe '#concat' do
|
|
|
|
it 'appends all elements from an array' do
|
|
|
|
collection = described_class.new([{ key: 'VAR_1', value: '1' }])
|
|
|
|
variables = [{ key: 'VAR_2', value: '2' }, { key: 'VAR_3', value: '3' }]
|
|
|
|
|
|
|
|
collection.concat(variables)
|
|
|
|
|
|
|
|
expect(collection).to include(key: 'VAR_1', value: '1', public: true)
|
|
|
|
expect(collection).to include(key: 'VAR_2', value: '2', public: true)
|
|
|
|
expect(collection).to include(key: 'VAR_3', value: '3', public: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends all elements from other collection' do
|
|
|
|
collection = described_class.new([{ key: 'VAR_1', value: '1' }])
|
|
|
|
additional = described_class.new([{ key: 'VAR_2', value: '2' },
|
|
|
|
{ key: 'VAR_3', value: '3' }])
|
|
|
|
|
|
|
|
collection.concat(additional)
|
|
|
|
|
|
|
|
expect(collection).to include(key: 'VAR_1', value: '1', public: true)
|
|
|
|
expect(collection).to include(key: 'VAR_2', value: '2', public: true)
|
|
|
|
expect(collection).to include(key: 'VAR_3', value: '3', public: true)
|
|
|
|
end
|
2018-03-14 06:04:42 -04:00
|
|
|
|
2019-03-02 09:18:14 -05:00
|
|
|
it 'does not concatenate resource if it undefined' do
|
|
|
|
collection = described_class.new([{ key: 'VAR_1', value: '1' }])
|
|
|
|
|
|
|
|
collection.concat(nil)
|
|
|
|
|
|
|
|
expect(collection).to be_one
|
|
|
|
end
|
|
|
|
|
2018-03-14 06:04:42 -04:00
|
|
|
it 'returns self' do
|
|
|
|
expect(subject.concat([key: 'VAR', value: 'test']))
|
|
|
|
.to eq subject
|
|
|
|
end
|
2018-03-13 09:29:54 -04:00
|
|
|
end
|
|
|
|
|
2017-09-22 06:17:01 -04:00
|
|
|
describe '#+' do
|
|
|
|
it 'makes it possible to combine with an array' do
|
2018-06-21 07:17:35 -04:00
|
|
|
collection = described_class.new([{ key: 'TEST', value: '1' }])
|
2017-09-22 07:46:20 -04:00
|
|
|
variables = [{ key: 'TEST', value: 'something' }]
|
2017-09-22 06:17:01 -04:00
|
|
|
|
|
|
|
expect((collection + variables).count).to eq 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes it possible to combine with another collection' do
|
2018-06-21 07:17:35 -04:00
|
|
|
collection = described_class.new([{ key: 'TEST', value: '1' }])
|
|
|
|
other = described_class.new([{ key: 'TEST', value: '2' }])
|
2017-09-22 06:17:01 -04:00
|
|
|
|
|
|
|
expect((collection + other).count).to eq 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-07 04:09:53 -05:00
|
|
|
describe '#to_runner_variables' do
|
|
|
|
it 'creates an array of hashes in a runner-compatible format' do
|
2018-06-21 07:17:35 -04:00
|
|
|
collection = described_class.new([{ key: 'TEST', value: '1' }])
|
2017-09-22 06:17:01 -04:00
|
|
|
|
2018-03-07 04:09:53 -05:00
|
|
|
expect(collection.to_runner_variables)
|
2019-02-26 19:22:51 -05:00
|
|
|
.to eq [{ key: 'TEST', value: '1', public: true, masked: false }]
|
2017-09-22 06:17:01 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-23 08:44:12 -04:00
|
|
|
|
|
|
|
describe '#to_hash' do
|
|
|
|
it 'returns regular hash in valid order without duplicates' do
|
|
|
|
collection = described_class.new
|
|
|
|
.append(key: 'TEST1', value: 'test-1')
|
|
|
|
.append(key: 'TEST2', value: 'test-2')
|
|
|
|
.append(key: 'TEST1', value: 'test-3')
|
|
|
|
|
|
|
|
expect(collection.to_hash).to eq('TEST1' => 'test-3',
|
|
|
|
'TEST2' => 'test-2')
|
|
|
|
|
|
|
|
expect(collection.to_hash).to include(TEST1: 'test-3')
|
|
|
|
expect(collection.to_hash).not_to include(TEST1: 'test-1')
|
|
|
|
end
|
|
|
|
end
|
2017-09-22 06:17:01 -04:00
|
|
|
end
|