Skip to content

Commit

Permalink
Escape percent signs and reject newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
amiryal committed Dec 19, 2011
1 parent 0be4ed8 commit 15339af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/whenever/job.rb
Expand Up @@ -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(/%/, '\%')

This comment has been minimized.

Copy link
@tithonium

tithonium May 3, 2012

Out of curiosity, why do you want to escape percent signs? This mucks up using date formats in the output specifier, for simple log rotation.

This comment has been minimized.

Copy link
@amiryal

amiryal May 4, 2012

Author Contributor

Have you read the original pull request? #200

Percent signs need to be escaped, according to man 5 crontab:

end

protected
Expand Down
26 changes: 26 additions & 0 deletions test/unit/job_test.rb
Expand Up @@ -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


Expand Down

0 comments on commit 15339af

Please sign in to comment.