Skip to content
This repository
Browse code

Merge pull request #286 from elhu/master

Fix #229 - Don't interpolate attributes with no corresponding value
  • Loading branch information...
commit 6dba273cf9ee9bdf83fe8afa1f640e76bf7e4d0f 2 parents f9e62e9 + e19beec
Javan Makhmali authored
4 lib/whenever/job.rb
@@ -10,7 +10,7 @@ def initialize(options = {})
10 10 @template = options.delete(:template)
11 11 @job_template = options.delete(:job_template) || ":job"
12 12 @roles = Array.wrap(options.delete(:roles))
13   - @options[:output] = Whenever::Output::Redirection.new(options[:output]).to_s if options.has_key?(:output)
  13 + @options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output]).to_s : ''
14 14 @options[:environment] ||= :production
15 15 @options[:path] = Shellwords.shellescape(@options[:path] || Whenever.path)
16 16 end
@@ -33,7 +33,7 @@ def has_role?(role)
33 33 def process_template(template, options)
34 34 template.gsub(/:\w+/) do |key|
35 35 before_and_after = [$`[-1..-1], $'[0..0]]
36   - option = options[key.sub(':', '').to_sym]
  36 + option = options[key.sub(':', '').to_sym] || key
37 37
38 38 if before_and_after.all? { |c| c == "'" }
39 39 escape_single_quotes(option)
32 test/functional/output_defined_job_test.rb
... ... @@ -1,7 +1,7 @@
1 1 require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2 2
3 3 class OutputDefinedJobTest < Test::Unit::TestCase
4   -
  4 +
5 5 context "A defined job with a :task" do
6 6 setup do
7 7 @output = Whenever.cron \
@@ -13,12 +13,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
13 13 end
14 14 file
15 15 end
16   -
  16 +
17 17 should "output the defined job with the task" do
18 18 assert_match /^.+ .+ .+ .+ before during after$/, @output
19 19 end
20 20 end
21   -
  21 +
22 22 context "A defined job with a :task and some options" do
23 23 setup do
24 24 @output = Whenever.cron \
@@ -30,12 +30,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
30 30 end
31 31 file
32 32 end
33   -
  33 +
34 34 should "output the defined job with the task and options" do
35 35 assert_match /^.+ .+ .+ .+ before during after happy birthday$/, @output
36 36 end
37 37 end
38   -
  38 +
39 39 context "A defined job with a :task and an option where the option is set globally" do
40 40 setup do
41 41 @output = Whenever.cron \
@@ -48,12 +48,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
48 48 end
49 49 file
50 50 end
51   -
  51 +
52 52 should "output the defined job with the task and options" do
53 53 assert_match /^.+ .+ .+ .+ before during after happy$/, @output
54 54 end
55 55 end
56   -
  56 +
57 57 context "A defined job with a :task and an option where the option is set globally and locally" do
58 58 setup do
59 59 @output = Whenever.cron \
@@ -66,12 +66,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
66 66 end
67 67 file
68 68 end
69   -
  69 +
70 70 should "output the defined job using the local option" do
71 71 assert_match /^.+ .+ .+ .+ before during after local$/, @output
72 72 end
73 73 end
74   -
  74 +
75 75 context "A defined job with a :task and an option that is not set" do
76 76 setup do
77 77 @output = Whenever.cron \
@@ -83,16 +83,16 @@ class OutputDefinedJobTest < Test::Unit::TestCase
83 83 end
84 84 file
85 85 end
86   -
87   - should "output the defined job with that option removed" do
88   - assert_match /^.+ .+ .+ .+ before during after$/, @output
  86 +
  87 + should "output the defined job with that option left untouched" do
  88 + assert_match /^.+ .+ .+ .+ before during after :option1$/, @output
89 89 end
90 90 end
91   -
  91 +
92 92 context "A defined job that uses a :path where none is explicitly set" do
93 93 setup do
94 94 Whenever.stubs(:path).returns('/my/path')
95   -
  95 +
96 96 @output = Whenever.cron \
97 97 <<-file
98 98 set :job_template, nil
@@ -102,10 +102,10 @@ class OutputDefinedJobTest < Test::Unit::TestCase
102 102 end
103 103 file
104 104 end
105   -
  105 +
106 106 should "default to using the Whenever.path" do
107 107 assert_match two_hours + %( cd /my/path && blahblah), @output
108 108 end
109 109 end
110 110
111   -end
  111 +end
4 test/unit/job_test.rb
@@ -30,6 +30,10 @@ class JobTest < Test::Unit::TestCase
30 30 assert_equal '/my/path', new_job(:template => ':path').output
31 31 end
32 32
  33 + should "not substitute parameters for which no value is set" do
  34 + assert_equal 'Hello :world', new_job(:template => ':matching :world', :matching => 'Hello').output
  35 + end
  36 +
33 37 should "escape the :path" do
34 38 assert_equal '/my/spacey\ path', new_job(:template => ':path', :path => '/my/spacey path').output
35 39 end

0 comments on commit 6dba273

Please sign in to comment.
Something went wrong with that request. Please try again.