Permalink
Browse files

Merge pull request #200 from amiryal/master

Escape % characters and raise exception on newline
  • Loading branch information...
2 parents 0be4ed8 + 15339af commit b196f2c9c4e5e93fa7c94c3f613a405b35019e70 @javan javan committed Dec 19, 2011
Showing with 31 additions and 1 deletion.
  1. +5 −1 lib/whenever/job.rb
  2. +26 −0 test/unit/job_test.rb
View
@@ -14,7 +14,11 @@ def initialize(options = {})
def output
job = process_template(@template, @options).strip
- process_template(@job_template, { :job => job }).strip
+ out = process_template(@job_template, { :job => job }).strip
+ if out =~ /\n/
+ raise ArgumentError, "Task contains newline"
+ end
+ out.gsub(/%/, '\%')
end
protected
View
@@ -20,6 +20,32 @@ class JobTest < Test::Unit::TestCase
Whenever.expects(:path).returns('/my/path')
assert_equal '/my/path', new_job(:template => ':path').output
end
+
+ should "escape percent signs" do
+ job = new_job(
+ :template => "before :foo after",
+ :foo => "percent -> % <- percent"
+ )
+ assert_equal %q(before percent -> \% <- percent after), job.output
+ end
+
+ should "assume percent signs are not already escaped" do
+ job = new_job(
+ :template => "before :foo after",
+ :foo => %q(percent preceded by a backslash -> \% <-)
+ )
+ assert_equal %q(before percent preceded by a backslash -> \\\% <- after), job.output
+ end
+
+ should "reject newlines" do
+ job = new_job(
+ :template => "before :foo after",
+ :foo => "newline -> \n <- newline"
+ )
+ assert_raise ArgumentError do
+ job.output
+ end
+ end
end

0 comments on commit b196f2c

Please sign in to comment.