diff options
Diffstat (limited to 'spec/lib/gitlab/ci/config/external/mapper_spec.rb')
-rw-r--r-- | spec/lib/gitlab/ci/config/external/mapper_spec.rb | 126 |
1 files changed, 114 insertions, 12 deletions
diff --git a/spec/lib/gitlab/ci/config/external/mapper_spec.rb b/spec/lib/gitlab/ci/config/external/mapper_spec.rb index 7ad57827e30..4fdaaca8316 100644 --- a/spec/lib/gitlab/ci/config/external/mapper_spec.rb +++ b/spec/lib/gitlab/ci/config/external/mapper_spec.rb @@ -10,7 +10,7 @@ RSpec.describe Gitlab::Ci::Config::External::Mapper do let(:local_file) { '/lib/gitlab/ci/templates/non-existent-file.yml' } let(:remote_url) { 'https://gitlab.com/gitlab-org/gitlab-foss/blob/1234/.gitlab-ci-1.yml' } let(:template_file) { 'Auto-DevOps.gitlab-ci.yml' } - let(:context_params) { { project: project, sha: '123456', user: user } } + let(:context_params) { { project: project, sha: '123456', user: user, variables: project.predefined_variables.to_runner_variables } } let(:context) { Gitlab::Ci::Config::External::Context.new(**context_params) } let(:file_content) do @@ -124,17 +124,6 @@ RSpec.describe Gitlab::Ci::Config::External::Mapper do an_instance_of(Gitlab::Ci::Config::External::File::Project), an_instance_of(Gitlab::Ci::Config::External::File::Project)) end - - context 'when FF ci_include_multiple_files_from_project is disabled' do - before do - stub_feature_flags(ci_include_multiple_files_from_project: false) - end - - it 'returns a File instance' do - expect(subject).to contain_exactly( - an_instance_of(Gitlab::Ci::Config::External::File::Project)) - end - end end end @@ -236,5 +225,118 @@ RSpec.describe Gitlab::Ci::Config::External::Mapper do end end end + + context "when 'include' section uses project variable" do + let(:full_local_file_path) { '$CI_PROJECT_PATH' + local_file } + + context 'when local file is included as a single string' do + let(:values) do + { include: full_local_file_path } + end + + it 'expands the variable', :aggregate_failures do + expect(subject[0].location).to eq(project.full_path + local_file) + expect(subject).to contain_exactly(an_instance_of(Gitlab::Ci::Config::External::File::Local)) + end + end + + context 'when remote file is included as a single string' do + let(:remote_url) { "#{Gitlab.config.gitlab.url}/radio/.gitlab-ci.yml" } + + let(:values) do + { include: '$CI_SERVER_URL/radio/.gitlab-ci.yml' } + end + + it 'expands the variable', :aggregate_failures do + expect(subject[0].location).to eq(remote_url) + expect(subject).to contain_exactly(an_instance_of(Gitlab::Ci::Config::External::File::Remote)) + end + end + + context 'defined as an array' do + let(:values) do + { include: [full_local_file_path, remote_url], + image: 'ruby:2.7' } + end + + it 'expands the variable' do + expect(subject[0].location).to eq(project.full_path + local_file) + expect(subject[1].location).to eq(remote_url) + end + end + + context 'defined as an array of hashes' do + let(:values) do + { include: [{ local: full_local_file_path }, { remote: remote_url }], + image: 'ruby:2.7' } + end + + it 'expands the variable' do + expect(subject[0].location).to eq(project.full_path + local_file) + expect(subject[1].location).to eq(remote_url) + end + end + + context 'local file hash' do + let(:values) do + { include: { 'local' => full_local_file_path } } + end + + it 'expands the variable' do + expect(subject[0].location).to eq(project.full_path + local_file) + end + end + + context 'project name' do + let(:values) do + { include: { project: '$CI_PROJECT_PATH', file: local_file }, + image: 'ruby:2.7' } + end + + it 'expands the variable', :aggregate_failures do + expect(subject[0].project_name).to eq(project.full_path) + expect(subject).to contain_exactly(an_instance_of(Gitlab::Ci::Config::External::File::Project)) + end + end + + context 'with multiple files' do + let(:values) do + { include: { project: project.full_path, file: [full_local_file_path, 'another_file_path.yml'] }, + image: 'ruby:2.7' } + end + + it 'expands the variable' do + expect(subject[0].location).to eq(project.full_path + local_file) + expect(subject[1].location).to eq('another_file_path.yml') + end + end + + context 'when include variable has an unsupported type for variable expansion' do + let(:values) do + { include: { project: project.id, file: local_file }, + image: 'ruby:2.7' } + end + + it 'does not invoke expansion for the variable', :aggregate_failures do + expect(ExpandVariables).not_to receive(:expand).with(project.id, context_params[:variables]) + + expect { subject }.to raise_error(described_class::AmbigiousSpecificationError) + end + end + + context 'when feature flag is turned off' do + let(:values) do + { include: full_local_file_path } + end + + before do + stub_feature_flags(variables_in_include_section_ci: false) + end + + it 'does not expand the variables' do + expect(subject[0].location).to eq('$CI_PROJECT_PATH' + local_file) + end + end + end end end |