test/paperclip_test.rb in paperclip-cloudfiles-2.3.2 vs test/paperclip_test.rb in paperclip-cloudfiles-2.3.8

- old
+ new

@@ -1,97 +1,64 @@ -require 'test/helper' +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") + should "tell you the command isn't there if the shell returns 127" do + with_exitstatus_returning(127) do + assert_raises(Paperclip::CommandNotFoundError) do + Paperclip.run("command") + end + 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) + should "tell you the command isn't there if an ENOENT is raised" do + assert_raises(Paperclip::CommandNotFoundError) do + Paperclip::CommandLine.stubs(:"`").raises(Errno::ENOENT) + Paperclip.run("command") end - - should "return 'NUL'" do - assert_equal "NUL", Paperclip.bit_bucket - end end + end - context "on systems with /dev/null" do - setup do - File.expects(:exists?).with("/dev/null").returns(true) + context "Paperclip.each_instance_with_attachment" do + setup do + @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') + d1 = Dummy.create(:avatar => @file) + d2 = Dummy.create + d3 = Dummy.create(:avatar => @file) + @expected = [d1, d3] + end + should "yield every instance of a model that has an attachment" do + actual = [] + Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance| + actual << instance end - - should "return '/dev/null'" do - assert_equal "/dev/null", Paperclip.bit_bucket - end + assert_same_elements @expected, actual 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) } @@ -130,19 +97,19 @@ end should "not assign the avatar on mass-set" do @dummy.attributes = { :other => "I'm set!", :avatar => @file } - + assert_equal "I'm set!", @dummy.other assert ! @dummy.avatar? end should "still allow assigment on normal set" do @dummy.other = "I'm set!" @dummy.avatar = @file - + assert_equal "I'm set!", @dummy.other assert @dummy.avatar? end end @@ -241,13 +208,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 @@ -258,11 +232,11 @@ 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 @@ -293,24 +267,24 @@ [:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"], [:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args| 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 @dummy.errors[:avatar_file_size].any?{|e| e.match %r/between 0 and 10240 bytes/ } + assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/between 0 and 10240 bytes/ } end end end end