Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tempfile using the basename without extension: #201

Merged
merged 5 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rack/test/uploaded_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def initialize_from_file_path(path)
raise "#{path} file does not exist" unless ::File.exist?(path)

@original_filename = ::File.basename(path)
extension = ::File.extname(@original_filename)

@tempfile = Tempfile.new([@original_filename, ::File.extname(path)])
@tempfile = Tempfile.new([::File.basename(@original_filename, extension), extension])
@tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)

ObjectSpace.define_finalizer(self, self.class.finalize(@tempfile))
Expand Down
7 changes: 7 additions & 0 deletions spec/rack/test/uploaded_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def test_file_path
expect(File.extname(uploaded_file.path)).to eq('.txt')
end

it 'creates Tempfiles with a path that includes a single extension' do
uploaded_file = Rack::Test::UploadedFile.new(test_file_path)

regex = /foo#{Time.now.year}.*\.txt\Z/
expect(uploaded_file.path).to match(regex)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, this is what the test output looks like with this change, but without the actual bug fix:

  1) Rack::Test::UploadedFile creates Tempfiles with a path that includes a single extension
     Failure/Error: expect(uploaded_file.path).to match(regex)

       expected "/var/folders/r8/mlwxnwln1vl5_t_nn90cpr5w0000gn/T/foo.txt20180327-86535-10jssch.txt" to match /foo2018.*\.txt\Z/
       Diff:
       @@ -1,2 +1,2 @@
       -/foo2018.*\.txt\Z/
       +"/var/folders/r8/mlwxnwln1vl5_t_nn90cpr5w0000gn/T/foo.txt20180327-86535-10jssch.txt"

...i.e. foo.txt20180327-86535-10jssch.txt contains the .txt extension twice.


context 'it should call its destructor' do
it 'calls the destructor' do
expect(Rack::Test::UploadedFile).to receive(:actually_finalize).at_least(:once)
Expand Down