2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Ci::Config::Entry::Variables do
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:config) { {} }
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:metadata) { {} }
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
subject(:entry) { described_class.new(config, **metadata) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
entry.compose!
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
shared_examples 'valid config' do
|
|
|
|
describe '#value' do
|
|
|
|
it 'returns hash with key value strings' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(entry.value).to eq result
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
describe '#errors' do
|
|
|
|
it 'does not append errors' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(entry.errors).to be_empty
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
describe '#valid?' do
|
|
|
|
it 'is valid' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(entry).to be_valid
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
shared_examples 'invalid config' do |error_message|
|
2021-01-03 14:25:43 +05:30
|
|
|
describe '#valid?' do
|
|
|
|
it 'is not valid' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(entry).not_to be_valid
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
describe '#errors' do
|
|
|
|
it 'saves errors' do
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(entry.errors)
|
|
|
|
.to include(error_message)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when entry config value has key-value pairs' do
|
|
|
|
let(:config) do
|
|
|
|
{ 'VARIABLE_1' => 'value 1', 'VARIABLE_2' => 'value 2' }
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
let(:result) do
|
|
|
|
{ 'VARIABLE_1' => 'value 1', 'VARIABLE_2' => 'value 2' }
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it_behaves_like 'valid config'
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
describe '#value_with_data' do
|
|
|
|
it 'returns variable with data' do
|
|
|
|
expect(entry.value_with_data).to eq(
|
|
|
|
'VARIABLE_1' => { value: 'value 1' },
|
|
|
|
'VARIABLE_2' => { value: 'value 2' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with numeric keys and values in the config' do
|
|
|
|
let(:config) { { 10 => 20 } }
|
|
|
|
let(:result) do
|
|
|
|
{ '10' => '20' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'valid config'
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
context 'when key is an array' do
|
|
|
|
let(:config) { { ['VAR1'] => 'val1' } }
|
|
|
|
|
|
|
|
it_behaves_like 'invalid config', /must be an alphanumeric string/
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is a symbol' do
|
|
|
|
let(:config) { { 'VAR1' => :val1 } }
|
|
|
|
let(:result) do
|
|
|
|
{ 'VAR1' => 'val1' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'valid config'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is a boolean' do
|
|
|
|
let(:config) { { 'VAR1' => true } }
|
|
|
|
|
|
|
|
it_behaves_like 'invalid config', /must be either a string or a hash/
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
context 'when entry config value has unallowed value key-value pair and value is a string' do
|
|
|
|
let(:config) do
|
|
|
|
{ 'VARIABLE_1' => { value: 'value', description: 'variable 1' } }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no allowed_value_data metadata' do
|
|
|
|
it_behaves_like 'invalid config', /variable_1 config must be a string/
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metadata has allow_array_value and allowed_value_data' do
|
|
|
|
let(:metadata) { { allowed_value_data: %i[value description], allow_array_value: true } }
|
|
|
|
|
|
|
|
let(:result) do
|
|
|
|
{ 'VARIABLE_1' => 'value' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'valid config'
|
|
|
|
|
|
|
|
describe '#value_with_data' do
|
|
|
|
it 'returns variable with data' do
|
|
|
|
expect(entry.value_with_data).to eq(
|
|
|
|
'VARIABLE_1' => { value: 'value', description: 'variable 1' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when entry config value has key-value pair and value is an array' do
|
|
|
|
let(:config) do
|
|
|
|
{ 'VARIABLE_1' => { value: %w[value1 value2], description: 'variable 1' } }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no allowed_value_data metadata' do
|
|
|
|
it_behaves_like 'invalid config', /variable_1 config value must be an alphanumeric string/
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metadata has allow_array_value and allowed_value_data' do
|
|
|
|
let(:metadata) { { allowed_value_data: %i[value description], allow_array_value: true } }
|
|
|
|
|
|
|
|
let(:result) do
|
|
|
|
{ 'VARIABLE_1' => 'value1' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'valid config'
|
|
|
|
|
|
|
|
describe '#value_with_data' do
|
|
|
|
it 'returns variable with data' do
|
|
|
|
expect(entry.value_with_data).to eq(
|
|
|
|
'VARIABLE_1' => { value: 'value1', value_options: %w[value1 value2], description: 'variable 1' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when entry config value has key-value pair and hash' do
|
|
|
|
let(:config) do
|
|
|
|
{ 'VARIABLE_1' => { value: 'value 1', description: 'variable 1' },
|
|
|
|
'VARIABLE_2' => 'value 2' }
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it_behaves_like 'invalid config', /variable_1 config must be a string/
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
context 'when metadata has allowed_value_data' do
|
|
|
|
let(:metadata) { { allowed_value_data: %i[value description] } }
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
let(:result) do
|
|
|
|
{ 'VARIABLE_1' => 'value 1', 'VARIABLE_2' => 'value 2' }
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
it_behaves_like 'valid config'
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
describe '#value_with_data' do
|
|
|
|
it 'returns variable with data' do
|
|
|
|
expect(entry.value_with_data).to eq(
|
|
|
|
'VARIABLE_1' => { value: 'value 1', description: 'variable 1' },
|
|
|
|
'VARIABLE_2' => { value: 'value 2' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when entry value is an array' do
|
|
|
|
let(:config) { [:VAR, 'test'] }
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it_behaves_like 'invalid config', /variables config should be a hash/
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
context 'when metadata has allowed_value_data' do
|
|
|
|
let(:metadata) { { allowed_value_data: %i[value description] } }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
context 'when entry value has hash with other key-pairs' do
|
|
|
|
let(:config) do
|
|
|
|
{ 'VARIABLE_1' => { value: 'value 1', hello: 'variable 1' },
|
|
|
|
'VARIABLE_2' => 'value 2' }
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it_behaves_like 'invalid config', /variable_1 config uses invalid data keys: hello/
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
context 'when entry config value has hash with nil description' do
|
|
|
|
let(:config) do
|
|
|
|
{ 'VARIABLE_1' => { value: 'value 1', description: nil } }
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it_behaves_like 'invalid config', /variable_1 config description must be an alphanumeric string/
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
context 'when entry config value has hash without description' do
|
|
|
|
let(:config) do
|
|
|
|
{ 'VARIABLE_1' => { value: 'value 1' } }
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:result) do
|
|
|
|
{ 'VARIABLE_1' => 'value 1' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'valid config'
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|