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

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 ...@@ -11,11 +11,11 @@ module Gitlab
include ::Gitlab::Config::Entry::Validatable include ::Gitlab::Config::Entry::Validatable
   
validations do validations do
validates :config, array_of_strings_or_string: true validates :config, array_of_strings_or_arrays_of_strings: true
end end
   
def value def value
Array(@config) Array(@config).flatten
end end
end end
end end
......
...@@ -206,6 +206,16 @@ module Gitlab ...@@ -206,6 +206,16 @@ module Gitlab
end end
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 class ArrayOfStringsOrRegexpsWithFallbackValidator < ArrayOfStringsOrRegexpsValidator
protected protected
   
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe Gitlab::Ci::Config::Entry::Commands do describe Gitlab::Ci::Config::Entry::Commands do
let(:entry) { described_class.new(config) } 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) } let(:config) { %w(ls pwd) }
   
describe '#value' do describe '#value' do
...@@ -37,13 +37,57 @@ describe Gitlab::Ci::Config::Entry::Commands do ...@@ -37,13 +37,57 @@ describe Gitlab::Ci::Config::Entry::Commands do
end end
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 context 'when entry value is not valid' do
let(:config) { 1 } let(:config) { 1 }
   
describe '#errors' do describe '#errors' do
it 'saves errors' do it 'saves errors' do
expect(entry.errors) 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 end
end end
......
...@@ -363,14 +363,28 @@ module Gitlab ...@@ -363,14 +363,28 @@ module Gitlab
end end
   
describe "script" do describe "script" do
let(:config) do context 'when script is array of strings' do
{ let(:config) do
test: { script: ["script"] } {
} test: { script: ["script"] }
}
end
it "return commands with scripts concencaced" do
expect(subject[:options][:script]).to eq(["script"])
end
end end
   
it "return commands with scripts concencaced" do context 'when script is array of arrays of strings' do
expect(subject[:options][:script]).to eq(["script"]) 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
end end
   
......
Supports Markdown
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