Skip to content

Commit

Permalink
UploadedFile#close should leave UploadedFile in a state where #opened…
Browse files Browse the repository at this point in the history
…? is false (#656)

This means removing the reference to the now-closed (and unusable) IO object. Which meant refactoring an existing test slightly, as it tried to refer to the IO object after UploadedFile#close.
  • Loading branch information
jrochkind authored Apr 28, 2024
1 parent 8064881 commit 7aa69d8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/shrine/uploaded_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def rewind
# opened IO object.
def close
io.close if opened?
@io = nil
end

# Returns whether the file has already been opened.
Expand Down
5 changes: 4 additions & 1 deletion test/plugin/rack_response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@
uploaded_file = @uploader.upload(fakeio)
uploaded_file.open
response = uploaded_file.to_rack_response

existing_io = uploaded_file.to_io

response[2].close
assert uploaded_file.to_io.closed?
assert existing_io.closed?
end

it "returns ranged responses when :range is given" do
Expand Down
8 changes: 8 additions & 0 deletions test/uploaded_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ def uploaded_file(data = {})
uploaded_file.storage.expects(:open).never
uploaded_file.close
end

it "leaves the UploadedFile no longer #opened?" do
uploaded_file = @uploader.upload(fakeio)
uploaded_file.open
uploaded_file.close

refute uploaded_file.opened?
end
end

describe "#url" do
Expand Down

0 comments on commit 7aa69d8

Please sign in to comment.