diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-14 11:41:52 +0300 |
commit | 585826cb22ecea5998a2c2a4675735c94bdeedac (patch) | |
tree | 5b05f0b30d33cef48963609e8a18a4dff260eab3 /spec/lib/gitlab/ci/config | |
parent | df221d036e5d0c6c0ee4d55b9c97f481ee05dee8 (diff) |
Add latest changes from gitlab-org/gitlab@16-6-stable-eev16.6.0-rc42
Diffstat (limited to 'spec/lib/gitlab/ci/config')
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/bridge_spec.rb | 60 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/commands_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/environment_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/image_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/job_spec.rb | 76 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/pages_spec.rb | 49 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/policy_spec.rb | 3 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/processable_spec.rb | 14 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/root_spec.rb | 14 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/service_spec.rb | 12 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/extendable/entry_spec.rb | 13 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/external/file/base_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/external/mapper_spec.rb | 12 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/header/input_spec.rb | 31 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/interpolation/inputs_spec.rb | 86 |
15 files changed, 290 insertions, 92 deletions
diff --git a/spec/lib/gitlab/ci/config/entry/bridge_spec.rb b/spec/lib/gitlab/ci/config/entry/bridge_spec.rb index 567ffa68836..6e6b9d949c5 100644 --- a/spec/lib/gitlab/ci/config/entry/bridge_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/bridge_spec.rb @@ -101,14 +101,16 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do describe '#value' do it 'is returns a bridge job configuration' do - expect(subject.value).to eq(name: :my_bridge, - trigger: { project: 'some/project' }, - ignore: false, - stage: 'test', - only: { refs: %w[branches tags] }, - job_variables: {}, - root_variables_inheritance: true, - scheduling_type: :stage) + expect(subject.value).to eq( + name: :my_bridge, + trigger: { project: 'some/project' }, + ignore: false, + stage: 'test', + only: { refs: %w[branches tags] }, + job_variables: {}, + root_variables_inheritance: true, + scheduling_type: :stage + ) end end end @@ -124,15 +126,16 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do describe '#value' do it 'is returns a bridge job configuration hash' do - expect(subject.value).to eq(name: :my_bridge, - trigger: { project: 'some/project', - branch: 'feature' }, - ignore: false, - stage: 'test', - only: { refs: %w[branches tags] }, - job_variables: {}, - root_variables_inheritance: true, - scheduling_type: :stage) + expect(subject.value).to eq( + name: :my_bridge, + trigger: { project: 'some/project', branch: 'feature' }, + ignore: false, + stage: 'test', + only: { refs: %w[branches tags] }, + job_variables: {}, + root_variables_inheritance: true, + scheduling_type: :stage + ) end end end @@ -283,8 +286,8 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do ignore: false, stage: 'test', only: { refs: %w[branches tags] }, - parallel: { matrix: [{ 'PROVIDER' => ['aws'], 'STACK' => %w(monitoring app1) }, - { 'PROVIDER' => ['gcp'], 'STACK' => %w(data) }] }, + parallel: { matrix: [{ 'PROVIDER' => ['aws'], 'STACK' => %w[monitoring app1] }, + { 'PROVIDER' => ['gcp'], 'STACK' => %w[data] }] }, job_variables: {}, root_variables_inheritance: true, scheduling_type: :stage @@ -305,15 +308,16 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do describe '#value' do it 'returns a bridge job configuration hash' do - expect(subject.value).to eq(name: :my_bridge, - trigger: { project: 'some/project', - forward: { pipeline_variables: true } }, - ignore: false, - stage: 'test', - only: { refs: %w[branches tags] }, - job_variables: {}, - root_variables_inheritance: true, - scheduling_type: :stage) + expect(subject.value).to eq( + name: :my_bridge, + trigger: { project: 'some/project', forward: { pipeline_variables: true } }, + ignore: false, + stage: 'test', + only: { refs: %w[branches tags] }, + job_variables: {}, + root_variables_inheritance: true, + scheduling_type: :stage + ) end end end diff --git a/spec/lib/gitlab/ci/config/entry/commands_spec.rb b/spec/lib/gitlab/ci/config/entry/commands_spec.rb index 1b8dfae692a..f84a78b4804 100644 --- a/spec/lib/gitlab/ci/config/entry/commands_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/commands_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Commands do let(:entry) { described_class.new(config) } context 'when entry config value is an array of strings' do - let(:config) { %w(ls pwd) } + let(:config) { %w[ls pwd] } describe '#value' do it 'returns array of strings' do diff --git a/spec/lib/gitlab/ci/config/entry/environment_spec.rb b/spec/lib/gitlab/ci/config/entry/environment_spec.rb index 3562706ff33..cff94a96c99 100644 --- a/spec/lib/gitlab/ci/config/entry/environment_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/environment_spec.rb @@ -93,7 +93,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Environment do context 'when valid action is used' do where(:action) do - %w(start stop prepare verify access) + %w[start stop prepare verify access] end with_them do diff --git a/spec/lib/gitlab/ci/config/entry/image_spec.rb b/spec/lib/gitlab/ci/config/entry/image_spec.rb index b37498ba10a..17c45ec4c2c 100644 --- a/spec/lib/gitlab/ci/config/entry/image_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/image_spec.rb @@ -56,7 +56,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Image do end context 'when configuration is a hash' do - let(:config) { { name: 'image:1.0', entrypoint: %w(/bin/sh run) } } + let(:config) { { name: 'image:1.0', entrypoint: %w[/bin/sh run] } } describe '#value' do it 'returns image hash' do @@ -84,13 +84,13 @@ RSpec.describe Gitlab::Ci::Config::Entry::Image do describe '#entrypoint' do it "returns image's entrypoint" do - expect(entry.entrypoint).to eq %w(/bin/sh run) + expect(entry.entrypoint).to eq %w[/bin/sh run] end end context 'when configuration has ports' do let(:ports) { [{ number: 80, protocol: 'http', name: 'foobar' }] } - let(:config) { { name: 'image:1.0', entrypoint: %w(/bin/sh run), ports: ports } } + let(:config) { { name: 'image:1.0', entrypoint: %w[/bin/sh run], ports: ports } } let(:entry) { described_class.new(config, with_image_ports: image_ports) } let(:image_ports) { false } diff --git a/spec/lib/gitlab/ci/config/entry/job_spec.rb b/spec/lib/gitlab/ci/config/entry/job_spec.rb index 1a78d929871..24d3cac6616 100644 --- a/spec/lib/gitlab/ci/config/entry/job_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/job_spec.rb @@ -598,7 +598,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_compo end end - context 'when job is not a pages job' do + context 'when job is not a pages job', feature_category: :pages do let(:name) { :rspec } context 'if the config contains a publish entry' do @@ -609,9 +609,18 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_compo expect(entry.errors).to include /job publish can only be used within a `pages` job/ end end + + context 'if the config contains a pages entry' do + let(:entry) { described_class.new({ script: 'echo', pages: { path_prefix: 'foo' } }, name: name) } + + it 'is invalid' do + expect(entry).not_to be_valid + expect(entry.errors).to include /job pages can only be used within a `pages` job/ + end + end end - context 'when job is a pages job' do + context 'when job is a pages job', feature_category: :pages do let(:name) { :pages } context 'when it does not have a publish entry' do @@ -629,6 +638,28 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_compo expect(entry).to be_valid end end + + context 'when it has a pages entry' do + let(:entry) { described_class.new({ script: 'echo', pages: { path_prefix: 'foo' } }, name: name) } + + it 'is valid' do + expect(entry).to be_valid + end + end + end + end + + describe '#pages_job?', :aggregate_failures, feature_category: :pages do + where(:name, :result) do + :pages | true + :'pages:staging' | false + :'something:pages:else' | false + end + + with_them do + subject { described_class.new({}, name: name).pages_job? } + + it { is_expected.to eq(result) } end end @@ -739,20 +770,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_compo end end - describe '#pages_job?', :aggregate_failures, feature_category: :pages do - where(:name, :result) do - :pages | true - :'pages:staging' | false - :'something:pages:else' | false - end - - with_them do - subject { described_class.new({}, name: name).pages_job? } - - it { is_expected.to eq(result) } - end - end - context 'when composed' do before do entry.compose! @@ -773,19 +790,20 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job, feature_category: :pipeline_compo end it 'returns correct value' do - expect(entry.value) - .to eq(name: :rspec, - before_script: %w[ls pwd], - script: %w[rspec], - stage: 'test', - ignore: false, - after_script: %w[cleanup], - hooks: { pre_get_sources_script: ['echo hello'] }, - only: { refs: %w[branches tags] }, - job_variables: {}, - root_variables_inheritance: true, - scheduling_type: :stage, - id_tokens: { TEST_ID_TOKEN: { aud: 'https://gitlab.com' } }) + expect(entry.value).to eq( + name: :rspec, + before_script: %w[ls pwd], + script: %w[rspec], + stage: 'test', + ignore: false, + after_script: %w[cleanup], + hooks: { pre_get_sources_script: ['echo hello'] }, + only: { refs: %w[branches tags] }, + job_variables: {}, + root_variables_inheritance: true, + scheduling_type: :stage, + id_tokens: { TEST_ID_TOKEN: { aud: 'https://gitlab.com' } } + ) end end end diff --git a/spec/lib/gitlab/ci/config/entry/pages_spec.rb b/spec/lib/gitlab/ci/config/entry/pages_spec.rb new file mode 100644 index 00000000000..0ee692a443e --- /dev/null +++ b/spec/lib/gitlab/ci/config/entry/pages_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Config::Entry::Pages, feature_category: :pages do + subject(:entry) { described_class.new(config) } + + describe 'validation' do + context 'when value given is not a hash' do + let(:config) { 'value' } + + it 'is invalid' do + expect(entry).not_to be_valid + expect(entry.errors).to include('pages config should be a hash') + end + end + + context 'when value is a hash' do + context 'when the hash is valid' do + let(:config) { { path_prefix: 'prefix' } } + + it 'is valid' do + expect(entry).to be_valid + expect(entry.value).to eq({ + path_prefix: 'prefix' + }) + end + end + + context 'when path_prefix key is not a string' do + let(:config) { { path_prefix: 1 } } + + it 'is invalid' do + expect(entry).not_to be_valid + expect(entry.errors).to include('pages path prefix should be a string') + end + end + + context 'when hash contains not allowed keys' do + let(:config) { { unknown: 'echo' } } + + it 'is invalid' do + expect(entry).not_to be_valid + expect(entry.errors).to include('pages config contains unknown keys: unknown') + end + end + end + end +end diff --git a/spec/lib/gitlab/ci/config/entry/policy_spec.rb b/spec/lib/gitlab/ci/config/entry/policy_spec.rb index 7093a0a6edf..77a895b75c0 100644 --- a/spec/lib/gitlab/ci/config/entry/policy_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/policy_spec.rb @@ -221,8 +221,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Policy, feature_category: :continuous_ let(:config) { { variables: %w[$VARIABLE] } } it 'includes default values' do - expect(entry.value).to eq(refs: %w[branches tags], - variables: %w[$VARIABLE]) + expect(entry.value).to eq(refs: %w[branches tags], variables: %w[$VARIABLE]) end end end diff --git a/spec/lib/gitlab/ci/config/entry/processable_spec.rb b/spec/lib/gitlab/ci/config/entry/processable_spec.rb index 132e75a808b..44e2fdbac37 100644 --- a/spec/lib/gitlab/ci/config/entry/processable_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/processable_spec.rb @@ -119,6 +119,20 @@ RSpec.describe Gitlab::Ci::Config::Entry::Processable, feature_category: :pipeli end end + context 'when script: and trigger: are used together' do + let(:config) do + { + script: 'echo', + trigger: 'test-group/test-project' + } + end + + it 'returns is invalid' do + expect(entry).not_to be_valid + expect(entry.errors).to include(/these keys cannot be used together: script, trigger/) + end + end + context 'when only: is used with rules:' do let(:config) { { only: ['merge_requests'], rules: [{ if: '$THIS' }] } } diff --git a/spec/lib/gitlab/ci/config/entry/root_spec.rb b/spec/lib/gitlab/ci/config/entry/root_spec.rb index 5fac5298e8e..0370bcbccf5 100644 --- a/spec/lib/gitlab/ci/config/entry/root_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/root_spec.rb @@ -31,7 +31,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do context 'when top-level entries are defined' do let(:hash) do { - before_script: %w(ls pwd), + before_script: %w[ls pwd], image: 'image:1.0', default: {}, services: ['postgres:9.1', 'mysql:5.5'], @@ -41,7 +41,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do VAR3: { value: 'val3', options: %w[val3 val4 val5], description: 'this is var 3 and some options' } }, after_script: ['make clean'], - stages: %w(build pages release), + stages: %w[build pages release], cache: { key: 'k', untracked: true, paths: ['public/'] }, rspec: { script: %w[rspec ls] }, spinach: { before_script: [], variables: {}, script: 'spinach' }, @@ -123,7 +123,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do expect(root.jobs_value[:rspec]).to eq( { name: :rspec, script: %w[rspec ls], - before_script: %w(ls pwd), + before_script: %w[ls pwd], image: { name: 'image:1.0' }, services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], stage: 'test', @@ -162,7 +162,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do services: [{ name: "postgres:9.1" }, { name: "mysql:5.5" }], cache: [{ key: "k", untracked: true, paths: ["public/"], policy: "pull-push", when: 'on_success', unprotect: false, fallback_keys: [] }], - only: { refs: %w(branches tags) }, + only: { refs: %w[branches tags] }, job_variables: { 'VAR' => { value: 'job' } }, root_variables_inheritance: true, after_script: [], @@ -176,14 +176,14 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do context 'when a mix of top-level and default entries is used' do let(:hash) do - { before_script: %w(ls pwd), + { before_script: %w[ls pwd], after_script: ['make clean'], default: { image: 'image:1.0', services: ['postgres:9.1', 'mysql:5.5'] }, variables: { VAR: 'root' }, - stages: %w(build pages), + stages: %w[build pages], cache: { key: 'k', untracked: true, paths: ['public/'] }, rspec: { script: %w[rspec ls] }, spinach: { before_script: [], variables: { VAR: 'job' }, script: 'spinach' } } @@ -205,7 +205,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do expect(root.jobs_value).to eq( rspec: { name: :rspec, script: %w[rspec ls], - before_script: %w(ls pwd), + before_script: %w[ls pwd], image: { name: 'image:1.0' }, services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], stage: 'test', diff --git a/spec/lib/gitlab/ci/config/entry/service_spec.rb b/spec/lib/gitlab/ci/config/entry/service_spec.rb index e36484bb0ae..1f935bebed5 100644 --- a/spec/lib/gitlab/ci/config/entry/service_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/service_spec.rb @@ -51,7 +51,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Service do context 'when configuration is a hash' do let(:config) do - { name: 'postgresql:9.5', alias: 'db', command: %w(cmd run), entrypoint: %w(/bin/sh run) } + { name: 'postgresql:9.5', alias: 'db', command: %w[cmd run], entrypoint: %w[/bin/sh run] } end describe '#valid?' do @@ -80,13 +80,13 @@ RSpec.describe Gitlab::Ci::Config::Entry::Service do describe '#command' do it "returns service's command" do - expect(entry.command).to eq %w(cmd run) + expect(entry.command).to eq %w[cmd run] end end describe '#entrypoint' do it "returns service's entrypoint" do - expect(entry.entrypoint).to eq %w(/bin/sh run) + expect(entry.entrypoint).to eq %w[/bin/sh run] end end @@ -99,7 +99,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Service do context 'when configuration has ports' do let(:ports) { [{ number: 80, protocol: 'http', name: 'foobar' }] } let(:config) do - { name: 'postgresql:9.5', alias: 'db', command: %w(cmd run), entrypoint: %w(/bin/sh run), ports: ports } + { name: 'postgresql:9.5', alias: 'db', command: %w[cmd run], entrypoint: %w[/bin/sh run], ports: ports } end let(:entry) { described_class.new(config, with_image_ports: image_ports) } @@ -198,7 +198,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Service do context 'when service has ports' do let(:ports) { [{ number: 80, protocol: 'http', name: 'foobar' }] } let(:config) do - { name: 'postgresql:9.5', command: %w(cmd run), entrypoint: %w(/bin/sh run), ports: ports } + { name: 'postgresql:9.5', command: %w[cmd run], entrypoint: %w[/bin/sh run], ports: ports } end it 'alias field is mandatory' do @@ -209,7 +209,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Service do context 'when service does not have ports' do let(:config) do - { name: 'postgresql:9.5', alias: 'db', command: %w(cmd run), entrypoint: %w(/bin/sh run) } + { name: 'postgresql:9.5', alias: 'db', command: %w[cmd run], entrypoint: %w[/bin/sh run] } end it 'alias field is optional' do diff --git a/spec/lib/gitlab/ci/config/extendable/entry_spec.rb b/spec/lib/gitlab/ci/config/extendable/entry_spec.rb index 69aa3bab77a..b57a56b8389 100644 --- a/spec/lib/gitlab/ci/config/extendable/entry_spec.rb +++ b/spec/lib/gitlab/ci/config/extendable/entry_spec.rb @@ -128,8 +128,7 @@ RSpec.describe Gitlab::Ci::Config::Extendable::Entry do it 'raises an error' do expect { subject.extend! } - .to raise_error(described_class::InvalidExtensionError, - /invalid base hash/) + .to raise_error(described_class::InvalidExtensionError, /invalid base hash/) end end @@ -140,8 +139,7 @@ RSpec.describe Gitlab::Ci::Config::Extendable::Entry do it 'raises an error' do expect { subject.extend! } - .to raise_error(described_class::InvalidExtensionError, - /unknown key/) + .to raise_error(described_class::InvalidExtensionError, /unknown key/) end end @@ -178,7 +176,7 @@ RSpec.describe Gitlab::Ci::Config::Extendable::Entry do { first: { script: 'my value', image: 'ubuntu' }, second: { image: 'alpine' }, - test: { extends: %w(first second) } + test: { extends: %w[first second] } } end @@ -186,7 +184,7 @@ RSpec.describe Gitlab::Ci::Config::Extendable::Entry do { first: { script: 'my value', image: 'ubuntu' }, second: { image: 'alpine' }, - test: { extends: %w(first second), script: 'my value', image: 'alpine' } + test: { extends: %w[first second], script: 'my value', image: 'alpine' } } end @@ -230,8 +228,7 @@ RSpec.describe Gitlab::Ci::Config::Extendable::Entry do it 'raises an error' do expect { subject.extend! } - .to raise_error(described_class::CircularDependencyError, - /circular dependency detected/) + .to raise_error(described_class::CircularDependencyError, /circular dependency detected/) end end diff --git a/spec/lib/gitlab/ci/config/external/file/base_spec.rb b/spec/lib/gitlab/ci/config/external/file/base_spec.rb index 1415dbeb532..bcfab620bd9 100644 --- a/spec/lib/gitlab/ci/config/external/file/base_spec.rb +++ b/spec/lib/gitlab/ci/config/external/file/base_spec.rb @@ -60,7 +60,7 @@ RSpec.describe Gitlab::Ci::Config::External::File::Base, feature_category: :pipe end context 'when location is not a string' do - let(:location) { %w(some/file.txt other/file.txt) } + let(:location) { %w[some/file.txt other/file.txt] } it { is_expected.to be_falsy } end diff --git a/spec/lib/gitlab/ci/config/external/mapper_spec.rb b/spec/lib/gitlab/ci/config/external/mapper_spec.rb index 56d1ddee4b8..5f28b45496f 100644 --- a/spec/lib/gitlab/ci/config/external/mapper_spec.rb +++ b/spec/lib/gitlab/ci/config/external/mapper_spec.rb @@ -406,8 +406,10 @@ RSpec.describe Gitlab::Ci::Config::External::Mapper, feature_category: :pipeline end it 'includes the matched local files' do - expect(subject).to contain_exactly(an_instance_of(Gitlab::Ci::Config::External::File::Local), - an_instance_of(Gitlab::Ci::Config::External::File::Local)) + expect(subject).to contain_exactly( + an_instance_of(Gitlab::Ci::Config::External::File::Local), + an_instance_of(Gitlab::Ci::Config::External::File::Local) + ) expect(subject.map(&:location)).to contain_exactly('myfolder/file1.yml', 'myfolder/file2.yml') end @@ -424,8 +426,10 @@ RSpec.describe Gitlab::Ci::Config::External::Mapper, feature_category: :pipeline let(:project_id) { project.id } it 'includes the file' do - expect(subject).to contain_exactly(an_instance_of(Gitlab::Ci::Config::External::File::Remote), - an_instance_of(Gitlab::Ci::Config::External::File::Local)) + expect(subject).to contain_exactly( + an_instance_of(Gitlab::Ci::Config::External::File::Remote), + an_instance_of(Gitlab::Ci::Config::External::File::Local) + ) end end diff --git a/spec/lib/gitlab/ci/config/header/input_spec.rb b/spec/lib/gitlab/ci/config/header/input_spec.rb index 5d1fa4a8e6e..df70d1fd7c8 100644 --- a/spec/lib/gitlab/ci/config/header/input_spec.rb +++ b/spec/lib/gitlab/ci/config/header/input_spec.rb @@ -40,12 +40,24 @@ RSpec.describe Gitlab::Ci::Config::Header::Input, feature_category: :pipeline_co end end - context 'when has a default value' do + context 'when has a string default value' do let(:input_hash) { { default: 'bar' } } it_behaves_like 'a valid input' end + context 'when has a numeric default value' do + let(:input_hash) { { default: 6.66 } } + + it_behaves_like 'a valid input' + end + + context 'when has a boolean default value' do + let(:input_hash) { { default: true } } + + it_behaves_like 'a valid input' + end + context 'when has a description value' do let(:input_hash) { { description: 'bar' } } @@ -103,4 +115,21 @@ RSpec.describe Gitlab::Ci::Config::Header::Input, feature_category: :pipeline_co it_behaves_like 'an invalid input' end + + context 'when the limit for allowed number of options is reached' do + let(:limit) { described_class::ALLOWED_OPTIONS_LIMIT } + let(:input_hash) { { default: 'value1', options: options } } + let(:options) { Array.new(limit.next) { |i| "value#{i}" } } + + describe '#valid?' do + it { is_expected.not_to be_valid } + end + + describe '#errors' do + it 'returns error about incorrect type' do + expect(config.errors).to contain_exactly( + "foo config cannot define more than #{limit} options") + end + end + end end diff --git a/spec/lib/gitlab/ci/config/interpolation/inputs_spec.rb b/spec/lib/gitlab/ci/config/interpolation/inputs_spec.rb index b0618081207..57ced4eab98 100644 --- a/spec/lib/gitlab/ci/config/interpolation/inputs_spec.rb +++ b/spec/lib/gitlab/ci/config/interpolation/inputs_spec.rb @@ -7,6 +7,90 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::Inputs, feature_category: :pip let(:specs) { { foo: { default: 'bar' } } } let(:args) { {} } + context 'when inputs are valid strings and have options' do + let(:specs) { { foo: { default: 'one', options: %w[one two three] } } } + + context 'and the value is selected' do + let(:args) { { foo: 'two' } } + + it 'assigns the selected value' do + expect(inputs).to be_valid + expect(inputs.to_hash).to eq({ foo: 'two' }) + end + end + + context 'and the value is not selected' do + it 'assigns the default value' do + expect(inputs).to be_valid + expect(inputs.to_hash).to eq({ foo: 'one' }) + end + end + end + + context 'when inputs options are valid integers' do + let(:specs) { { foo: { default: 1, options: [1, 2, 3, 4, 5], type: 'number' } } } + + context 'and a value of the wrong type is given' do + let(:args) { { foo: 'word' } } + + it 'returns an error' do + expect(inputs).not_to be_valid + expect(inputs.errors).to contain_exactly( + "`foo` input: `word` cannot be used because it is not in the list of the allowed options", + "`foo` input: provided value is not a number" + ) + end + end + + context 'and the value is selected' do + let(:args) { { foo: 2 } } + + it 'assigns the selected value' do + expect(inputs).to be_valid + expect(inputs.to_hash).to eq({ foo: 2 }) + end + end + + context 'and the value is not selected' do + it 'assigns the default value' do + expect(inputs).to be_valid + expect(inputs.to_hash).to eq({ foo: 1 }) + end + end + end + + context 'when inputs have invalid type options' do + let(:specs) { { foo: { default: true, options: [true, false], type: 'boolean' } } } + + it 'returns an error' do + expect(inputs).not_to be_valid + expect(inputs.errors).to contain_exactly("`foo` input: Options can only be used with string and number inputs") + end + end + + context 'when inputs are valid with options but the default value is not in the options' do + let(:specs) { { foo: { default: 'coop', options: %w[one two three] } } } + + it 'returns an error' do + expect(inputs).not_to be_valid + expect(inputs.errors).to contain_exactly( + '`foo` input: `coop` cannot be used because it is not in the list of allowed options' + ) + end + end + + context 'when inputs are valid with options but the value is not in the options' do + let(:specs) { { foo: { default: 'one', options: %w[one two three] } } } + let(:args) { { foo: 'niet' } } + + it 'returns an error' do + expect(inputs).not_to be_valid + expect(inputs.errors).to contain_exactly( + '`foo` input: `niet` cannot be used because it is not in the list of allowed options' + ) + end + end + context 'when given unrecognized inputs' do let(:specs) { { foo: nil } } let(:args) { { foo: 'bar', test: 'bar' } } @@ -164,7 +248,7 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::Inputs, feature_category: :pip context 'when the value is not a number' do let(:specs) { { number_input: { type: 'number' } } } - let(:args) { { number_input: 'NaN' } } + let(:args) { { number_input: false } } it 'is invalid' do expect(inputs).not_to be_valid |