From 2f374787c1bc4ec0c31bf665fbadc8a5e79e18ed Mon Sep 17 00:00:00 2001 From: Edouard Chin Date: Tue, 27 Mar 2018 13:59:29 -0400 Subject: [PATCH] Create tempfile using the basename without extension: (#201) * Create tempfile using the basename without extension: - 5f7845118f649b60862a9bc01f6b2a0f932e89d7 introduced a change to keep the filename extension when creating the tempfile - `Tempfile#path` is now returning the filename with the extension followed by random chars and the extension again ```ruby puts Rack::Test::UploadedFile.new('foo.txt').path # => '/var/xxx/foo.txt2017-30-08-dsad.txt' ``` - This PR modifies this behaviour to not include the file extension as part of the file basename when creating the Tempfile * PR Review --- lib/rack/test/uploaded_file.rb | 3 ++- spec/rack/test/uploaded_file_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rack/test/uploaded_file.rb b/lib/rack/test/uploaded_file.rb index cb68bd83..d77fa353 100644 --- a/lib/rack/test/uploaded_file.rb +++ b/lib/rack/test/uploaded_file.rb @@ -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)) diff --git a/spec/rack/test/uploaded_file_spec.rb b/spec/rack/test/uploaded_file_spec.rb index d558f64b..84d09f83 100644 --- a/spec/rack/test/uploaded_file_spec.rb +++ b/spec/rack/test/uploaded_file_spec.rb @@ -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 + context 'it should call its destructor' do it 'calls the destructor' do expect(Rack::Test::UploadedFile).to receive(:actually_finalize).at_least(:once)