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