Skip to content

Commit

Permalink
Add exception message to avoid exposing class (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
j15e committed Jul 20, 2023
1 parent 8bdf43f commit 88a2ce1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/paperclip/geometry_detector_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(file)

def make
geometry = GeometryParser.new(geometry_string.strip).make
geometry || raise(Errors::NotIdentifiedByImageMagickError.new)
geometry || raise(Errors::NotIdentifiedByImageMagickError.new("Could not identify image size"))
end

private
Expand Down
9 changes: 9 additions & 0 deletions spec/paperclip/geometry_detector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@
Paperclip.options[:use_exif_orientation] = true
end
end

it "raises an exception with a message when the file is not an image" do
file = fixture_file("text.txt")
factory = Paperclip::GeometryDetector.new(file)

expect do
factory.make
end.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError, "Could not identify image size")
end
end
20 changes: 16 additions & 4 deletions spec/paperclip/geometry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,35 @@

it "does not generate from a bad file" do
file = "/home/This File Does Not Exist.omg"
expect { @geo = Paperclip::Geometry.from_file(file) }.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError)
expect do
@geo = Paperclip::Geometry.from_file(file)
end.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError,
"Could not identify image size")
end

it "does not generate from a blank filename" do
file = ""
expect { @geo = Paperclip::Geometry.from_file(file) }.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError)
expect do
@geo = Paperclip::Geometry.from_file(file)
end.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError,
"Cannot find the geometry of a file with a blank name")
end

it "does not generate from a nil file" do
file = nil
expect { @geo = Paperclip::Geometry.from_file(file) }.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError)
expect do
@geo = Paperclip::Geometry.from_file(file)
end.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError,
"Cannot find the geometry of a file with a blank name")
end

it "does not generate from a file with no path" do
file = double("file", path: "")
allow(file).to receive(:respond_to?).with(:path).and_return(true)
expect { @geo = Paperclip::Geometry.from_file(file) }.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError)
expect do
@geo = Paperclip::Geometry.from_file(file)
end.to raise_error(Paperclip::Errors::NotIdentifiedByImageMagickError,
"Cannot find the geometry of a file with a blank name")
end

it "lets us know when a command isn't found versus a processing error" do
Expand Down

0 comments on commit 88a2ce1

Please sign in to comment.