Skip to content

Commit 582493a

Browse files
committed
Fix reading and writing (use binary modes)
1 parent cc57fc4 commit 582493a

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

lib/vcr/cassette/persisters/file_system.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def storage_location=(dir)
2323
def [](file_name)
2424
path = absolute_path_to_file(file_name)
2525
return nil unless File.exist?(path)
26-
File.read(path)
26+
File.binread(path)
2727
end
2828

2929
# Sets the cassette for the given storage key (file name).
@@ -34,7 +34,7 @@ def []=(file_name, content)
3434
path = absolute_path_to_file(file_name)
3535
directory = File.dirname(path)
3636
FileUtils.mkdir_p(directory) unless File.exist?(directory)
37-
File.open(path, 'w') { |f| f.write(content) }
37+
File.binwrite(path, content)
3838
end
3939

4040
# @private
@@ -44,7 +44,6 @@ def absolute_path_to_file(file_name)
4444
end
4545

4646
private
47-
4847
def absolute_path_for(path)
4948
Dir.chdir(path) { Dir.pwd }
5049
end

spec/lib/vcr/cassette/persisters/file_system_spec.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ class Persisters
2525
end
2626

2727
describe "#[]=" do
28-
it 'writes the given file contents to the given file name' do
29-
expect(File.exist?(FileSystem.storage_location + '/foo.txt')).to be false
30-
FileSystem["foo.txt"] = "bar"
31-
expect(File.read(FileSystem.storage_location + '/foo.txt')).to eq("bar")
28+
context 'with a simple file_name and binary content' do
29+
let(:file_name) { 'foo.txt' }
30+
let(:content) { SecureRandom.random_bytes(20) }
31+
let(:location) { FileSystem.storage_location + '/' + file_name }
32+
33+
it 'writes the given file contents to the given file name' do
34+
expect(File.exist?(location)).to be false
35+
FileSystem[file_name] = content
36+
expect(File.binread(location)).to eq(content)
37+
end
3238
end
3339

3440
it 'creates any needed intermediary directories' do

spec/lib/vcr/cassette_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def stub_old_interactions(interactions)
273273

274274
allow(::File).to receive(:exist?).with(file_name).and_return(true)
275275
allow(::File).to receive(:size?).with(file_name).and_return(true)
276-
allow(::File).to receive(:read).with(file_name).and_return(yaml)
276+
allow(::File).to receive(:binread).with(file_name).and_return(yaml)
277277
end
278278

279279
context 'and the earliest recorded interaction was recorded less than 7 days ago' do

0 commit comments

Comments
 (0)