From f4f6390ba6955d7735d0e31ba28f7646b2b6ab51 Mon Sep 17 00:00:00 2001 From: "Watanabe, Mitsutoshi" Date: Tue, 2 Feb 2016 16:18:06 +0900 Subject: [PATCH] Supported dynamic set variables when proc given. --- lib/whenever/job.rb | 1 + test/functional/output_set_proc_test.rb | 47 +++++++++++++++++++++++++ test/unit/job_test.rb | 28 +++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 test/functional/output_set_proc_test.rb diff --git a/lib/whenever/job.rb b/lib/whenever/job.rb index 1652c8af..1fb2b484 100644 --- a/lib/whenever/job.rb +++ b/lib/whenever/job.rb @@ -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) diff --git a/test/functional/output_set_proc_test.rb b/test/functional/output_set_proc_test.rb new file mode 100644 index 00000000..a8c68d65 --- /dev/null +++ b/test/functional/output_set_proc_test.rb @@ -0,0 +1,47 @@ +require 'test_helper' + +class OutputSetProcTest < Whenever::TestCase + + test "Variables which is dynamically set by proc" 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" + 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 diff --git a/test/unit/job_test.rb b/test/unit/job_test.rb index a9e34ce5..c7c5ce09 100644 --- a/test/unit/job_test.rb +++ b/test/unit/job_test.rb @@ -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 @@ -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' @@ -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