Commit 9e5117ee authored by Matija Čupić's avatar Matija Čupić

Flatten Entry::Commands elements

Flattens Entry::Commands elements so we can use YAML anchors as part of
the script definition.
parent 7f936a16
......@@ -11,11 +11,11 @@ module Gitlab
include ::Gitlab::Config::Entry::Validatable
validations do
validates :config, array_of_strings_or_string: true
validates :config, array_of_strings_or_arrays_of_strings: true
end
def value
Array(@config)
Array(@config).flatten
end
end
end
......
......@@ -206,6 +206,16 @@ module Gitlab
end
end
class ArrayOfStringsOrArraysOfStringsValidator < ActiveModel::EachValidator
include LegacyValidationHelpers
def validate_each(record, attribute, value)
unless value.is_a?(Array) && value.all? { |element| element.is_a?(String) || validate_array_of_strings(element) }
record.errors.add(attribute, 'should be an array of strings and arrays of strings')
end
end
end
class ArrayOfStringsOrRegexpsWithFallbackValidator < ArrayOfStringsOrRegexpsValidator
protected
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
describe Gitlab::Ci::Config::Entry::Commands do
let(:entry) { described_class.new(config) }
context 'when entry config value is an array' do
context 'when entry config value is an array of strings' do
let(:config) { %w(ls pwd) }
describe '#value' do
......@@ -37,13 +37,57 @@ describe Gitlab::Ci::Config::Entry::Commands do
end
end
context 'when entry config value is array of arrays of strings' do
let(:config) { [['ls'], ['pwd', 'echo 1']] }
describe '#value' do
it 'returns array of strings' do
expect(entry.value).to eq ['ls', 'pwd', 'echo 1']
end
end
describe '#errors' do
it 'does not append errors' do
expect(entry.errors).to be_empty
end
end
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
end
end
end
context 'when entry config value is array of strings and arrays of strings' do
let(:config) { ['ls', ['pwd', 'echo 1']] }
describe '#value' do
it 'returns array of strings' do
expect(entry.value).to eq ['ls', 'pwd', 'echo 1']
end
end
describe '#errors' do
it 'does not append errors' do
expect(entry.errors).to be_empty
end
end
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
end
end
end
context 'when entry value is not valid' do
let(:config) { 1 }
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include 'commands config should be an array of strings or a string'
.to include 'commands config should be an array of strings and arrays of strings'
end
end
end
......
......@@ -363,6 +363,7 @@ module Gitlab
end
describe "script" do
context 'when script is array of strings' do
let(:config) do
{
test: { script: ["script"] }
......@@ -374,6 +375,19 @@ module Gitlab
end
end
context 'when script is array of arrays of strings' do
let(:config) do
{
test: { script: [["script"], ["echo 1"], "ls"] }
}
end
it "return commands with scripts concencaced" do
expect(subject[:options][:script]).to eq(["script", "echo 1", "ls"])
end
end
end
describe "after_script" do
context "in global context" do
let(:config) do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment