2019-09-24 15:06:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 09:08:32 +00:00
|
|
|
RSpec.describe Gitlab::Ci::Build::Rules::Rule::Clause::Exists do
|
2022-08-25 12:12:20 +00:00
|
|
|
describe '#satisfied_by?' do
|
2022-11-07 15:11:00 +00:00
|
|
|
subject(:satisfied_by?) { described_class.new(globs).satisfied_by?(nil, context) }
|
|
|
|
|
|
|
|
shared_examples 'a rules:exists with a context' do
|
2022-08-25 12:12:20 +00:00
|
|
|
it_behaves_like 'a glob matching rule' do
|
|
|
|
let(:project) { create(:project, :custom_repo, files: files) }
|
|
|
|
end
|
2019-09-26 06:06:27 +00:00
|
|
|
|
2022-11-07 15:11:00 +00:00
|
|
|
context 'when the rules:exists has a variable' do
|
|
|
|
let_it_be(:project) { create(:project, :custom_repo, files: { 'helm/helm_file.txt' => '' }) }
|
|
|
|
|
|
|
|
let(:globs) { ['$HELM_DIR/**/*'] }
|
|
|
|
|
|
|
|
let(:variables_hash) do
|
|
|
|
{ 'HELM_DIR' => 'helm' }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(context).to receive(:variables_hash).and_return(variables_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the ci_variables_rules_exists FF is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_variable_expansion_in_rules_exists: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the ci_variables_rules_exists FF is enabled' do
|
|
|
|
context 'when the context has the specified variables' do
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when variable expansion does not match' do
|
|
|
|
let(:variables_hash) { {} }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-25 12:12:20 +00:00
|
|
|
context 'after pattern comparision limit is reached' do
|
|
|
|
let(:globs) { ['*definitely_not_a_matching_glob*'] }
|
|
|
|
let(:project) { create(:project, :repository) }
|
2019-09-26 06:06:27 +00:00
|
|
|
|
2022-08-25 12:12:20 +00:00
|
|
|
before do
|
|
|
|
stub_const('Gitlab::Ci::Build::Rules::Rule::Clause::Exists::MAX_PATTERN_COMPARISONS', 2)
|
|
|
|
expect(File).to receive(:fnmatch?).twice.and_call_original
|
|
|
|
end
|
2019-09-26 06:06:27 +00:00
|
|
|
|
2022-08-25 12:12:20 +00:00
|
|
|
it { is_expected.to be_truthy }
|
2019-09-26 06:06:27 +00:00
|
|
|
end
|
2019-09-24 15:06:34 +00:00
|
|
|
end
|
2021-11-11 15:10:57 +00:00
|
|
|
|
2022-11-07 15:11:00 +00:00
|
|
|
context 'when the rules are being evaluated at job level' do
|
|
|
|
it_behaves_like 'a rules:exists with a context' do
|
2022-08-25 12:12:20 +00:00
|
|
|
let(:pipeline) { build(:ci_pipeline, project: project, sha: project.repository.commit.sha) }
|
|
|
|
let(:context) { Gitlab::Ci::Build::Context::Build.new(pipeline, sha: project.repository.commit.sha) }
|
2021-11-11 15:10:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-07 15:11:00 +00:00
|
|
|
context 'when the rules are being evaluated for an entire pipeline' do
|
|
|
|
it_behaves_like 'a rules:exists with a context' do
|
2022-08-25 12:12:20 +00:00
|
|
|
let(:pipeline) { build(:ci_pipeline, project: project, sha: project.repository.commit.sha) }
|
2021-11-11 15:10:57 +00:00
|
|
|
let(:context) { Gitlab::Ci::Build::Context::Global.new(pipeline, yaml_variables: {}) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-07 15:11:00 +00:00
|
|
|
context 'when rules are being evaluated with `include`' do
|
2022-08-25 12:12:20 +00:00
|
|
|
let(:context) { Gitlab::Ci::Config::External::Context.new(project: project, sha: sha) }
|
|
|
|
|
2022-11-07 15:11:00 +00:00
|
|
|
it_behaves_like 'a rules:exists with a context' do
|
2022-08-25 12:12:20 +00:00
|
|
|
let(:sha) { project.repository.commit.sha }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when context has no project' do
|
|
|
|
let(:globs) { ['Dockerfile'] }
|
|
|
|
let(:project) {}
|
|
|
|
let(:sha) { 'abc1234' }
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
2021-11-11 15:10:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-24 15:06:34 +00:00
|
|
|
end
|