test/paperclip_test.rb in paperclip-2.3.3 vs test/paperclip_test.rb in paperclip-2.3.4

- old
+ new

@@ -1,120 +1,50 @@ require 'test/helper' class PaperclipTest < Test::Unit::TestCase - [:image_magick_path, :command_path].each do |path| - context "Calling Paperclip.run with #{path} specified" do - setup do - Paperclip.options[:image_magick_path] = nil - Paperclip.options[:command_path] = nil - Paperclip.options[path] = "/usr/bin" - end - - should "return the expected path for path_for_command" do - assert_equal "/usr/bin/convert", Paperclip.path_for_command("convert") - end - - should "execute the right command" do - Paperclip.expects(:path_for_command).with("convert").returns("/usr/bin/convert") - Paperclip.expects(:bit_bucket).returns("/dev/null") - Paperclip.expects(:"`").with("/usr/bin/convert 'one.jpg' 'two.jpg' 2>/dev/null") - Paperclip.run("convert", "one.jpg", "two.jpg") - end - end - end - - context "Calling Paperclip.run with no path specified" do + context "Calling Paperclip.run" do setup do Paperclip.options[:image_magick_path] = nil Paperclip.options[:command_path] = nil + Paperclip::CommandLine.stubs(:'`') end - should "return the expected path fro path_for_command" do - assert_equal "convert", Paperclip.path_for_command("convert") + should "execute the right command with :image_magick_path" do + Paperclip.options[:image_magick_path] = "/usr/bin" + Paperclip.expects(:log).with(includes('[DEPRECATION]')) + Paperclip.expects(:log).with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]})) + Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]})) + Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg") end - should "execute the right command" do - Paperclip.expects(:path_for_command).with("convert").returns("convert") - Paperclip.expects(:bit_bucket).returns("/dev/null") - Paperclip.expects(:"`").with("convert 'one.jpg' 'two.jpg' 2>/dev/null") - Paperclip.run("convert", "one.jpg", "two.jpg") + should "execute the right command with :command_path" do + Paperclip.options[:command_path] = "/usr/bin" + Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]})) + Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg") end - end - context "Calling Paperclip.run and logging" do - should "log the command when :log_command is true" do - Paperclip.options[:image_magick_path] = nil - Paperclip.options[:command_path] = nil - Paperclip.stubs(:bit_bucket).returns("/dev/null") - Paperclip.expects(:log).with("this 'is the command' 2>/dev/null") - Paperclip.expects(:"`").with("this 'is the command' 2>/dev/null") - Paperclip.options[:log_command] = true - Paperclip.run("this","is the command") + should "execute the right command with no path" do + Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{convert ['"]one.jpg['"] ['"]two.jpg['"]})) + Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg") end - should "not log the command when :log_command is false" do - Paperclip.options[:image_magick_path] = nil - Paperclip.options[:command_path] = nil - Paperclip.stubs(:bit_bucket).returns("/dev/null") - Paperclip.expects(:log).with("this 'is the command' 2>/dev/null").never - Paperclip.expects(:"`").with("this 'is the command' 2>/dev/null") - Paperclip.options[:log_command] = false - Paperclip.run("this","is the command") - end - end - - context "Calling Paperclip.run when the command is not found" do should "tell you the command isn't there if the shell returns 127" do - begin + with_exitstatus_returning(127) do assert_raises(Paperclip::CommandNotFoundError) do - `ruby -e 'exit 127'` # Stub $?.exitstatus to be 127, i.e. Command Not Found. - Paperclip.stubs(:"`").returns("") Paperclip.run("command") end - ensure - `ruby -e 'exit 0'` # Unstub $?.exitstatus end end + should "tell you the command isn't there if an ENOENT is raised" do assert_raises(Paperclip::CommandNotFoundError) do - Paperclip.stubs(:"`").raises(Errno::ENOENT) + Paperclip::CommandLine.stubs(:"`").raises(Errno::ENOENT) Paperclip.run("command") end end end - should "prevent dangerous characters in the command via quoting" do - Paperclip.options[:image_magick_path] = nil - Paperclip.options[:command_path] = nil - Paperclip.options[:log_command] = false - Paperclip.options[:swallow_stderr] = false - Paperclip.expects(:"`").with(%q[this 'is' 'jack'\''s' '`command`' 'line!']) - Paperclip.run("this", "is", "jack's", "`command`", "line!") - end - - context "Paperclip.bit_bucket" do - context "on systems without /dev/null" do - setup do - File.expects(:exists?).with("/dev/null").returns(false) - end - - should "return 'NUL'" do - assert_equal "NUL", Paperclip.bit_bucket - end - end - - context "on systems with /dev/null" do - setup do - File.expects(:exists?).with("/dev/null").returns(true) - end - - should "return '/dev/null'" do - assert_equal "/dev/null", Paperclip.bit_bucket - end - end - end - should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) } end should "raise when sent #processor and the name of a class that doesn't exist" do @@ -240,13 +170,20 @@ @dummy.expects(:foo).returns(true) assert @dummy.valid? end end + should "not have Attachment in the ActiveRecord::Base namespace" do + assert_raises(NameError) do + ActiveRecord::Base::Attachment + end + end + def self.should_validate validation, options, valid_file, invalid_file context "with #{validation} validation and #{options.inspect} options" do setup do + rebuild_class Dummy.send(:"validates_attachment_#{validation}", :avatar, options) @dummy = Dummy.new end context "and assigning nil" do setup do @@ -257,10 +194,10 @@ should "have an error on the attachment" do assert @dummy.errors[:avatar_file_name] end else should "not have an error on the attachment" do - assert @dummy.errors[:avatar_file_name].blank?, @dummy.errors.full_messages.join(", ") + assert @dummy.errors.blank?, @dummy.errors.full_messages.join(", ") end end end context "and assigned a valid file" do setup do