Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supported dynamic set variables when proc given. #607

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/whenever/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def process_template(template, options)
template.gsub(/:\w+/) do |key|
before_and_after = [$`[-1..-1], $'[0..0]]
option = options[key.sub(':', '').to_sym] || key
option = instance_eval(&option) if option.respond_to? :call

if before_and_after.all? { |c| c == "'" }
escape_single_quotes(option)
Expand Down
47 changes: 47 additions & 0 deletions test/functional/output_set_proc_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'test_helper'

class OutputSetProcTest < Whenever::TestCase

test "Variables which is dynamically set by proc" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: Variables which is => Variables which are

output = Whenever.cron \
<<-file
set :lock_file_name , proc { @options[:task] }
set :job_template, "/bin/bash -l -c 'cd :path && setlock -n /var/run/:lock_file_name :job'"
every 2.hours do
set :path, "/tmp"
command "blahblah"
end
file

assert_match /setlock -n \/var\/run\/blahblah/, output
end

test "Variables which is dynamically set by lambda" do
output = Whenever.cron \
<<-file
set :lock_file_name , lambda { |x| @options[:task] }
set :job_template, "/bin/bash -l -c 'cd :path && setlock -n /var/run/:lock_file_name :job'"
every 2.hours do
set :path, "/tmp"
command "blahblah"
end
file

assert_match /setlock -n \/var\/run\/blahblah/, output
end

test "Variables which is dynamically set by proc can be overwritten by command set" do
output = Whenever.cron \
<<-file
set :lock_file_name , proc { @options[:task] }
set :job_template, "/bin/bash -l -c 'cd :path && setlock -n /var/run/:lock_file_name :job'"
every 2.hours do
set :path, "/tmp"
command "blahblah", :lock_file_name => "custom_lock"
end
file

assert_match /setlock -n \/var\/run\/custom_lock/, output
end

end
28 changes: 28 additions & 0 deletions test/unit/job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ class JobTest < Whenever::TestCase

assert_equal "before newline -> <- newline space -> <- space after", job.output
end

should "eval when proc/lambda is passed" do
job = new_job(
:template => 'before :dynamic_foo after',
:foo => "percent -> % <- percent",
:dynamic_foo => proc { @options[:foo] }
)
assert_equal %q(before percent -> \% <- percent after), job.output
end
end


Expand All @@ -83,6 +92,15 @@ class JobWithQuotesTest < Whenever::TestCase
end

should "output escaped double quotes when it's wrapped in them" do
job = new_job(
:template => 'before ":dynamic_foo" after',
:foo => 'quote -> " <- quote',
:dynamic_foo => proc { @options[:foo] }
)
assert_equal %q(before "quote -> \" <- quote" after), job.output
end

should "eval when proc/lambda is passed" do
job = new_job(
:template => 'before ":foo" after',
:foo => 'quote -> " <- quote'
Expand Down Expand Up @@ -111,4 +129,14 @@ class JobWithJobTemplateTest < Whenever::TestCase
job = new_job(:template => 'before ":task" after', :task => 'quote -> " <- quote', :job_template => 'left ":job" right')
assert_equal %q(left "before \"quote -> \\\" <- quote\" after" right), job.output
end

should "eval when proc/lambda is passed" do
job = new_job(
:template => 'before ":dynamic_task" after',
:task => 'quote -> " <- quote',
:job_template => 'left ":job" right',
:dynamic_task => proc { @options[:task] }
)
assert_equal %q(left "before \"quote -> \\\" <- quote\" after" right), job.output
end
end