2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'fast_spec_helper'
|
|
|
|
require 'support/helpers/stub_feature_flags'
|
|
|
|
require_dependency 'active_model'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Ci::Config::Entry::Rules do
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:factory) do
|
|
|
|
Gitlab::Config::Entry::Factory.new(described_class)
|
|
|
|
.metadata(metadata)
|
|
|
|
.value(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:metadata) { { allowed_when: %w[always never] } }
|
|
|
|
let(:entry) { factory.create! }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
describe '.new' do
|
|
|
|
subject { entry }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
before do
|
|
|
|
subject.compose!
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'with a list of rule rule' do
|
|
|
|
let(:config) do
|
|
|
|
[{ if: '$THIS == "that"', when: 'never' }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_a(described_class) }
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a list of two rules' do
|
|
|
|
let(:config) do
|
|
|
|
[
|
|
|
|
{ if: '$THIS == "that"', when: 'always' },
|
|
|
|
{ if: '$SKIP', when: 'never' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'with a single rule object' do
|
|
|
|
let(:config) do
|
|
|
|
{ if: '$SKIP', when: 'never' }
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with nested rules' do
|
|
|
|
let(:config) do
|
|
|
|
[
|
|
|
|
{ if: '$THIS == "that"', when: 'always' },
|
2021-11-11 11:23:49 +05:30
|
|
|
[{ if: '$SKIP', when: 'never' }, { if: '$THIS == "other"', when: 'always' }]
|
2021-10-27 15:23:28 +05:30
|
|
|
]
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'with rules nested more than one level' do
|
2019-12-04 20:38:33 +05:30
|
|
|
let(:config) do
|
2021-10-27 15:23:28 +05:30
|
|
|
[
|
|
|
|
{ if: '$THIS == "that"', when: 'always' },
|
2021-11-11 11:23:49 +05:30
|
|
|
[{ if: '$SKIP', when: 'never' }, [{ if: '$THIS == "other"', when: 'always' }]]
|
2021-10-27 15:23:28 +05:30
|
|
|
]
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
it { is_expected.to be_valid }
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#value' do
|
|
|
|
subject { entry.value }
|
|
|
|
|
|
|
|
context 'with a list of rule rule' do
|
|
|
|
let(:config) do
|
|
|
|
[{ if: '$THIS == "that"', when: 'never' }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(config) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a list of two rules' do
|
|
|
|
let(:config) do
|
|
|
|
[
|
|
|
|
{ if: '$THIS == "that"', when: 'always' },
|
|
|
|
{ if: '$SKIP', when: 'never' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(config) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single rule object' do
|
|
|
|
let(:config) do
|
|
|
|
{ if: '$SKIP', when: 'never' }
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it { is_expected.to eq([config]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with nested rules' do
|
|
|
|
let(:first_rule) { { if: '$THIS == "that"', when: 'always' } }
|
|
|
|
let(:second_rule) { { if: '$SKIP', when: 'never' } }
|
|
|
|
|
|
|
|
let(:config) do
|
|
|
|
[
|
|
|
|
first_rule,
|
|
|
|
[second_rule]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(first_rule, second_rule) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with rules nested more than one level' do
|
|
|
|
let(:first_rule) { { if: '$THIS == "that"', when: 'always' } }
|
|
|
|
let(:second_rule) { { if: '$SKIP', when: 'never' } }
|
2021-11-11 11:23:49 +05:30
|
|
|
let(:third_rule) { { if: '$THIS == "other"', when: 'always' } }
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
let(:config) do
|
|
|
|
[
|
|
|
|
first_rule,
|
|
|
|
[second_rule, [third_rule]]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(first_rule, second_rule, third_rule) }
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.default' do
|
|
|
|
it 'does not have default policy' do
|
|
|
|
expect(described_class.default).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|