debian-mirror-gitlab/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
8.3 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2020-10-24 23:57:45 +05:30
require 'spec_helper'
2018-03-27 19:54:05 +05:30
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Ci::Pipeline::Expression::Statement do
2021-04-17 20:07:23 +05:30
let(:variables) do
Gitlab::Ci::Variables::Collection.new
.append(key: 'PRESENT_VARIABLE', value: 'my variable')
.append(key: 'PATH_VARIABLE', value: 'a/path/variable/value')
.append(key: 'FULL_PATH_VARIABLE', value: '/a/full/path/variable/value')
.append(key: 'EMPTY_VARIABLE', value: '')
2022-01-26 12:08:38 +05:30
.to_hash
2018-05-09 12:01:36 +05:30
end
2021-04-17 20:07:23 +05:30
subject do
described_class.new(text, variables)
2018-03-27 19:54:05 +05:30
end
2018-05-09 12:01:36 +05:30
describe '.new' do
context 'when variables are not provided' do
it 'allows to properly initializes the statement' do
statement = described_class.new('$PRESENT_VARIABLE')
expect(statement.evaluate).to be_nil
end
end
2018-03-27 19:54:05 +05:30
end
2019-09-04 21:01:54 +05:30
describe '#evaluate' do
using RSpec::Parameterized::TableSyntax
2018-03-27 19:54:05 +05:30
2019-09-04 21:01:54 +05:30
where(:expression, :value) do
'$PRESENT_VARIABLE == "my variable"' | true
'"my variable" == $PRESENT_VARIABLE' | true
'$PRESENT_VARIABLE == null' | false
'$EMPTY_VARIABLE == null' | false
'"" == $EMPTY_VARIABLE' | true
'$EMPTY_VARIABLE' | ''
'$UNDEFINED_VARIABLE == null' | true
'null == $UNDEFINED_VARIABLE' | true
'$PRESENT_VARIABLE' | 'my variable'
'$UNDEFINED_VARIABLE' | nil
"$PRESENT_VARIABLE =~ /var.*e$/" | true
'$PRESENT_VARIABLE =~ /va\r.*e$/' | false
'$PRESENT_VARIABLE =~ /va\/r.*e$/' | false
"$PRESENT_VARIABLE =~ /var.*e$/" | true
"$PRESENT_VARIABLE =~ /^var.*/" | false
"$EMPTY_VARIABLE =~ /var.*/" | false
"$UNDEFINED_VARIABLE =~ /var.*/" | false
"$PRESENT_VARIABLE =~ /VAR.*/i" | true
'$PATH_VARIABLE =~ /path\/variable/' | true
'$FULL_PATH_VARIABLE =~ /^\/a\/full\/path\/variable\/value$/' | true
'$FULL_PATH_VARIABLE =~ /\\/path\\/variable\\/value$/' | true
'$PRESENT_VARIABLE != "my variable"' | false
'"my variable" != $PRESENT_VARIABLE' | false
'$PRESENT_VARIABLE != null' | true
'$EMPTY_VARIABLE != null' | true
'"" != $EMPTY_VARIABLE' | false
'$UNDEFINED_VARIABLE != null' | false
'null != $UNDEFINED_VARIABLE' | false
"$PRESENT_VARIABLE !~ /var.*e$/" | false
"$PRESENT_VARIABLE !~ /^var.*/" | true
'$PRESENT_VARIABLE !~ /^v\ar.*/' | true
'$PRESENT_VARIABLE !~ /^v\/ar.*/' | true
"$EMPTY_VARIABLE !~ /var.*/" | true
"$UNDEFINED_VARIABLE !~ /var.*/" | true
"$PRESENT_VARIABLE !~ /VAR.*/i" | false
'$PRESENT_VARIABLE && "string"' | 'string'
'$PRESENT_VARIABLE && $PRESENT_VARIABLE' | 'my variable'
'$PRESENT_VARIABLE && $EMPTY_VARIABLE' | ''
'$PRESENT_VARIABLE && null' | nil
'"string" && $PRESENT_VARIABLE' | 'my variable'
'$EMPTY_VARIABLE && $PRESENT_VARIABLE' | 'my variable'
'null && $PRESENT_VARIABLE' | nil
'$EMPTY_VARIABLE && "string"' | 'string'
'$EMPTY_VARIABLE && $EMPTY_VARIABLE' | ''
'"string" && $EMPTY_VARIABLE' | ''
'"string" && null' | nil
'null && "string"' | nil
'"string" && "string"' | 'string'
'null && null' | nil
'$PRESENT_VARIABLE =~ /my var/ && $EMPTY_VARIABLE =~ /nope/' | false
'$EMPTY_VARIABLE == "" && $PRESENT_VARIABLE' | 'my variable'
'$EMPTY_VARIABLE == "" && $PRESENT_VARIABLE != "nope"' | true
'$PRESENT_VARIABLE && $EMPTY_VARIABLE' | ''
'$PRESENT_VARIABLE && $UNDEFINED_VARIABLE' | nil
'$UNDEFINED_VARIABLE && $EMPTY_VARIABLE' | nil
'$UNDEFINED_VARIABLE && $PRESENT_VARIABLE' | nil
'$FULL_PATH_VARIABLE =~ /^\/a\/full\/path\/variable\/value$/ && $PATH_VARIABLE =~ /path\/variable/' | true
'$FULL_PATH_VARIABLE =~ /^\/a\/bad\/path\/variable\/value$/ && $PATH_VARIABLE =~ /path\/variable/' | false
'$FULL_PATH_VARIABLE =~ /^\/a\/full\/path\/variable\/value$/ && $PATH_VARIABLE =~ /bad\/path\/variable/' | false
'$FULL_PATH_VARIABLE =~ /^\/a\/bad\/path\/variable\/value$/ && $PATH_VARIABLE =~ /bad\/path\/variable/' | false
'$FULL_PATH_VARIABLE =~ /^\/a\/full\/path\/variable\/value$/ || $PATH_VARIABLE =~ /path\/variable/' | true
'$FULL_PATH_VARIABLE =~ /^\/a\/bad\/path\/variable\/value$/ || $PATH_VARIABLE =~ /path\/variable/' | true
'$FULL_PATH_VARIABLE =~ /^\/a\/full\/path\/variable\/value$/ || $PATH_VARIABLE =~ /bad\/path\/variable/' | true
'$FULL_PATH_VARIABLE =~ /^\/a\/bad\/path\/variable\/value$/ || $PATH_VARIABLE =~ /bad\/path\/variable/' | false
'$PRESENT_VARIABLE =~ /my var/ || $EMPTY_VARIABLE =~ /nope/' | true
'$EMPTY_VARIABLE == "" || $PRESENT_VARIABLE' | true
'$PRESENT_VARIABLE != "nope" || $EMPTY_VARIABLE == ""' | true
'$PRESENT_VARIABLE && null || $EMPTY_VARIABLE == ""' | true
'$PRESENT_VARIABLE || $UNDEFINED_VARIABLE' | 'my variable'
'$UNDEFINED_VARIABLE || $PRESENT_VARIABLE' | 'my variable'
'$UNDEFINED_VARIABLE == null || $PRESENT_VARIABLE' | true
'$PRESENT_VARIABLE || $UNDEFINED_VARIABLE == null' | 'my variable'
2020-10-24 23:57:45 +05:30
'($PRESENT_VARIABLE)' | 'my variable'
'(($PRESENT_VARIABLE))' | 'my variable'
'(($PRESENT_VARIABLE && null) || $EMPTY_VARIABLE == "")' | true
'($PRESENT_VARIABLE) && (null || $EMPTY_VARIABLE == "")' | true
'("string" || "test") == "string"' | true
'(null || ("test" == "string"))' | false
'("string" == ("test" && "string"))' | true
'("string" == ("test" || "string"))' | false
'("string" == "test" || "string")' | "string"
'("string" == ("string" || (("1" == "1") && ("2" == "3"))))' | true
2018-03-27 19:54:05 +05:30
end
2019-09-04 21:01:54 +05:30
with_them do
let(:text) { expression }
it "evaluates to `#{params[:value].inspect}`" do
expect(subject.evaluate).to eq(value)
2018-03-27 19:54:05 +05:30
end
end
end
2018-05-09 12:01:36 +05:30
describe '#truthful?' do
2018-11-08 19:23:39 +05:30
using RSpec::Parameterized::TableSyntax
where(:expression, :value) do
'$PRESENT_VARIABLE == "my variable"' | true
"$PRESENT_VARIABLE == 'no match'" | false
'$UNDEFINED_VARIABLE == null' | true
'$PRESENT_VARIABLE' | true
'$UNDEFINED_VARIABLE' | false
'$EMPTY_VARIABLE' | false
'$INVALID = 1' | false
"$PRESENT_VARIABLE =~ /var.*/" | true
"$UNDEFINED_VARIABLE =~ /var.*/" | false
2019-09-04 21:01:54 +05:30
"$PRESENT_VARIABLE !~ /var.*/" | false
"$UNDEFINED_VARIABLE !~ /var.*/" | true
2018-11-08 19:23:39 +05:30
end
with_them do
let(:text) { expression }
it "returns `#{params[:value].inspect}`" do
expect(subject.truthful?).to eq value
end
end
context 'when evaluating expression raises an error' do
let(:text) { '$PRESENT_VARIABLE' }
it 'returns false' do
allow(subject).to receive(:evaluate)
.and_raise(described_class::StatementError)
expect(subject.truthful?).to be_falsey
2018-05-09 12:01:36 +05:30
end
end
end
2018-03-27 19:54:05 +05:30
end