Skip to content

Commit

Permalink
Create tempfile using the basename without extension: (#201)
Browse files Browse the repository at this point in the history
* Create tempfile using the basename without extension:

- 5f78451 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
  • Loading branch information
Edouard-chin authored and perlun committed Mar 27, 2018
1 parent 07a57b6 commit 2f37478
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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

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

0 comments on commit 2f37478

Please sign in to comment.