Skip to content
Browse files

Merge pull request #286 from elhu/master

Fix #229 - Don't interpolate attributes with no corresponding value
  • Loading branch information...
2 parents f9e62e9 + e19beec commit 6dba273cf9ee9bdf83fe8afa1f640e76bf7e4d0f @javan committed
Showing with 22 additions and 18 deletions.
  1. +2 −2 lib/whenever/job.rb
  2. +16 −16 test/functional/output_defined_job_test.rb
  3. +4 −0 test/unit/job_test.rb
View
4 lib/whenever/job.rb
@@ -10,7 +10,7 @@ def initialize(options = {})
@template = options.delete(:template)
@job_template = options.delete(:job_template) || ":job"
@roles = Array.wrap(options.delete(:roles))
- @options[:output] = Whenever::Output::Redirection.new(options[:output]).to_s if options.has_key?(:output)
+ @options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output]).to_s : ''
@options[:environment] ||= :production
@options[:path] = Shellwords.shellescape(@options[:path] || Whenever.path)
end
@@ -33,7 +33,7 @@ def has_role?(role)
def process_template(template, options)
template.gsub(/:\w+/) do |key|
before_and_after = [$`[-1..-1], $'[0..0]]
- option = options[key.sub(':', '').to_sym]
+ option = options[key.sub(':', '').to_sym] || key
if before_and_after.all? { |c| c == "'" }
escape_single_quotes(option)
View
32 test/functional/output_defined_job_test.rb
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
class OutputDefinedJobTest < Test::Unit::TestCase
-
+
context "A defined job with a :task" do
setup do
@output = Whenever.cron \
@@ -13,12 +13,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
end
file
end
-
+
should "output the defined job with the task" do
assert_match /^.+ .+ .+ .+ before during after$/, @output
end
end
-
+
context "A defined job with a :task and some options" do
setup do
@output = Whenever.cron \
@@ -30,12 +30,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
end
file
end
-
+
should "output the defined job with the task and options" do
assert_match /^.+ .+ .+ .+ before during after happy birthday$/, @output
end
end
-
+
context "A defined job with a :task and an option where the option is set globally" do
setup do
@output = Whenever.cron \
@@ -48,12 +48,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
end
file
end
-
+
should "output the defined job with the task and options" do
assert_match /^.+ .+ .+ .+ before during after happy$/, @output
end
end
-
+
context "A defined job with a :task and an option where the option is set globally and locally" do
setup do
@output = Whenever.cron \
@@ -66,12 +66,12 @@ class OutputDefinedJobTest < Test::Unit::TestCase
end
file
end
-
+
should "output the defined job using the local option" do
assert_match /^.+ .+ .+ .+ before during after local$/, @output
end
end
-
+
context "A defined job with a :task and an option that is not set" do
setup do
@output = Whenever.cron \
@@ -83,16 +83,16 @@ class OutputDefinedJobTest < Test::Unit::TestCase
end
file
end
-
- should "output the defined job with that option removed" do
- assert_match /^.+ .+ .+ .+ before during after$/, @output
+
+ should "output the defined job with that option left untouched" do
+ assert_match /^.+ .+ .+ .+ before during after :option1$/, @output
end
end
-
+
context "A defined job that uses a :path where none is explicitly set" do
setup do
Whenever.stubs(:path).returns('/my/path')
-
+
@output = Whenever.cron \
<<-file
set :job_template, nil
@@ -102,10 +102,10 @@ class OutputDefinedJobTest < Test::Unit::TestCase
end
file
end
-
+
should "default to using the Whenever.path" do
assert_match two_hours + %( cd /my/path && blahblah), @output
end
end
-end
+end
View
4 test/unit/job_test.rb
@@ -30,6 +30,10 @@ class JobTest < Test::Unit::TestCase
assert_equal '/my/path', new_job(:template => ':path').output
end
+ should "not substitute parameters for which no value is set" do
+ assert_equal 'Hello :world', new_job(:template => ':matching :world', :matching => 'Hello').output
+ end
+
should "escape the :path" do
assert_equal '/my/spacey\ path', new_job(:template => ':path', :path => '/my/spacey path').output
end

0 comments on commit 6dba273

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