diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-04-16 19:49:15 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-04-16 20:22:49 +0300 |
commit | 4cc9a02ee033564b62b45977571319d129df465b (patch) | |
tree | 72073bed6119fd3b411c0c08d3b459d6620caf6c | |
parent | b340b59743e8cd47fc1f4fa2020b400d82bfd86e (diff) |
Allow the before_script and finally_script to be overwritten in context of job
-rw-r--r-- | lib/ci/gitlab_ci_yaml_processor.rb | 22 | ||||
-rw-r--r-- | spec/lib/ci/gitlab_ci_yaml_processor_spec.rb | 73 |
2 files changed, 81 insertions, 14 deletions
diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index be2462949f1..2e5b84a57d6 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -7,7 +7,7 @@ module Ci ALLOWED_YAML_KEYS = [:before_script, :finally_script, :image, :services, :types, :stages, :variables, :cache] ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services, :allow_failure, :type, :stage, :when, :artifacts, :cache, - :dependencies] + :dependencies, :before_script, :finally_script] attr_reader :before_script, :finally_script, :image, :services, :variables, :path, :cache @@ -73,7 +73,7 @@ module Ci { stage_idx: stages.index(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] || [], name: name, only: job[:only], @@ -86,19 +86,11 @@ module Ci artifacts: job[:artifacts], cache: job[:cache] || @cache, dependencies: job[:dependencies], - finally_script: @finally_script, + finally_script: job[:finally_script] || @finally_script, }.compact } end - def normalize_script(script) - if script.is_a? Array - script.join("\n") - else - script - end - end - def validate! unless validate_array_of_strings(@before_script) raise ValidationError, "before_script should be an array of strings" @@ -177,6 +169,14 @@ module Ci raise ValidationError, "#{name} job: script should be a string or an array of a strings" 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]) raise ValidationError, "#{name} job: image should be a string" end diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb index 8e373ae55b0..a6a1a5e3bef 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -293,6 +293,46 @@ module Ci 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 context "in global context" do let(:config) { @@ -306,6 +346,19 @@ module Ci expect(subject[:options][:finally_script]).to eq(["finally_script"]) 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 @@ -558,7 +611,7 @@ module Ci stage_idx: 1, name: :normal_job, only: nil, - commands: "\ntest", + commands: "test", tag_list: [], options: {}, when: "on_success", @@ -585,7 +638,7 @@ EOT stage_idx: 1, name: :job1, only: nil, - commands: "\nexecute-script-for-job", + commands: "execute-script-for-job", tag_list: [], options: {}, when: "on_success", @@ -597,7 +650,7 @@ EOT stage_idx: 1, name: :job2, only: nil, - commands: "\nexecute-script-for-job", + commands: "execute-script-for-job", tag_list: [], options: {}, when: "on_success", @@ -629,6 +682,13 @@ EOT end.to raise_error(GitlabCiYamlProcessor::ValidationError, "before_script should be an array of strings") 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 config = YAML.dump({ finally_script: "bundle update", rspec: { script: "test" } }) expect do @@ -636,6 +696,13 @@ EOT end.to raise_error(GitlabCiYamlProcessor::ValidationError, "finally_script should be an array of strings") 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 config = YAML.dump({ image: ["test"], rspec: { script: "test" } }) expect do |