|
2 | 2 |
|
3 | 3 | describe "IO::Buffer.map" do |
4 | 4 | before :all do |
5 | | - @big_file_name = tmp("big_file") |
6 | | - # Usually 4 kibibytes + 16 bytes |
7 | | - File.write(@big_file_name, "12345678" * (IO::Buffer::PAGE_SIZE / 8 + 2)) |
| 5 | + @tmp_files = [] |
| 6 | + |
| 7 | + @big_file_name = nil |
| 8 | + @small_file_name = nil |
8 | 9 | end |
9 | 10 |
|
10 | 11 | after :all do |
11 | | - File.delete(@big_file_name) |
| 12 | + @tmp_files.each {|file| File.delete(file)} |
12 | 13 | end |
13 | 14 |
|
14 | 15 | def open_fixture |
15 | | - File.open("#{__dir__}/../fixtures/read_text.txt", "rb+") |
| 16 | + unless @small_file_name |
| 17 | + @small_file_name = tmp("read_text.txt") |
| 18 | + File.copy_stream(fixture(__dir__, "read_text.txt"), @small_file_name) |
| 19 | + @tmp_files << @small_file_name |
| 20 | + end |
| 21 | + File.open(@small_file_name, "rb+") |
16 | 22 | end |
17 | 23 |
|
18 | 24 | def open_big_file_fixture |
| 25 | + unless @big_file_name |
| 26 | + @big_file_name = tmp("big_file") |
| 27 | + # Usually 4 kibibytes + 16 bytes |
| 28 | + File.write(@big_file_name, "12345678" * (IO::Buffer::PAGE_SIZE / 8 + 2)) |
| 29 | + @tmp_files << @big_file_name |
| 30 | + end |
19 | 31 | File.open(@big_file_name, "rb+") |
20 | 32 | end |
21 | 33 |
|
@@ -91,15 +103,16 @@ def open_big_file_fixture |
91 | 103 | context "with an empty file" do |
92 | 104 | ruby_version_is "4.0" do |
93 | 105 | it "raises ArgumentError" do |
94 | | - @file = File.open("#{__dir__}/../fixtures/empty.txt", "rb+") |
| 106 | + file_name = tmp("empty.txt") |
| 107 | + @file = File.open(file_name, "wb+") |
95 | 108 | -> { IO::Buffer.map(@file) }.should raise_error(ArgumentError, "Invalid negative or zero file size!") |
96 | 109 | end |
97 | 110 | end |
98 | 111 | end |
99 | 112 |
|
100 | 113 | context "with a file opened only for reading" do |
101 | 114 | it "raises a SystemCallError unless read-only" do |
102 | | - @file = File.open("#{__dir__}/../fixtures/read_text.txt", "rb") |
| 115 | + @file = File.open(fixture(__dir__, "read_text.txt"), "rb") |
103 | 116 | -> { IO::Buffer.map(@file) }.should raise_error(SystemCallError) |
104 | 117 | end |
105 | 118 | end |
@@ -251,7 +264,7 @@ def open_big_file_fixture |
251 | 264 | end |
252 | 265 |
|
253 | 266 | it "allows mapping read-only files" do |
254 | | - @file = File.open("#{__dir__}/../fixtures/read_text.txt", "rb") |
| 267 | + @file = File.open(fixture(__dir__, "read_text.txt"), "rb") |
255 | 268 | @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::READONLY) |
256 | 269 |
|
257 | 270 | @buffer.should.readonly? |
@@ -284,7 +297,7 @@ def open_big_file_fixture |
284 | 297 | end |
285 | 298 |
|
286 | 299 | it "allows mapping read-only files and modifying the buffer" do |
287 | | - @file = File.open("#{__dir__}/../fixtures/read_text.txt", "rb") |
| 300 | + @file = File.open(fixture(__dir__, "read_text.txt"), "rb") |
288 | 301 | @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::PRIVATE) |
289 | 302 |
|
290 | 303 | @buffer.should.private? |
|
0 commit comments