debian-mirror-gitlab/spec/lib/gitlab/ci/config/external/rules_spec.rb

74 lines
2.3 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
2021-12-11 22:18:48 +05:30
require 'spec_helper'
2021-10-27 15:23:28 +05:30
RSpec.describe Gitlab::Ci::Config::External::Rules do
let(:rule_hashes) {}
subject(:rules) { described_class.new(rule_hashes) }
describe '#evaluate' do
2022-01-26 12:08:38 +05:30
let(:context) { double(variables_hash: {}) }
2021-10-27 15:23:28 +05:30
subject(:result) { rules.evaluate(context).pass? }
context 'when there is no rule' do
it { is_expected.to eq(true) }
end
2021-11-18 22:05:49 +05:30
context 'when there is a rule with if' do
2021-10-27 15:23:28 +05:30
let(:rule_hashes) { [{ if: '$MY_VAR == "hello"' }] }
context 'when the rule matches' do
2022-01-26 12:08:38 +05:30
let(:context) { double(variables_hash: { 'MY_VAR' => 'hello' }) }
2021-10-27 15:23:28 +05:30
it { is_expected.to eq(true) }
end
context 'when the rule does not match' do
2022-01-26 12:08:38 +05:30
let(:context) { double(variables_hash: { 'MY_VAR' => 'invalid' }) }
2021-10-27 15:23:28 +05:30
it { is_expected.to eq(false) }
end
end
2021-11-18 22:05:49 +05:30
2021-12-11 22:18:48 +05:30
context 'when there is a rule with exists' do
let(:project) { create(:project, :repository) }
let(:context) { double(project: project, sha: project.repository.tree.sha, top_level_worktree_paths: ['test.md']) }
let(:rule_hashes) { [{ exists: 'Dockerfile' }] }
context 'when the file does not exist' do
it { is_expected.to eq(false) }
end
context 'when the file exists' do
let(:context) { double(project: project, sha: project.repository.tree.sha, top_level_worktree_paths: ['Dockerfile']) }
before do
2022-04-04 11:22:00 +05:30
project.repository.create_file(project.first_owner, 'Dockerfile', "commit", message: 'test', branch_name: "master")
2021-12-11 22:18:48 +05:30
end
it { is_expected.to eq(true) }
end
end
2021-11-18 22:05:49 +05:30
context 'when there is a rule with if and when' do
let(:rule_hashes) { [{ if: '$MY_VAR == "hello"', when: 'on_success' }] }
it 'raises an error' do
expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
'invalid include rule: {:if=>"$MY_VAR == \"hello\"", :when=>"on_success"}')
end
end
2021-12-11 22:18:48 +05:30
context 'when there is a rule with changes' do
let(:rule_hashes) { [{ changes: ['$MY_VAR'] }] }
2021-11-18 22:05:49 +05:30
it 'raises an error' do
expect { result }.to raise_error(described_class::InvalidIncludeRulesError,
2021-12-11 22:18:48 +05:30
'invalid include rule: {:changes=>["$MY_VAR"]}')
2021-11-18 22:05:49 +05:30
end
end
2021-10-27 15:23:28 +05:30
end
end