Skip to content

Commit

Permalink
Override global option with group option
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
njakobsen committed Dec 21, 2021
1 parent e916b58 commit 6e62bb9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/whenever/job_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/functional/output_defined_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6e62bb9

Please sign in to comment.