test/paperclip_test.rb in paperclip-2.3.0 vs test/paperclip_test.rb in paperclip-2.3.1

- old
+ new

@@ -1,16 +1,20 @@ require 'test/helper' class PaperclipTest < Test::Unit::TestCase - [:image_magick_path, :convert_path].each do |path| - context "Calling Paperclip.run with an #{path} specified" do + [: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[:convert_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") @@ -19,53 +23,57 @@ end context "Calling Paperclip.run with no path specified" do setup do Paperclip.options[:image_magick_path] = nil - Paperclip.options[:convert_path] = nil + Paperclip.options[:command_path] = nil end + should "return the expected path fro path_for_command" do + assert_equal "convert", Paperclip.path_for_command("convert") + 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") end + end - should "log the command when :log_command is set" do + context "Calling Paperclip.run and logging" do + setup do + Paperclip.options[:image_magick_path] = nil + Paperclip.options[:command_path] = nil + Paperclip.stubs(:bit_bucket).returns("/dev/null") + Paperclip.stubs(:log) + Paperclip.stubs(:"`").with("this is the command 2>/dev/null") + end + + should "log the command when :log_command is true" do Paperclip.options[:log_command] = true - Paperclip.expects(: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.run("this","is the command") + assert_received(Paperclip, :log) do |p| + p.with("this is the command 2>/dev/null") + end + assert_received(Paperclip, :`) do |p| + p.with("this is the command 2>/dev/null") + 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) } + should "not log the command when :log_command is false" do + Paperclip.options[:log_command] = false + Paperclip.run("this","is the command") + assert_received(Paperclip, :log) do |p| + p.with("this is the command 2>/dev/null").never + end + assert_received(Paperclip, :`) do |p| + p.with("this is the command 2>/dev/null") + end + end end - should "raise when sent #processor and the name of a class that doesn't exist" do - assert_raises(NameError){ Paperclip.processor(:boogey_man) } - end - - should "return a class when sent #processor and the name of a class under Paperclip" do - assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail) - end - - should "call a proc sent to check_guard" do - @dummy = Dummy.new - @dummy.expects(:one).returns(:one) - assert_equal :one, @dummy.avatar.send(:check_guard, lambda{|x| x.one }) - end - - should "call a method name sent to check_guard" do - @dummy = Dummy.new - @dummy.expects(:one).returns(:one) - assert_equal :one, @dummy.avatar.send(:check_guard, :one) - end - context "Paperclip.bit_bucket" do context "on systems without /dev/null" do setup do File.expects(:exists?).with("/dev/null").returns(false) end @@ -84,10 +92,22 @@ 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 + assert_raises(NameError){ Paperclip.processor(:boogey_man) } + end + + should "return a class when sent #processor and the name of a class under Paperclip" do + assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail) + end + context "An ActiveRecord model with an 'avatar' attachment" do setup do rebuild_model :path => "tmp/:class/omg/:style.:extension" @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') end @@ -137,11 +157,12 @@ @subdummy = SubDummy.create(:avatar => @file) end end should "be able to see the attachment definition from the subclass's class" do - assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path] + assert_equal "tmp/:class/omg/:style.:extension", + SubDummy.attachment_definitions[:avatar][:path] end teardown do Object.send(:remove_const, "SubDummy") rescue nil end @@ -283,9 +304,24 @@ validation, options, valid_file, invalid_file = args valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb") invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb") should_validate validation, options, valid_file, invalid_file + end + + context "with size validation and less_than 10240 option" do + context "and assigned an invalid file" do + setup do + Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240) + @dummy = Dummy.new + @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb") + @dummy.valid? + end + + should "have a file size min/max error message" do + assert_match /between 0 and 10240 bytes/, @dummy.errors.on(:avatar) + end + end end end end