Commit 70bda3e8 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement script in Ci config and use in legacy one

parent c2d6d61d
...@@ -82,7 +82,7 @@ module Ci ...@@ -82,7 +82,7 @@ module Ci
{ {
stage_idx: stages.index(job[:stage]), stage_idx: stages.index(job[:stage]),
stage: job[:stage], stage: job[:stage],
commands: [job[:before_script] || @before_script, job[:script]].flatten.join("\n"), commands: [job[:before_script] || [@ci_config.before_script], job[:script]].flatten.compact.join("\n"),
tag_list: job[:tags] || [], tag_list: job[:tags] || [],
name: name, name: name,
only: job[:only], only: job[:only],
......
...@@ -5,6 +5,11 @@ module Gitlab ...@@ -5,6 +5,11 @@ module Gitlab
delegate :valid?, :errors, to: :@global delegate :valid?, :errors, to: :@global
##
# Temporary delegations that should be removed after refactoring
#
delegate :before_script, to: :@global
def initialize(config) def initialize(config)
loader = Loader.new(config) loader = Loader.new(config)
......
...@@ -5,6 +5,16 @@ module Gitlab ...@@ -5,6 +5,16 @@ module Gitlab
class BeforeScript < Entry class BeforeScript < Entry
include ValidationHelpers include ValidationHelpers
def description
'Script that is executed before the one defined in a job.'
end
def script
raise unless valid?
@value.join("\n")
end
def validate! def validate!
unless validate_array_of_strings(@value) unless validate_array_of_strings(@value)
@errors << 'before_script should be an array of strings' @errors << 'before_script should be an array of strings'
......
...@@ -55,6 +55,10 @@ module Gitlab ...@@ -55,6 +55,10 @@ module Gitlab
raise NotImplementedError raise NotImplementedError
end end
def description
raise NotImplementedError
end
class << self class << self
attr_reader :nodes attr_reader :nodes
......
...@@ -4,6 +4,10 @@ module Gitlab ...@@ -4,6 +4,10 @@ module Gitlab
module Node module Node
class Global < Entry class Global < Entry
add_node :before_script, BeforeScript add_node :before_script, BeforeScript
def before_script
@before_script.script
end
end end
end end
end end
......
...@@ -2,25 +2,38 @@ require 'spec_helper' ...@@ -2,25 +2,38 @@ require 'spec_helper'
describe Gitlab::Ci::Config::Node::BeforeScript do describe Gitlab::Ci::Config::Node::BeforeScript do
let(:entry) { described_class.new(value, double)} let(:entry) { described_class.new(value, double)}
before { entry.validate! }
describe '#validate!' do context 'when entry value is correct' do
before { entry.validate! } let(:value) { ['ls', 'pwd'] }
context 'when entry value is correct' do describe '#script' do
let(:value) { ['ls', 'pwd'] } it 'returns concatenated command' do
expect(entry.script).to eq "ls\npwd"
end
end
describe '#errors' do
it 'does not append errors' do it 'does not append errors' do
expect(entry.errors).to be_empty expect(entry.errors).to be_empty
end end
end end
end
context 'when entry value is not correct' do context 'when entry value is not correct' do
let(:value) { 'ls' } let(:value) { 'ls' }
describe '#errors' do
it 'saves errors' do it 'saves errors' do
expect(entry.errors) expect(entry.errors)
.to include /should be an array of strings/ .to include /should be an array of strings/
end end
end end
describe '#script' do
it 'raises error' do
expect { entry.script }.to raise_error
end
end
end end
end end
...@@ -3,6 +3,8 @@ require 'spec_helper' ...@@ -3,6 +3,8 @@ require 'spec_helper'
describe Gitlab::Ci::Config::Node::Global do describe Gitlab::Ci::Config::Node::Global do
let(:global) { described_class.new(hash) } let(:global) { described_class.new(hash) }
before { global.process! }
describe '#keys' do describe '#keys' do
it 'can contain global config keys' do it 'can contain global config keys' do
expect(global.keys).to include :before_script expect(global.keys).to include :before_script
...@@ -19,8 +21,6 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -19,8 +21,6 @@ describe Gitlab::Ci::Config::Node::Global do
end end
describe '#process!' do describe '#process!' do
before { global.process! }
it 'creates nodes hash' do it 'creates nodes hash' do
expect(global.nodes).to be_an Array expect(global.nodes).to be_an Array
end end
...@@ -40,6 +40,12 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -40,6 +40,12 @@ describe Gitlab::Ci::Config::Node::Global do
expect(global).not_to be_leaf expect(global).not_to be_leaf
end end
end end
describe '#before_script' do
it 'returns correct script' do
expect(global.before_script).to eq "ls\npwd"
end
end
end end
context 'when hash is not valid' do context 'when hash is not valid' do
...@@ -47,8 +53,6 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -47,8 +53,6 @@ describe Gitlab::Ci::Config::Node::Global do
{ before_script: 'ls' } { before_script: 'ls' }
end end
before { global.process! }
describe '#valid?' do describe '#valid?' do
it 'is not valid' do it 'is not valid' do
expect(global).not_to be_valid expect(global).not_to be_valid
...@@ -66,8 +70,6 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -66,8 +70,6 @@ describe Gitlab::Ci::Config::Node::Global do
context 'when value is not a hash' do context 'when value is not a hash' do
let(:hash) { [] } let(:hash) { [] }
before { global.process! }
describe '#valid?' do describe '#valid?' do
it 'is not valid' do it 'is not valid' do
expect(global).not_to be_valid expect(global).not_to be_valid
......
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