@@ -18,6 +18,10 @@ using fmt::buffered_file;
18
18
using testing::HasSubstr;
19
19
using wstring_view = fmt::basic_string_view<wchar_t >;
20
20
21
+ static std::string uniq_file_name (unsigned line_number) {
22
+ return " test-file" + std::to_string (line_number);
23
+ }
24
+
21
25
#ifdef _WIN32
22
26
23
27
# include < windows.h>
@@ -232,68 +236,75 @@ TEST(buffered_file_test, descriptor) {
232
236
}
233
237
234
238
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);
236
241
fmt::ostream moved (std::move (out));
237
242
moved.print (" hello" );
238
243
}
239
244
240
245
TEST (ostream_test, move_while_holding_data) {
246
+ auto test_file = uniq_file_name (__LINE__);
241
247
{
242
- fmt::ostream out = fmt::output_file (" test-file " );
248
+ fmt::ostream out = fmt::output_file (test_file );
243
249
out.print (" Hello, " );
244
250
fmt::ostream moved (std::move (out));
245
251
moved.print (" world!\n " );
246
252
}
247
253
{
248
- file in (" test-file " , file::RDONLY);
254
+ file in (test_file , file::RDONLY);
249
255
EXPECT_READ (in, " Hello, world!\n " );
250
256
}
251
257
}
252
258
253
259
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);
255
262
out.print (" The answer is {}.\n " , 42 );
256
263
out.close ();
257
- file in (" test-file " , file::RDONLY);
264
+ file in (test_file , file::RDONLY);
258
265
EXPECT_READ (in, " The answer is 42.\n " );
259
266
}
260
267
261
268
TEST (ostream_test, buffer_boundary) {
262
269
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);
264
272
out.print (" {}" , str);
265
273
out.print (" {}" , str);
266
274
out.close ();
267
- file in (" test-file " , file::RDONLY);
275
+ file in (test_file , file::RDONLY);
268
276
EXPECT_READ (in, str + str);
269
277
}
270
278
271
279
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 );
273
282
out.print (" {}" , " foo" );
274
283
out.close ();
275
- file in (" test-file " , file::RDONLY);
284
+ file in (test_file , file::RDONLY);
276
285
EXPECT_READ (in, " foo" );
277
286
}
278
287
279
288
TEST (ostream_test, truncate) {
289
+ auto test_file = uniq_file_name (__LINE__);
280
290
{
281
- fmt::ostream out = fmt::output_file (" test-file " );
291
+ fmt::ostream out = fmt::output_file (test_file );
282
292
out.print (" 0123456789" );
283
293
}
284
294
{
285
- fmt::ostream out = fmt::output_file (" test-file " );
295
+ fmt::ostream out = fmt::output_file (test_file );
286
296
out.print (" foo" );
287
297
}
288
- file in (" test-file " , file::RDONLY);
298
+ file in (test_file , file::RDONLY);
289
299
EXPECT_EQ (" foo" , read (in, 4 ));
290
300
}
291
301
292
302
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);
294
305
out.print (" x" );
295
306
out.flush ();
296
- auto in = fmt::file (" test-file " , file::RDONLY);
307
+ auto in = fmt::file (test_file , file::RDONLY);
297
308
EXPECT_READ (in, " x" );
298
309
}
299
310
@@ -303,10 +314,11 @@ TEST(file_test, default_ctor) {
303
314
}
304
315
305
316
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" );
307
319
std::fputs (file_content, fp);
308
320
std::fclose (fp);
309
- file f (" test-file " , file::RDONLY);
321
+ file f (test_file. c_str () , file::RDONLY);
310
322
// Check if the file is open by reading one character from it.
311
323
char buffer;
312
324
bool isopen = FMT_POSIX (read (f.descriptor (), &buffer, 1 )) == 1 ;
@@ -417,7 +429,8 @@ TEST(file_test, read) {
417
429
}
418
430
419
431
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);
421
434
char buf;
422
435
// We intentionally read from a file opened in the write-only mode to
423
436
// cause error.
@@ -426,13 +439,15 @@ TEST(file_test, read_error) {
426
439
427
440
TEST (file_test, write) {
428
441
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);
430
444
pipe .write_end .close ();
431
- EXPECT_READ (pipe .read_end , " test " );
445
+ EXPECT_READ (pipe .read_end , test_file );
432
446
}
433
447
434
448
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);
436
451
// We intentionally write to a file opened in the read-only mode to
437
452
// cause error.
438
453
EXPECT_SYSTEM_ERROR (f.write (" " , 1 ), EBADF, " cannot write to file" );
0 commit comments