Commit 6920390a authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add before script and commands to CI job entry

parent 097550f0
...@@ -10,6 +10,7 @@ module Gitlab ...@@ -10,6 +10,7 @@ module Gitlab
validations do validations do
validates :config, presence: true validates :config, presence: true
validates :global, required: true, on: :processed
end end
node :before_script, Script, node :before_script, Script,
...@@ -30,8 +31,6 @@ module Gitlab ...@@ -30,8 +31,6 @@ module Gitlab
helpers :before_script, :script, :stage, :type, :after_script helpers :before_script, :script, :stage, :type, :after_script
def value def value
raise InvalidError unless valid?
## ##
# TODO, refactoring step: do not expose internal configuration, # TODO, refactoring step: do not expose internal configuration,
# return only hash value without merging it to internal config. # return only hash value without merging it to internal config.
...@@ -39,6 +38,18 @@ module Gitlab ...@@ -39,6 +38,18 @@ module Gitlab
@config.merge(to_hash.compact) @config.merge(to_hash.compact)
end end
def before_script
if before_script_defined?
before_script_value.to_a
else
@global.before_script.to_a
end
end
def commands
(before_script + script).join("\n")
end
private private
def to_hash def to_hash
......
...@@ -60,14 +60,62 @@ describe Gitlab::Ci::Config::Node::Job do ...@@ -60,14 +60,62 @@ describe Gitlab::Ci::Config::Node::Job do
after_script: %w[cleanup]) after_script: %w[cleanup])
end end
end end
end
context 'when entry is incorrect' do describe '#before_script' do
let(:config) { {} } context 'when global entry has before script' do
before do
allow(global).to receive(:before_script)
.and_return(%w[ls pwd])
end
context 'when before script is overridden' do
let(:config) do
{ before_script: %w[whoami],
script: 'rspec' }
end
it 'returns correct script' do
expect(entry.before_script).to eq %w[whoami]
end
end
context 'when before script is not overriden' do
let(:config) do
{ script: %w[spinach] }
end
it 'raises error' do it 'returns correct script' do
expect { entry.value }.to raise_error( expect(entry.before_script).to eq %w[ls pwd]
Gitlab::Ci::Config::Node::Entry::InvalidError end
) end
end
context 'when global entry does not have before script' do
before do
allow(global).to receive(:before_script)
.and_return(nil)
end
context 'when job has before script' do
let(:config) do
{ before_script: %w[whoami],
script: 'rspec' }
end
it 'returns correct script' do
expect(entry.before_script).to eq %w[whoami]
end
end
context 'when job does not have before script' do
let(:config) do
{ script: %w[ls test] }
end
it 'returns correct script' do
expect(entry.before_script).to eq []
end
end end
end end
end end
...@@ -77,4 +125,61 @@ describe Gitlab::Ci::Config::Node::Job do ...@@ -77,4 +125,61 @@ describe Gitlab::Ci::Config::Node::Job do
expect(entry).to be_relevant expect(entry).to be_relevant
end end
end end
describe '#commands' do
context 'when global entry has before script' do
before do
allow(global).to receive(:before_script)
.and_return(%w[ls pwd])
end
context 'when before script is overridden' do
let(:config) do
{ before_script: %w[whoami],
script: 'rspec' }
end
it 'returns correct commands' do
expect(entry.commands).to eq "whoami\nrspec"
end
end
context 'when before script is not overriden' do
let(:config) do
{ script: %w[rspec spinach] }
end
it 'returns correct commands' do
expect(entry.commands).to eq "ls\npwd\nrspec\nspinach"
end
end
end
context 'when global entry does not have before script' do
before do
allow(global).to receive(:before_script)
.and_return(nil)
end
context 'when job has before script' do
let(:config) do
{ before_script: %w[whoami],
script: 'rspec' }
end
it 'returns correct commands' do
expect(entry.commands).to eq "whoami\nrspec"
end
end
context 'when job does not have before script' do
let(:config) do
{ script: %w[ls test] }
end
it 'returns correct commands' do
expect(entry.commands).to eq "ls\ntest"
end
end
end
end
end end
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