attempting to escape quotes

This commit is contained in:
Javan Makhmali 2010-07-02 17:39:17 -04:00
parent e12d3436d6
commit 505a0b4075
3 changed files with 56 additions and 38 deletions

View File

@ -15,15 +15,26 @@ module Whenever
def output
template = @options[:template].dup
unless @options[:escape_quotes] === false
template.sub!("':task'", %Q('#{@options[:task].gsub(/'/) { "'\''" }}'))
template.sub!('":task"', %Q("#{@options[:task].gsub(/"/) { '\"' }}"))
end
template.gsub(/:\w+/) do |key|
@options[key.sub(':', '').to_sym]
if key == ':task' && template.index("':task'")
escape_single_quotes(@options[:task])
elsif key == ':task' && template.index('":task"')
escape_double_quotes(@options[:task])
else
@options[key.sub(':', '').to_sym]
end
end
end
protected
def escape_single_quotes(str)
str.gsub(/'/, %q('\''))
end
def escape_double_quotes(str)
str.gsub(/"/, %q(\"))
end
end
end

38
test/job_test.rb Normal file
View File

@ -0,0 +1,38 @@
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
class JobTest < Test::Unit::TestCase
context "A Job" do
should "output the :task" do
job = new_job(:template => ":task", :task => 'abc123')
assert_equal %q(abc123), job.output
end
should "output the :task if it's in single quotes" do
job = new_job(:template => "':task'", :task => 'abc123')
assert_equal %q('abc123'), job.output
end
should "output the :task if it's in double quotes" do
job = new_job(:template => '":task"', :task => 'abc123')
assert_equal %q("abc123"), job.output
end
should "output escaped single quotes in :task when it's wrapped in them" do
job = new_job(:template => "outside ':task' outside", :task => "'inside'")
assert_equal %q(outside ''\''inside'\''' outside), job.output
end
should "output escaped double quotes in :task when it's wrapped in them" do
job = new_job(:template => 'outside ":task" outside', :task => '"inside"')
assert_equal %q(outside "\"inside"\" outside), job.output
end
end
private
def new_job(options)
Whenever::Job.new(options)
end
end

View File

@ -83,36 +83,5 @@ class OutputDefinedJobTest < Test::Unit::TestCase
assert_match /^.+ .+ .+ .+ before during after$/, @output
end
end
context "A defined job with a task in single quotes" do
setup do
@output = Whenever.cron \
<<-file
job_type :some_job, "happy ':task'"
every 2.hours do
some_job "first 'birthday'"
end
file
end
should "output the defined job with the single quotes escaped" do
assert_match two_hours + %Q( happy 'first '\''birthday'\''), @output
end
end
context "A defined job with a task in double quotes" do
setup do
@output = Whenever.cron \
<<-file
job_type :some_job, 'happy ":task"'
every 2.hours do
some_job 'first "birthday"'
end
file
end
should "output the defined job with the single quotes escaped" do
assert_match two_hours + ' happy "first \"birthday\""', @output
end
end
end