Skip to content

Commit

Permalink
Fix to raise exception when ImageMagick is not installed
Browse files Browse the repository at this point in the history
Fixes #2060
  • Loading branch information
mshibuya committed Jan 22, 2023
1 parent f34a9bd commit d90c399
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/carrierwave/processing/mini_magick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ def manipulate!
FileUtils.mv image.path, current_path

image.run_command("identify", current_path)
rescue ::MiniMagick::Error, ::MiniMagick::Invalid
rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
raise e if e.message =~ /(You must have .+ installed|is not installed|executable not found)/
message = I18n.translate(:"errors.messages.processing_error")
raise CarrierWave::ProcessingError, message
ensure
Expand Down Expand Up @@ -309,7 +310,8 @@ def minimagick!(block = nil)
file.content_type = Marcel::Magic.by_path(move_to).try(:type)
file.move_to(move_to, permissions, directory_permissions)
end
rescue ::MiniMagick::Error, ::MiniMagick::Invalid
rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
raise e if e.message =~ /(You must have .+ installed|is not installed|executable not found)/
message = I18n.translate(:"errors.messages.processing_error")
raise CarrierWave::ProcessingError, message
end
Expand Down
64 changes: 64 additions & 0 deletions spec/processing/mini_magick_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,69 @@
end
end
end

context "of failing to find ImageMagick/GraphicsMagick" do
before do
MiniMagick.remove_instance_variable(:@processor) if MiniMagick.instance_variable_defined?(:@processor)
MiniMagick.remove_instance_variable(:@cli) if MiniMagick.instance_variable_defined?(:@cli)
allow(MiniMagick::Utilities).to receive(:which).and_return(nil)
end

it "raises MiniMagick::Error" do
expect { instance.resize_to_limit(200, 200) }.to raise_exception(MiniMagick::Error)
end
end

context "of being configured to use ImageMagick but failing to execute" do
before do
allow(MiniMagick).to receive(:processor).and_return(:magick)
allow_any_instance_of(MiniMagick::Shell).to receive(:execute_open3).and_raise(Errno::ENOENT)
end

it "raises MiniMagick::Error" do
expect { instance.resize_to_limit(200, 200) }.to raise_exception(MiniMagick::Error)
end
end
end

describe "#manipulate!" do
it "performs manipulation using the given block" do
instance.manipulate! do |image|
image.format('png')
end
expect(instance).to be_format('png')
end

context "on failing to find ImageMagick/GraphicsMagick" do
before do
MiniMagick.remove_instance_variable(:@processor) if MiniMagick.instance_variable_defined?(:@processor)
MiniMagick.remove_instance_variable(:@cli) if MiniMagick.instance_variable_defined?(:@cli)
allow(MiniMagick::Utilities).to receive(:which).and_return(nil)
end

it "raises MiniMagick::Invalid" do
expect do
instance.manipulate! do |image|
image.format('png')
end
end.to raise_exception(MiniMagick::Invalid)
end
end

context "on being configured to use ImageMagick but failing to execute" do
before do
allow(MiniMagick).to receive(:processor).and_return(:magick)
allow_any_instance_of(MiniMagick::Shell).to receive(:execute_open3).and_raise(Errno::ENOENT)
end
after { MiniMagick.remove_instance_variable(:@processor) }

it "raises MiniMagick::Invalid" do
expect do
instance.manipulate! do |image|
image.format('png')
end
end.to raise_exception(MiniMagick::Invalid)
end
end
end
end

0 comments on commit d90c399

Please sign in to comment.