Skip to content

Commit dfe5b12

Browse files
authored
Update os-test.cc (#3883)
uniq test file names for the os-test suite (#3878 issue).
1 parent 09935d8 commit dfe5b12

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

test/os-test.cc

+35-20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ using fmt::buffered_file;
1818
using testing::HasSubstr;
1919
using wstring_view = fmt::basic_string_view<wchar_t>;
2020

21+
static std::string uniq_file_name(unsigned line_number) {
22+
return "test-file" + std::to_string(line_number);
23+
}
24+
2125
#ifdef _WIN32
2226

2327
# include <windows.h>
@@ -232,68 +236,75 @@ TEST(buffered_file_test, descriptor) {
232236
}
233237

234238
TEST(ostream_test, move) {
235-
fmt::ostream out = fmt::output_file("test-file");
239+
auto test_file = uniq_file_name(__LINE__);
240+
fmt::ostream out = fmt::output_file(test_file);
236241
fmt::ostream moved(std::move(out));
237242
moved.print("hello");
238243
}
239244

240245
TEST(ostream_test, move_while_holding_data) {
246+
auto test_file = uniq_file_name(__LINE__);
241247
{
242-
fmt::ostream out = fmt::output_file("test-file");
248+
fmt::ostream out = fmt::output_file(test_file);
243249
out.print("Hello, ");
244250
fmt::ostream moved(std::move(out));
245251
moved.print("world!\n");
246252
}
247253
{
248-
file in("test-file", file::RDONLY);
254+
file in(test_file, file::RDONLY);
249255
EXPECT_READ(in, "Hello, world!\n");
250256
}
251257
}
252258

253259
TEST(ostream_test, print) {
254-
fmt::ostream out = fmt::output_file("test-file");
260+
auto test_file = uniq_file_name(__LINE__);
261+
fmt::ostream out = fmt::output_file(test_file);
255262
out.print("The answer is {}.\n", 42);
256263
out.close();
257-
file in("test-file", file::RDONLY);
264+
file in(test_file, file::RDONLY);
258265
EXPECT_READ(in, "The answer is 42.\n");
259266
}
260267

261268
TEST(ostream_test, buffer_boundary) {
262269
auto str = std::string(4096, 'x');
263-
fmt::ostream out = fmt::output_file("test-file");
270+
auto test_file = uniq_file_name(__LINE__);
271+
fmt::ostream out = fmt::output_file(test_file);
264272
out.print("{}", str);
265273
out.print("{}", str);
266274
out.close();
267-
file in("test-file", file::RDONLY);
275+
file in(test_file, file::RDONLY);
268276
EXPECT_READ(in, str + str);
269277
}
270278

271279
TEST(ostream_test, buffer_size) {
272-
fmt::ostream out = fmt::output_file("test-file", fmt::buffer_size = 1);
280+
auto test_file = uniq_file_name(__LINE__);
281+
fmt::ostream out = fmt::output_file(test_file, fmt::buffer_size = 1);
273282
out.print("{}", "foo");
274283
out.close();
275-
file in("test-file", file::RDONLY);
284+
file in(test_file, file::RDONLY);
276285
EXPECT_READ(in, "foo");
277286
}
278287

279288
TEST(ostream_test, truncate) {
289+
auto test_file = uniq_file_name(__LINE__);
280290
{
281-
fmt::ostream out = fmt::output_file("test-file");
291+
fmt::ostream out = fmt::output_file(test_file);
282292
out.print("0123456789");
283293
}
284294
{
285-
fmt::ostream out = fmt::output_file("test-file");
295+
fmt::ostream out = fmt::output_file(test_file);
286296
out.print("foo");
287297
}
288-
file in("test-file", file::RDONLY);
298+
file in(test_file, file::RDONLY);
289299
EXPECT_EQ("foo", read(in, 4));
290300
}
291301

292302
TEST(ostream_test, flush) {
293-
auto out = fmt::output_file("test-file");
303+
auto test_file = uniq_file_name(__LINE__);
304+
auto out = fmt::output_file(test_file);
294305
out.print("x");
295306
out.flush();
296-
auto in = fmt::file("test-file", file::RDONLY);
307+
auto in = fmt::file(test_file, file::RDONLY);
297308
EXPECT_READ(in, "x");
298309
}
299310

@@ -303,10 +314,11 @@ TEST(file_test, default_ctor) {
303314
}
304315

305316
TEST(file_test, open_buffered_file_in_ctor) {
306-
FILE* fp = safe_fopen("test-file", "w");
317+
auto test_file = uniq_file_name(__LINE__);
318+
FILE* fp = safe_fopen(test_file.c_str(), "w");
307319
std::fputs(file_content, fp);
308320
std::fclose(fp);
309-
file f("test-file", file::RDONLY);
321+
file f(test_file.c_str(), file::RDONLY);
310322
// Check if the file is open by reading one character from it.
311323
char buffer;
312324
bool isopen = FMT_POSIX(read(f.descriptor(), &buffer, 1)) == 1;
@@ -417,7 +429,8 @@ TEST(file_test, read) {
417429
}
418430

419431
TEST(file_test, read_error) {
420-
file f("test-file", file::WRONLY);
432+
auto test_file = uniq_file_name(__LINE__);
433+
file f(test_file, file::WRONLY | file::CREATE);
421434
char buf;
422435
// We intentionally read from a file opened in the write-only mode to
423436
// cause error.
@@ -426,13 +439,15 @@ TEST(file_test, read_error) {
426439

427440
TEST(file_test, write) {
428441
auto pipe = fmt::pipe();
429-
write(pipe.write_end, "test");
442+
auto test_file = uniq_file_name(__LINE__);
443+
write(pipe.write_end, test_file);
430444
pipe.write_end.close();
431-
EXPECT_READ(pipe.read_end, "test");
445+
EXPECT_READ(pipe.read_end, test_file);
432446
}
433447

434448
TEST(file_test, write_error) {
435-
file f("test-file", file::RDONLY);
449+
auto test_file = uniq_file_name(__LINE__);
450+
file f(test_file, file::RDONLY | file::CREATE);
436451
// We intentionally write to a file opened in the read-only mode to
437452
// cause error.
438453
EXPECT_SYSTEM_ERROR(f.write(" ", 1), EBADF, "cannot write to file");

0 commit comments

Comments
 (0)