diff --git a/test/os-test.cc b/test/os-test.cc index 079ec920..7a18a3b4 100644 --- a/test/os-test.cc +++ b/test/os-test.cc @@ -10,6 +10,7 @@ #include // std::exit #include #include +#include #include "gtest-extra.h" #include "util.h" @@ -17,6 +18,14 @@ using fmt::buffered_file; using testing::HasSubstr; using wstring_view = fmt::basic_string_view; +std::mutex mutex_file_name; +std::string get_test_file_name() { + static int index = 0; + mutex_file_name.lock(); + std::string test_file = "test-file-" + std::to_string(index++); + mutex_file_name.unlock(); + return test_file; +} #ifdef _WIN32 @@ -90,10 +99,11 @@ TEST(os_test, report_windows_error) { # if FMT_USE_FCNTL && !defined(__MINGW32__) TEST(file_test, open_windows_file) { using fmt::file; - file out = file::open_windows_file(L"test-file", +std::string test_file = get_test_file_name(); + file out = file::open_windows_file(test_file, file::WRONLY | file::CREATE | file::TRUNC); out.write("x", 1); - file in = file::open_windows_file(L"test-file", file::RDONLY); + file in = file::open_windows_file(test_file, file::RDONLY); EXPECT_READ(in, "x"); } # endif // FMT_USE_FCNTL && !defined(__MINGW32__) @@ -232,68 +242,74 @@ TEST(buffered_file_test, descriptor) { } TEST(ostream_test, move) { - fmt::ostream out = fmt::output_file("test-file"); + fmt::ostream out = fmt::output_file(get_test_file_name()); fmt::ostream moved(std::move(out)); moved.print("hello"); } TEST(ostream_test, move_while_holding_data) { +std::string test_file = get_test_file_name(); { - fmt::ostream out = fmt::output_file("test-file"); + fmt::ostream out = fmt::output_file(test_file); out.print("Hello, "); fmt::ostream moved(std::move(out)); moved.print("world!\n"); } { - file in("test-file", file::RDONLY); + file in(test_file, file::RDONLY); EXPECT_READ(in, "Hello, world!\n"); } } TEST(ostream_test, print) { - fmt::ostream out = fmt::output_file("test-file"); +std::string test_file = get_test_file_name(); + fmt::ostream out = fmt::output_file(test_file); out.print("The answer is {}.\n", 42); out.close(); - file in("test-file", file::RDONLY); + file in(test_file, file::RDONLY); EXPECT_READ(in, "The answer is 42.\n"); } TEST(ostream_test, buffer_boundary) { +std::string test_file = get_test_file_name(); auto str = std::string(4096, 'x'); - fmt::ostream out = fmt::output_file("test-file"); + fmt::ostream out = fmt::output_file(test_file); out.print("{}", str); out.print("{}", str); out.close(); - file in("test-file", file::RDONLY); + file in(test_file, file::RDONLY); EXPECT_READ(in, str + str); } TEST(ostream_test, buffer_size) { - fmt::ostream out = fmt::output_file("test-file", fmt::buffer_size = 1); +std::string test_file = get_test_file_name(); + fmt::ostream out = fmt::output_file(test_file, fmt::buffer_size = 1); out.print("{}", "foo"); out.close(); - file in("test-file", file::RDONLY); + file in(test_file, file::RDONLY); EXPECT_READ(in, "foo"); } TEST(ostream_test, truncate) { +std::string test_file = get_test_file_name(); { - fmt::ostream out = fmt::output_file("test-file"); + fmt::ostream out = fmt::output_file(test_file); out.print("0123456789"); } { - fmt::ostream out = fmt::output_file("test-file"); + fmt::ostream out = fmt::output_file(test_file); out.print("foo"); } - file in("test-file", file::RDONLY); + file in(test_file, file::RDONLY); EXPECT_EQ("foo", read(in, 4)); } TEST(ostream_test, flush) { - auto out = fmt::output_file("test-file"); +std::string test_file = get_test_file_name(); + auto out = fmt::output_file(test_file); out.print("x"); out.flush(); - auto in = fmt::file("test-file", file::RDONLY); + auto in = fmt::file(test_file, file::RDONLY); EXPECT_READ(in, "x"); } @@ -417,7 +433,7 @@ TEST(file_test, read) { } TEST(file_test, read_error) { - file f("test-file", file::WRONLY); + file f(get_test_file_name(), file::WRONLY); char buf; // We intentionally read from a file opened in the write-only mode to // cause error. @@ -432,7 +448,7 @@ TEST(file_test, write) { } TEST(file_test, write_error) { - file f("test-file", file::RDONLY); + file f(get_test_file_name(), file::RDONLY); // We intentionally write to a file opened in the read-only mode to // cause error. EXPECT_SYSTEM_ERROR(f.write(" ", 1), EBADF, "cannot write to file");