diff options
Diffstat (limited to 'spec/lib/gitlab/ci/variables')
-rw-r--r-- | spec/lib/gitlab/ci/variables/collection/item_spec.rb | 39 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/collection/sort_spec.rb | 11 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/collection_spec.rb | 76 |
3 files changed, 119 insertions, 7 deletions
diff --git a/spec/lib/gitlab/ci/variables/collection/item_spec.rb b/spec/lib/gitlab/ci/variables/collection/item_spec.rb index ca9dc95711d..9443bf6d6d5 100644 --- a/spec/lib/gitlab/ci/variables/collection/item_spec.rb +++ b/spec/lib/gitlab/ci/variables/collection/item_spec.rb @@ -70,6 +70,43 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Item do end end + describe '.possible_var_reference?' do + context 'table tests' do + using RSpec::Parameterized::TableSyntax + + where do + { + "empty value": { + value: '', + result: false + }, + "normal value": { + value: 'some value', + result: false + }, + "simple expansions": { + value: 'key$variable', + result: true + }, + "complex expansions": { + value: 'key${variable}${variable2}', + result: true + }, + "complex expansions for Windows": { + value: 'key%variable%%variable2%', + result: true + } + } + end + + with_them do + subject { Gitlab::Ci::Variables::Collection::Item.possible_var_reference?(value) } + + it { is_expected.to eq(result) } + end + end + end + describe '#depends_on' do let(:item) { Gitlab::Ci::Variables::Collection::Item.new(**variable) } @@ -128,7 +165,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Item do end it 'supports using an active record resource' do - variable = create(:ci_variable, key: 'CI_VAR', value: '123') + variable = build(:ci_variable, key: 'CI_VAR', value: '123') resource = described_class.fabricate(variable) expect(resource).to be_a(described_class) diff --git a/spec/lib/gitlab/ci/variables/collection/sort_spec.rb b/spec/lib/gitlab/ci/variables/collection/sort_spec.rb index 73cf0e19d00..01eef673c35 100644 --- a/spec/lib/gitlab/ci/variables/collection/sort_spec.rb +++ b/spec/lib/gitlab/ci/variables/collection/sort_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' +require 'rspec-parameterized' RSpec.describe Gitlab::Ci::Variables::Collection::Sort do describe '#initialize with non-Collection value' do @@ -57,9 +58,9 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do }, "variable containing escaped variable reference": { variables: [ - { key: 'variable_a', value: 'value' }, { key: 'variable_b', value: '$$variable_a' }, - { key: 'variable_c', value: '$variable_b' } + { key: 'variable_c', value: '$variable_a' }, + { key: 'variable_a', value: 'value' } ], expected_errors: nil } @@ -144,11 +145,11 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do }, "variable containing escaped variable reference": { variables: [ - { key: 'variable_c', value: '$variable_b' }, { key: 'variable_b', value: '$$variable_a' }, + { key: 'variable_c', value: '$variable_a' }, { key: 'variable_a', value: 'value' } ], - result: %w[variable_a variable_b variable_c] + result: %w[variable_b variable_a variable_c] } } end diff --git a/spec/lib/gitlab/ci/variables/collection_spec.rb b/spec/lib/gitlab/ci/variables/collection_spec.rb index 7b77754190a..abda27f0d6e 100644 --- a/spec/lib/gitlab/ci/variables/collection_spec.rb +++ b/spec/lib/gitlab/ci/variables/collection_spec.rb @@ -44,6 +44,30 @@ RSpec.describe Gitlab::Ci::Variables::Collection do end end + describe '#compact' do + subject do + described_class.new + .append(key: 'STRING', value: 'string') + .append(key: 'NIL', value: nil) + .append(key: nil, value: 'string') + end + + it 'returns a new Collection instance', :aggregate_failures do + collection = subject.compact + + expect(collection).to be_an_instance_of(described_class) + expect(collection).not_to eql(subject) + end + + it 'rejects pair that has nil value', :aggregate_failures do + collection = subject.compact + + expect(collection).not_to include(key: 'NIL', value: nil, public: true) + expect(collection).to include(key: 'STRING', value: 'string', public: true) + expect(collection).to include(key: nil, value: 'string', public: true) + end + end + describe '#concat' do it 'appends all elements from an array' do collection = described_class.new([{ key: 'VAR_1', value: '1' }]) @@ -229,6 +253,11 @@ RSpec.describe Gitlab::Ci::Variables::Collection do value: 'key${MISSING_VAR}-${CI_JOB_NAME}', result: 'key${MISSING_VAR}-test-1', keep_undefined: true + }, + "escaped characters are kept intact": { + value: 'key-$TEST1-%%HOME%%-$${HOME}', + result: 'key-test-3-%%HOME%%-$${HOME}', + keep_undefined: false } } end @@ -291,6 +320,14 @@ RSpec.describe Gitlab::Ci::Variables::Collection do ], keep_undefined: false }, + "escaped characters in complex expansions are kept intact": { + variables: [ + { key: 'variable3', value: 'key_${variable}_$${HOME}_%%HOME%%' }, + { key: 'variable', value: '$variable2' }, + { key: 'variable2', value: 'value2' } + ], + keep_undefined: false + }, "array with cyclic dependency": { variables: [ { key: 'variable', value: '$variable2' }, @@ -391,6 +428,30 @@ RSpec.describe Gitlab::Ci::Variables::Collection do { key: 'variable3', value: 'keyvalueresult' } ] }, + "escaped characters in complex expansions keeping undefined are kept intact": { + variables: [ + { key: 'variable3', value: 'key_${variable}_$${HOME}_%%HOME%%' }, + { key: 'variable', value: '$variable2' }, + { key: 'variable2', value: 'value' } + ], + keep_undefined: true, + result: [ + { key: 'variable', value: 'value' }, + { key: 'variable2', value: 'value' }, + { key: 'variable3', value: 'key_value_$${HOME}_%%HOME%%' } + ] + }, + "escaped characters in complex expansions discarding undefined are kept intact": { + variables: [ + { key: 'variable2', value: 'key_${variable4}_$${HOME}_%%HOME%%' }, + { key: 'variable', value: 'value_$${HOME}_%%HOME%%' } + ], + keep_undefined: false, + result: [ + { key: 'variable', value: 'value_$${HOME}_%%HOME%%' }, + { key: 'variable2', value: 'key__$${HOME}_%%HOME%%' } + ] + }, "out-of-order expansion": { variables: [ { key: 'variable3', value: 'key$variable2$variable' }, @@ -417,7 +478,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection do { key: 'variable3', value: 'keyresultvalue' } ] }, - "missing variable": { + "missing variable discarding original": { variables: [ { key: 'variable2', value: 'key$variable' } ], @@ -461,6 +522,19 @@ RSpec.describe Gitlab::Ci::Variables::Collection do { key: 'variable3', value: 'key_$variable2_value2' } ] }, + "variable value referencing password with special characters": { + variables: [ + { key: 'VAR', value: '$PASSWORD' }, + { key: 'PASSWORD', value: 'my_password$$_%%_$A' }, + { key: 'A', value: 'value' } + ], + keep_undefined: false, + result: [ + { key: 'VAR', value: 'my_password$$_%%_value' }, + { key: 'PASSWORD', value: 'my_password$$_%%_value' }, + { key: 'A', value: 'value' } + ] + }, "cyclic dependency causes original array to be returned": { variables: [ { key: 'variable', value: '$variable2' }, |