diff options
Diffstat (limited to 'spec/lib/gitlab/ci/config/yaml')
-rw-r--r-- | spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb | 121 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb | 123 |
2 files changed, 244 insertions, 0 deletions
diff --git a/spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb b/spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb new file mode 100644 index 00000000000..c68dccd3455 --- /dev/null +++ b/spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb @@ -0,0 +1,121 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Config::Yaml::Tags::Reference do + let(:config) do + Gitlab::Ci::Config::Yaml.load!(yaml) + end + + describe '.tag' do + it 'implements the tag method' do + expect(described_class.tag).to eq('!reference') + end + end + + describe '#resolve' do + subject { Gitlab::Ci::Config::Yaml::Tags::Resolver.new(config).to_hash } + + context 'with circular references' do + let(:yaml) do + <<~YML + a: !reference [b] + b: !reference [a] + YML + end + + it 'raises CircularReferenceError' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, '!reference ["b"] is part of a circular chain' + end + end + + context 'with nested circular references' do + let(:yaml) do + <<~YML + a: !reference [b, c] + b: { c: !reference [d, e, f] } + d: { e: { f: !reference [a] } } + YML + end + + it 'raises CircularReferenceError' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, '!reference ["b", "c"] is part of a circular chain' + end + end + + context 'with missing references' do + let(:yaml) { 'a: !reference [b]' } + + it 'raises MissingReferenceError' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, '!reference ["b"] could not be found' + end + end + + context 'with invalid references' do + using RSpec::Parameterized::TableSyntax + + where(:yaml, :error_message) do + 'a: !reference' | '!reference [] is not valid' + 'a: !reference str' | '!reference "str" is not valid' + 'a: !reference 1' | '!reference "1" is not valid' + 'a: !reference [1]' | '!reference [1] is not valid' + 'a: !reference { b: c }' | '!reference {"b"=>"c"} is not valid' + end + + with_them do + it 'raises an error' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, error_message + end + end + end + + context 'with arrays' do + let(:yaml) do + <<~YML + a: { b: [1, 2] } + c: { d: { e: [3, 4] } } + f: { g: [ !reference [a, b], 5, !reference [c, d, e]] } + YML + end + + it { is_expected.to match(a_hash_including({ f: { g: [[1, 2], 5, [3, 4]] } })) } + end + + context 'with hashes' do + context 'when referencing an entire hash' do + let(:yaml) do + <<~YML + a: { b: { c: 'c', d: 'd' } } + e: { f: !reference [a, b] } + YML + end + + it { is_expected.to match(a_hash_including({ e: { f: { c: 'c', d: 'd' } } })) } + end + + context 'when referencing only a hash value' do + let(:yaml) do + <<~YML + a: { b: { c: 'c', d: 'd' } } + e: { f: { g: !reference [a, b, c], h: 'h' } } + i: !reference [e, f] + YML + end + + it { is_expected.to match(a_hash_including({ i: { g: 'c', h: 'h' } })) } + end + + context 'when referencing a value before its definition' do + let(:yaml) do + <<~YML + a: { b: !reference [c, d] } + g: { h: { i: 'i', j: 1 } } + c: { d: { e: !reference [g, h, j], f: 'f' } } + YML + end + + it { is_expected.to match(a_hash_including({ a: { b: { e: 1, f: 'f' } } })) } + end + end + end +end diff --git a/spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb b/spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb new file mode 100644 index 00000000000..594242c33cc --- /dev/null +++ b/spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Config::Yaml::Tags::Resolver do + let(:config) do + Gitlab::Ci::Config::Yaml.load!(yaml) + end + + describe '#to_hash' do + subject { described_class.new(config).to_hash } + + context 'when referencing deeply nested arrays' do + let(:yaml_templates) do + <<~YML + .job-1: + script: + - echo doing step 1 of job 1 + - echo doing step 2 of job 1 + + .job-2: + script: + - echo doing step 1 of job 2 + - !reference [.job-1, script] + - echo doing step 2 of job 2 + + .job-3: + script: + - echo doing step 1 of job 3 + - !reference [.job-2, script] + - echo doing step 2 of job 3 + YML + end + + let(:job_yaml) do + <<~YML + test: + script: + - echo preparing to test + - !reference [.job-3, script] + - echo test finished + YML + end + + shared_examples 'expands references' do + it 'expands the references' do + is_expected.to match({ + '.job-1': { + script: [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ] + }, + '.job-2': { + script: [ + 'echo doing step 1 of job 2', + [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ], + 'echo doing step 2 of job 2' + ] + }, + '.job-3': { + script: [ + 'echo doing step 1 of job 3', + [ + 'echo doing step 1 of job 2', + [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ], + 'echo doing step 2 of job 2' + ], + 'echo doing step 2 of job 3' + ] + }, + test: { + script: [ + 'echo preparing to test', + [ + 'echo doing step 1 of job 3', + [ + 'echo doing step 1 of job 2', + [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ], + 'echo doing step 2 of job 2' + ], + 'echo doing step 2 of job 3' + ], + 'echo test finished' + ] + } + }) + end + end + + context 'when templates are defined before the job' do + let(:yaml) do + <<~YML + #{yaml_templates} + #{job_yaml} + YML + end + + it_behaves_like 'expands references' + end + + context 'when templates are defined after the job' do + let(:yaml) do + <<~YML + #{job_yaml} + #{yaml_templates} + YML + end + + it_behaves_like 'expands references' + end + end + end +end |