Commit 22c2814b authored by Kamil Trzcinski's avatar Kamil Trzcinski

Refactor gitlab_ci_yaml_processor variables tests

parent aafd4349
...@@ -477,96 +477,120 @@ module Ci ...@@ -477,96 +477,120 @@ module Ci
end end
describe 'Variables' do describe 'Variables' do
context 'when global variables are defined' do let(:config_processor) { GitlabCiYamlProcessor.new(YAML.dump(config), path) }
it 'returns global variables' do
variables = {
VAR1: 'value1',
VAR2: 'value2',
}
config = YAML.dump({ subject { config_processor.builds.first[:yaml_variables] }
context 'when global variables are defined' do
let(:variables) do
{ VAR1: 'value1', VAR2: 'value2' }
end
let(:config) do
{
variables: variables, variables: variables,
before_script: ['pwd'], before_script: ['pwd'],
rspec: { script: 'rspec' } rspec: { script: 'rspec' }
}) }
end
config_processor = GitlabCiYamlProcessor.new(config, path) it 'returns global variables' do
expect(subject).to contain_exactly(
{ key: :VAR1, value: 'value1', public: true },
{ key: :VAR2, value: 'value2', public: true }
)
end
end
context 'when job and global variables are defined' do
let(:global_variables) do
{ VAR1: 'global1', VAR3: 'global3' }
end
let(:job_variables) do
{ VAR1: 'value1', VAR2: 'value2' }
end
let(:config) do
{
before_script: ['pwd'],
variables: global_variables,
rspec: { script: 'rspec', variables: job_variables }
}
end
expect(config_processor.global_variables).to eq(variables) it 'returns all unique variables' do
expect(subject).to contain_exactly(
{ key: :VAR3, value: 'global3', public: true },
{ key: :VAR1, value: 'value1', public: true },
{ key: :VAR2, value: 'value2', public: true }
)
end end
end end
context 'when job variables are defined' do context 'when job variables are defined' do
context 'when syntax is correct' do let(:config) do
it 'returns job variables' do {
variables = { before_script: ['pwd'],
KEY1: 'value1', rspec: { script: 'rspec', variables: variables }
SOME_KEY_2: 'value2' }
} end
context 'when also global variables are defined' do
config = YAML.dump( end
{ before_script: ['pwd'],
rspec: {
variables: variables,
script: 'rspec' }
})
config_processor = GitlabCiYamlProcessor.new(config, path) context 'when syntax is correct' do
let(:variables) do
{ VAR1: 'value1', VAR2: 'value2' }
end
expect(config_processor.job_variables(:rspec)).to eq variables it 'returns job variables' do
expect(subject).to contain_exactly(
{ key: :VAR1, value: 'value1', public: true },
{ key: :VAR2, value: 'value2', public: true }
)
end end
end end
context 'when syntax is incorrect' do context 'when syntax is incorrect' do
context 'when variables defined but invalid' do context 'when variables defined but invalid' do
it 'raises error' do let(:variables) do
variables = [:KEY1, 'value1', :KEY2, 'value2'] [ :VAR1, 'value1', :VAR2, 'value2' ]
end
config = YAML.dump(
{ before_script: ['pwd'],
rspec: {
variables: variables,
script: 'rspec' }
})
expect { GitlabCiYamlProcessor.new(config, path) } it 'raises error' do
expect { subject }
.to raise_error(GitlabCiYamlProcessor::ValidationError, .to raise_error(GitlabCiYamlProcessor::ValidationError,
/job: variables should be a map/) /job: variables should be a map/)
end end
end end
context 'when variables key defined but value not specified' do context 'when variables key defined but value not specified' do
it 'returns empty array' do let(:variables) do
config = YAML.dump( nil
{ before_script: ['pwd'], end
rspec: {
variables: nil,
script: 'rspec' }
})
config_processor = GitlabCiYamlProcessor.new(config, path)
it 'returns empty array' do
## ##
# When variables config is empty, we assume this is a valid # When variables config is empty, we assume this is a valid
# configuration, see issue #18775 # configuration, see issue #18775
# #
expect(config_processor.job_variables(:rspec)) expect(subject).to be_an_instance_of(Array)
.to be_an_instance_of(Array).and be_empty expect(subject).to be_empty
end end
end end
end end
end end
context 'when job variables are not defined' do context 'when job variables are not defined' do
it 'returns empty array' do let(:config) do
config = YAML.dump({ {
before_script: ['pwd'], before_script: ['pwd'],
rspec: { script: 'rspec' } rspec: { script: 'rspec' }
}) }
end
config_processor = GitlabCiYamlProcessor.new(config, path)
expect(config_processor.job_variables(:rspec)).to eq [] it 'returns empty array' do
expect(subject).to be_an_instance_of(Array)
expect(subject).to be_empty
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment