diff options
Diffstat (limited to 'spec/lib/gitlab/ci/config_spec.rb')
-rw-r--r-- | spec/lib/gitlab/ci/config_spec.rb | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/spec/lib/gitlab/ci/config_spec.rb b/spec/lib/gitlab/ci/config_spec.rb index 45ce4cac6c4..3ec4519748f 100644 --- a/spec/lib/gitlab/ci/config_spec.rb +++ b/spec/lib/gitlab/ci/config_spec.rb @@ -286,7 +286,9 @@ RSpec.describe Gitlab::Ci::Config do end context "when using 'include' directive" do - let(:project) { create(:project, :repository) } + let(:group) { create(:group) } + let(:project) { create(:project, :repository, group: group) } + let(:main_project) { create(:project, :repository, :public, group: group) } let(:remote_location) { 'https://gitlab.com/gitlab-org/gitlab-foss/blob/1234/.gitlab-ci-1.yml' } let(:local_location) { 'spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml' } @@ -317,7 +319,9 @@ RSpec.describe Gitlab::Ci::Config do include: - #{local_location} - #{remote_location} - + - project: '$MAIN_PROJECT' + ref: '$REF' + file: '$FILENAME' image: ruby:2.7 HEREDOC end @@ -331,6 +335,26 @@ RSpec.describe Gitlab::Ci::Config do allow(project.repository) .to receive(:blob_data_at).and_return(local_file_content) + + main_project.repository.create_file( + main_project.creator, + '.gitlab-ci.yml', + local_file_content, + message: 'Add README.md', + branch_name: 'master' + ) + + main_project.repository.create_file( + main_project.creator, + '.another-ci-file.yml', + local_file_content, + message: 'Add README.md', + branch_name: 'master' + ) + + create(:ci_variable, project: project, key: "REF", value: "HEAD") + create(:ci_group_variable, group: group, key: "FILENAME", value: ".gitlab-ci.yml") + create(:ci_instance_variable, key: 'MAIN_PROJECT', value: main_project.full_path) end context "when gitlab_ci_yml has valid 'include' defined" do @@ -344,6 +368,38 @@ RSpec.describe Gitlab::Ci::Config do expect(config.to_hash).to eq(composed_hash) end + + context 'handling variables' do + it 'contains all project variables' do + ref = config.context.variables.find { |v| v[:key] == 'REF' } + + expect(ref[:value]).to eq("HEAD") + end + + it 'contains all group variables' do + filename = config.context.variables.find { |v| v[:key] == 'FILENAME' } + + expect(filename[:value]).to eq(".gitlab-ci.yml") + end + + it 'contains all instance variables' do + project = config.context.variables.find { |v| v[:key] == 'MAIN_PROJECT' } + + expect(project[:value]).to eq(main_project.full_path) + end + + context 'overriding a group variable at project level' do + before do + create(:ci_variable, project: project, key: "FILENAME", value: ".another-ci-file.yml") + end + + it 'successfully overrides' do + filename = config.context.variables.to_hash[:FILENAME] + + expect(filename).to eq('.another-ci-file.yml') + end + end + end end context "when gitlab_ci.yml has invalid 'include' defined" do @@ -667,5 +723,33 @@ RSpec.describe Gitlab::Ci::Config do expect(config.to_hash).to eq(composed_hash) end end + + context "when an 'include' has rules" do + let(:gitlab_ci_yml) do + <<~HEREDOC + include: + - local: #{local_location} + rules: + - if: $CI_PROJECT_ID == "#{project_id}" + image: ruby:2.7 + HEREDOC + end + + context 'when the rules condition is satisfied' do + let(:project_id) { project.id } + + it 'includes the file' do + expect(config.to_hash).to include(local_location_hash) + end + end + + context 'when the rules condition is satisfied' do + let(:project_id) { non_existing_record_id } + + it 'does not include the file' do + expect(config.to_hash).not_to include(local_location_hash) + end + end + end end end |