Skip to content

Commit 8488344

Browse files
authored
Ensure saved files truncate existing file by default. (#33)
1 parent 931c772 commit 8488344

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/protocol/http/body/reader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def finish
4141
end
4242

4343
# Write the body of the response to the given file path.
44-
def save(path, mode = ::File::WRONLY|::File::CREAT, **options)
44+
def save(path, mode = ::File::WRONLY|::File::CREAT|::File::TRUNC, **options)
4545
if @body
4646
::File.open(path, mode, **options) do |file|
4747
self.each do |chunk|

test/protocol/http/body/reader.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ def initialize(body)
4040
reader.save(path)
4141
expect(File.read(path)).to be == 'thequickbrownfox'
4242
end
43-
43+
44+
it 'saves by truncating an existing file if it exists' do
45+
File.write(path, 'hello' * 100)
46+
reader.save(path)
47+
expect(File.read(path)).to be == 'thequickbrownfox'
48+
end
49+
4450
it 'mirrors the interface of File.open' do
4551
reader.save(path, 'w')
4652
expect(File.read(path)).to be == 'thequickbrownfox'

0 commit comments

Comments
 (0)