Navigation Menu

Skip to content

Commit

Permalink
escape paths
Browse files Browse the repository at this point in the history
  • Loading branch information
javan committed Dec 20, 2011
1 parent b196f2c commit edac3ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
14 changes: 8 additions & 6 deletions lib/whenever/job.rb
@@ -1,17 +1,19 @@
require 'shellwords'

module Whenever
class Job
attr_reader :at

def initialize(options = {})
@options = options
@at = options.delete(:at)
@template = options.delete(:template)
@job_template = options.delete(:job_template) || ":job"
@options[:output] = Whenever::Output::Redirection.new(options[:output]).to_s if options.has_key?(:output)
@options[:environment] ||= :production
@options[:path] ||= Whenever.path
@options[:path] = Shellwords.shellescape(@options[:path] || Whenever.path)
end

def output
job = process_template(@template, @options).strip
out = process_template(@job_template, { :job => job }).strip
Expand All @@ -20,9 +22,9 @@ def output
end
out.gsub(/%/, '\%')
end

protected

def process_template(template, options)
template.gsub(/:\w+/) do |key|
before_and_after = [$`[-1..-1], $'[0..0]]
Expand All @@ -41,7 +43,7 @@ def process_template(template, options)
def escape_single_quotes(str)
str.gsub(/'/) { "'\\''" }
end

def escape_double_quotes(str)
str.gsub(/"/) { '\"' }
end
Expand Down
34 changes: 19 additions & 15 deletions test/unit/job_test.rb
@@ -1,42 +1,46 @@
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")

class JobTest < Test::Unit::TestCase

context "A Job" do
should "return the :at set when #at is called" do
assert_equal 'foo', new_job(:at => 'foo').at
end

should "substitute the :task when #output is called" do
job = new_job(:template => ":task", :task => 'abc123')
assert_equal 'abc123', job.output
end

should "substitute the :path when #output is called" do
assert_equal 'foo', new_job(:template => ':path', :path => 'foo').output
end

should "substitute the :path with the default Whenever.path if none is provided when #output is called" do
Whenever.expects(:path).returns('/my/path')
assert_equal '/my/path', new_job(:template => ':path').output
end


should "escape the :path" do
assert_equal '/my/spacey\ path', new_job(:template => ':path', :path => '/my/spacey 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",
Expand All @@ -48,26 +52,26 @@ class JobTest < Test::Unit::TestCase
end
end


context "A Job with quotes" do
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 when it's wrapped in them" do
job = new_job(
:template => "before ':foo' after",
:foo => "quote -> ' <- quote"
)
assert_equal %q(before 'quote -> '\'' <- quote' after), job.output
end

should "output escaped double quotes when it's wrapped in them" do
job = new_job(
:template => 'before ":foo" after',
Expand All @@ -76,18 +80,18 @@ class JobTest < Test::Unit::TestCase
assert_equal %q(before "quote -> \" <- quote" after), job.output
end
end

context "A Job with a job_template" do
should "use the job template" do
job = new_job(:template => ':task', :task => 'abc123', :job_template => 'left :job right')
assert_equal 'left abc123 right', job.output
end

should "escape single quotes" do
job = new_job(:template => "before ':task' after", :task => "quote -> ' <- quote", :job_template => "left ':job' right")
assert_equal %q(left 'before '\''quote -> '\\''\\'\\'''\\'' <- quote'\'' after' right), job.output
end

should "escape double quotes" do
job = new_job(:template => 'before ":task" after', :task => 'quote -> " <- quote', :job_template => 'left ":job" right')
assert_equal %q(left "before \"quote -> \\\" <- quote\" after" right), job.output
Expand All @@ -99,5 +103,5 @@ class JobTest < Test::Unit::TestCase
def new_job(options={})
Whenever::Job.new(options)
end

end

0 comments on commit edac3ab

Please sign in to comment.