From 6e62bb9bee465a98e0f64b7c21039ae12df40760 Mon Sep 17 00:00:00 2001 From: Nicholas Jakobsen Date: Tue, 21 Dec 2021 15:42:43 -0800 Subject: [PATCH] Override global option with group option Local options override globally set options, but options set on the `every` group do not. It makes sense for the options defined closer to the actual job to override those set further away, so we change the merge order of options to allow options passed to `every` to override those set globally with `set`. We still allow local options set on each job to override those set on the group. --- lib/whenever/job_list.rb | 2 +- test/functional/output_defined_job_test.rb | 28 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/whenever/job_list.rb b/lib/whenever/job_list.rb index 7df39c68..da88cce8 100644 --- a/lib/whenever/job_list.rb +++ b/lib/whenever/job_list.rb @@ -66,7 +66,7 @@ def job_type(name, template) @jobs[options.fetch(:mailto)] ||= {} @jobs[options.fetch(:mailto)][@current_time_scope] ||= [] - @jobs[options.fetch(:mailto)][@current_time_scope] << Whenever::Job.new(@options.merge(@set_variables).merge(options)) + @jobs[options.fetch(:mailto)][@current_time_scope] << Whenever::Job.new(@set_variables.merge(@options).merge(options)) end end end diff --git a/test/functional/output_defined_job_test.rb b/test/functional/output_defined_job_test.rb index 9f163f66..6fcacf4b 100644 --- a/test/functional/output_defined_job_test.rb +++ b/test/functional/output_defined_job_test.rb @@ -55,6 +55,34 @@ class OutputDefinedJobTest < Whenever::TestCase assert_match(/^.+ .+ .+ .+ before during after local$/, output) end + test "defined job with a :task and an option where the option is set globally and on the group" do + output = Whenever.cron \ + <<-file + set :job_template, nil + job_type :some_job, "before :task after :option1" + set :option1, 'global' + every 2.hours, :option1 => 'group' do + some_job "during" + end + file + + assert_match(/^.+ .+ .+ .+ before during after group$/, output) + end + + test "defined job with a :task and an option where the option is set globally, on the group, and locally" do + output = Whenever.cron \ + <<-file + set :job_template, nil + job_type :some_job, "before :task after :option1" + set :option1, 'global' + every 2.hours, :option1 => 'group' do + some_job "during", :option1 => 'local' + end + file + + assert_match(/^.+ .+ .+ .+ before during after local$/, output) + end + test "defined job with a :task and an option that is not set" do output = Whenever.cron \ <<-file