From 505a0b4075c5eef136d96c80afa164d41fd72468 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Fri, 2 Jul 2010 17:39:17 -0400 Subject: [PATCH] attempting to escape quotes --- lib/whenever/job.rb | 23 ++++++++++++++------ test/job_test.rb | 38 +++++++++++++++++++++++++++++++++ test/output_defined_job_test.rb | 33 +--------------------------- 3 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 test/job_test.rb diff --git a/lib/whenever/job.rb b/lib/whenever/job.rb index e49eccc..6f095d6 100644 --- a/lib/whenever/job.rb +++ b/lib/whenever/job.rb @@ -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 \ No newline at end of file diff --git a/test/job_test.rb b/test/job_test.rb new file mode 100644 index 0000000..8cda6af --- /dev/null +++ b/test/job_test.rb @@ -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 \ No newline at end of file diff --git a/test/output_defined_job_test.rb b/test/output_defined_job_test.rb index ddaa220..a135054 100644 --- a/test/output_defined_job_test.rb +++ b/test/output_defined_job_test.rb @@ -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 \ No newline at end of file