Permalink
Browse files

Don't use single-quote to quote windows command lines.

  • Loading branch information...
1 parent ba69750 commit 00c0daf42be72c80602aac13c976f74ae1f19160 @jyurek jyurek committed Aug 6, 2010
Showing with 39 additions and 3 deletions.
  1. +10 −3 lib/paperclip/command_line.rb
  2. +29 −0 test/command_line_test.rb
@@ -62,12 +62,19 @@ def invalid_variables
def shell_quote(string)
return "" if string.nil? or string.blank?
- string.split("'").map{|m| "'#{m}'" }.join("\\'")
+ if self.class.unix?
+ string.split("'").map{|m| "'#{m}'" }.join("\\'")
+ else
+ %{"#{string}"}
+ end
end
def bit_bucket
- return "2>NUL" unless File.exist?("/dev/null")
- "2>/dev/null"
+ self.class.unix? ? "2>/dev/null" : "2>NUL"
+ end
+
+ def self.unix?
+ File.exist?("/dev/null")
end
end
end
View
@@ -3,6 +3,7 @@
class CommandLineTest < Test::Unit::TestCase
def setup
Paperclip::CommandLine.path = nil
+ File.stubs(:exist?).with("/dev/null").returns(true)
end
should "take a command and parameters and produce a shell command for bash" do
@@ -24,6 +25,15 @@ def setup
assert_equal "convert 'a.jpg' 'b.png'", cmd.command
end
+ should "quote command line options differently if we're on windows" do
+ File.stubs(:exist?).with("/dev/null").returns(false)
+ cmd = Paperclip::CommandLine.new("convert",
+ ":one :{two}",
+ :one => "a.jpg",
+ :two => "b.png")
+ assert_equal 'convert "a.jpg" "b.png"', cmd.command
+ end
+
should "be able to quote and interpolate dangerous variables" do
cmd = Paperclip::CommandLine.new("convert",
":one :two",
@@ -32,6 +42,15 @@ def setup
assert_equal "convert '`rm -rf`.jpg' 'ha'\\''ha.png'", cmd.command
end
+ should "be able to quote and interpolate dangerous variables even on windows" do
+ File.stubs(:exist?).with("/dev/null").returns(false)
+ cmd = Paperclip::CommandLine.new("convert",
+ ":one :two",
+ :one => "`rm -rf`.jpg",
+ :two => "ha'ha.png")
+ assert_equal %{convert "`rm -rf`.jpg" "ha'ha.png"}, cmd.command
+ end
+
should "add redirection to get rid of stderr in bash" do
File.stubs(:exist?).with("/dev/null").returns(true)
cmd = Paperclip::CommandLine.new("convert",
@@ -96,4 +115,14 @@ def setup
Paperclip.expects(:log).with("convert a.jpg b.png")
cmd.run
end
+
+ should "detect that the system is unix or windows based on presence of /dev/null" do
+ File.stubs(:exist?).returns(true)
+ assert Paperclip::CommandLine.unix?
+ end
+
+ should "detect that the system is not unix or windows based on absence of /dev/null" do
+ File.stubs(:exist?).returns(false)
+ assert ! Paperclip::CommandLine.unix?
+ end
end

0 comments on commit 00c0daf

Please sign in to comment.