BigW Consortium Gitlab

Commit 4cc9a02e by Kamil Trzcinski

Allow the before_script and finally_script to be overwritten in context of job

parent b340b597
...@@ -7,7 +7,7 @@ module Ci ...@@ -7,7 +7,7 @@ module Ci
ALLOWED_YAML_KEYS = [:before_script, :finally_script, :image, :services, :types, :stages, :variables, :cache] ALLOWED_YAML_KEYS = [:before_script, :finally_script, :image, :services, :types, :stages, :variables, :cache]
ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services, ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services,
:allow_failure, :type, :stage, :when, :artifacts, :cache, :allow_failure, :type, :stage, :when, :artifacts, :cache,
:dependencies] :dependencies, :before_script, :finally_script]
attr_reader :before_script, :finally_script, :image, :services, :variables, :path, :cache attr_reader :before_script, :finally_script, :image, :services, :variables, :path, :cache
...@@ -73,7 +73,7 @@ module Ci ...@@ -73,7 +73,7 @@ module Ci
{ {
stage_idx: stages.index(job[:stage]), stage_idx: stages.index(job[:stage]),
stage: job[:stage], stage: job[:stage],
commands: "#{@before_script.join("\n")}\n#{normalize_script(job[:script])}", commands: [job[:before_script] || @before_script, job[:script]].flatten.join("\n"),
tag_list: job[:tags] || [], tag_list: job[:tags] || [],
name: name, name: name,
only: job[:only], only: job[:only],
...@@ -86,19 +86,11 @@ module Ci ...@@ -86,19 +86,11 @@ module Ci
artifacts: job[:artifacts], artifacts: job[:artifacts],
cache: job[:cache] || @cache, cache: job[:cache] || @cache,
dependencies: job[:dependencies], dependencies: job[:dependencies],
finally_script: @finally_script, finally_script: job[:finally_script] || @finally_script,
}.compact }.compact
} }
end end
def normalize_script(script)
if script.is_a? Array
script.join("\n")
else
script
end
end
def validate! def validate!
unless validate_array_of_strings(@before_script) unless validate_array_of_strings(@before_script)
raise ValidationError, "before_script should be an array of strings" raise ValidationError, "before_script should be an array of strings"
...@@ -177,6 +169,14 @@ module Ci ...@@ -177,6 +169,14 @@ module Ci
raise ValidationError, "#{name} job: script should be a string or an array of a strings" raise ValidationError, "#{name} job: script should be a string or an array of a strings"
end end
if job[:before_script] && !validate_array_of_strings(job[:before_script])
raise ValidationError, "#{name} job: before_script should be an array of strings"
end
if job[:finally_script] && !validate_array_of_strings(job[:finally_script])
raise ValidationError, "#{name} job: finally_script should be an array of strings"
end
if job[:image] && !validate_string(job[:image]) if job[:image] && !validate_string(job[:image])
raise ValidationError, "#{name} job: image should be a string" raise ValidationError, "#{name} job: image should be a string"
end end
......
...@@ -293,6 +293,46 @@ module Ci ...@@ -293,6 +293,46 @@ module Ci
subject { config_processor.builds_for_stage_and_ref("test", "master").first } subject { config_processor.builds_for_stage_and_ref("test", "master").first }
describe "before_script" do
context "in global context" do
let(:config) {
{
before_script: ["global script"],
test: { script: ["script"] }
}
}
it "return commands with scripts concencaced" do
expect(subject[:commands]).to eq("global script\nscript")
end
end
context "overwritten in local context" do
let(:config) {
{
before_script: ["global script"],
test: { before_script: ["local script"], script: ["script"] }
}
}
it "return commands with scripts concencaced" do
expect(subject[:commands]).to eq("local script\nscript")
end
end
end
describe "script" do
let(:config) {
{
test: { script: ["script"] }
}
}
it "return commands with scripts concencaced" do
expect(subject[:commands]).to eq("script")
end
end
describe "finally_script" do describe "finally_script" do
context "in global context" do context "in global context" do
let(:config) { let(:config) {
...@@ -306,6 +346,19 @@ module Ci ...@@ -306,6 +346,19 @@ module Ci
expect(subject[:options][:finally_script]).to eq(["finally_script"]) expect(subject[:options][:finally_script]).to eq(["finally_script"])
end end
end end
context "overwritten in local context" do
let(:config) {
{
finally_script: ["local finally_script"],
test: { finally_script: ["local finally_script"], script: ["script"] }
}
}
it "return finally_script in options" do
expect(subject[:options][:finally_script]).to eq(["local finally_script"])
end
end
end end
end end
...@@ -558,7 +611,7 @@ module Ci ...@@ -558,7 +611,7 @@ module Ci
stage_idx: 1, stage_idx: 1,
name: :normal_job, name: :normal_job,
only: nil, only: nil,
commands: "\ntest", commands: "test",
tag_list: [], tag_list: [],
options: {}, options: {},
when: "on_success", when: "on_success",
...@@ -585,7 +638,7 @@ EOT ...@@ -585,7 +638,7 @@ EOT
stage_idx: 1, stage_idx: 1,
name: :job1, name: :job1,
only: nil, only: nil,
commands: "\nexecute-script-for-job", commands: "execute-script-for-job",
tag_list: [], tag_list: [],
options: {}, options: {},
when: "on_success", when: "on_success",
...@@ -597,7 +650,7 @@ EOT ...@@ -597,7 +650,7 @@ EOT
stage_idx: 1, stage_idx: 1,
name: :job2, name: :job2,
only: nil, only: nil,
commands: "\nexecute-script-for-job", commands: "execute-script-for-job",
tag_list: [], tag_list: [],
options: {}, options: {},
when: "on_success", when: "on_success",
...@@ -629,6 +682,13 @@ EOT ...@@ -629,6 +682,13 @@ EOT
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script should be an array of strings") end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script should be an array of strings")
end end
it "returns errors if job before_script parameter is not an array of strings" do
config = YAML.dump({ rspec: { script: "test", before_script: [10, "test"] } })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: before_script should be an array of strings")
end
it "returns errors if finally_script parameter is invalid" do it "returns errors if finally_script parameter is invalid" do
config = YAML.dump({ finally_script: "bundle update", rspec: { script: "test" } }) config = YAML.dump({ finally_script: "bundle update", rspec: { script: "test" } })
expect do expect do
...@@ -636,6 +696,13 @@ EOT ...@@ -636,6 +696,13 @@ EOT
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "finally_script should be an array of strings") end.to raise_error(GitlabCiYamlProcessor::ValidationError, "finally_script should be an array of strings")
end end
it "returns errors if job finally_script parameter is not an array of strings" do
config = YAML.dump({ rspec: { script: "test", finally_script: [10, "test"] } })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: finally_script should be an array of strings")
end
it "returns errors if image parameter is invalid" do it "returns errors if image parameter is invalid" do
config = YAML.dump({ image: ["test"], rspec: { script: "test" } }) config = YAML.dump({ image: ["test"], rspec: { script: "test" } })
expect do expect do
......
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