summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-07-13 14:38:10 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-07-13 14:38:10 +0200
commit6920390aad683dcc73109be5a23b647c918f9309 (patch)
tree898067082590de74b68ec4ee3125f5cb1a7fd477
parent097550f08a2a92dd1efff5da97cff0228afde57b (diff)
downloadgitlab-ce-6920390aad683dcc73109be5a23b647c918f9309.tar.gz
Add before script and commands to CI job entry
-rw-r--r--lib/gitlab/ci/config/node/job.rb15
-rw-r--r--spec/lib/gitlab/ci/config/node/job_spec.rb117
2 files changed, 124 insertions, 8 deletions
diff --git a/lib/gitlab/ci/config/node/job.rb b/lib/gitlab/ci/config/node/job.rb
index 9a019216ca3..5ee91ebcf0b 100644
--- a/lib/gitlab/ci/config/node/job.rb
+++ b/lib/gitlab/ci/config/node/job.rb
@@ -10,6 +10,7 @@ module Gitlab
validations do
validates :config, presence: true
+ validates :global, required: true, on: :processed
end
node :before_script, Script,
@@ -30,8 +31,6 @@ module Gitlab
helpers :before_script, :script, :stage, :type, :after_script
def value
- raise InvalidError unless valid?
-
##
# TODO, refactoring step: do not expose internal configuration,
# return only hash value without merging it to internal config.
@@ -39,6 +38,18 @@ module Gitlab
@config.merge(to_hash.compact)
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
def to_hash
diff --git a/spec/lib/gitlab/ci/config/node/job_spec.rb b/spec/lib/gitlab/ci/config/node/job_spec.rb
index 77efc73632d..635362611a0 100644
--- a/spec/lib/gitlab/ci/config/node/job_spec.rb
+++ b/spec/lib/gitlab/ci/config/node/job_spec.rb
@@ -60,14 +60,62 @@ describe Gitlab::Ci::Config::Node::Job do
after_script: %w[cleanup])
end
end
+ end
+
+ describe '#before_script' 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 entry is incorrect' do
- let(:config) { {} }
+ context 'when before script is overridden' do
+ let(:config) do
+ { before_script: %w[whoami],
+ script: 'rspec' }
+ end
- it 'raises error' do
- expect { entry.value }.to raise_error(
- Gitlab::Ci::Config::Node::Entry::InvalidError
- )
+ 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 'returns correct script' do
+ expect(entry.before_script).to eq %w[ls pwd]
+ 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
@@ -77,4 +125,61 @@ describe Gitlab::Ci::Config::Node::Job do
expect(entry).to be_relevant
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