Update os-test.cc (#3883)

uniq test file names for the os-test suite (#3878 issue).
This commit is contained in:
vgorrX 2024-03-06 21:37:57 +07:00 committed by GitHub
parent 09935d8239
commit dfe5b12c08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,6 +18,10 @@ using fmt::buffered_file;
using testing::HasSubstr;
using wstring_view = fmt::basic_string_view<wchar_t>;
static std::string uniq_file_name(unsigned line_number) {
return "test-file" + std::to_string(line_number);
}
#ifdef _WIN32
# include <windows.h>
@ -232,68 +236,75 @@ TEST(buffered_file_test, descriptor) {
}
TEST(ostream_test, move) {
fmt::ostream out = fmt::output_file("test-file");
auto test_file = uniq_file_name(__LINE__);
fmt::ostream out = fmt::output_file(test_file);
fmt::ostream moved(std::move(out));
moved.print("hello");
}
TEST(ostream_test, move_while_holding_data) {
auto test_file = uniq_file_name(__LINE__);
{
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");
auto test_file = uniq_file_name(__LINE__);
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) {
auto str = std::string(4096, 'x');
fmt::ostream out = fmt::output_file("test-file");
auto test_file = uniq_file_name(__LINE__);
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);
auto test_file = uniq_file_name(__LINE__);
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) {
auto test_file = uniq_file_name(__LINE__);
{
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");
auto test_file = uniq_file_name(__LINE__);
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");
}
@ -303,10 +314,11 @@ TEST(file_test, default_ctor) {
}
TEST(file_test, open_buffered_file_in_ctor) {
FILE* fp = safe_fopen("test-file", "w");
auto test_file = uniq_file_name(__LINE__);
FILE* fp = safe_fopen(test_file.c_str(), "w");
std::fputs(file_content, fp);
std::fclose(fp);
file f("test-file", file::RDONLY);
file f(test_file.c_str(), file::RDONLY);
// Check if the file is open by reading one character from it.
char buffer;
bool isopen = FMT_POSIX(read(f.descriptor(), &buffer, 1)) == 1;
@ -417,7 +429,8 @@ TEST(file_test, read) {
}
TEST(file_test, read_error) {
file f("test-file", file::WRONLY);
auto test_file = uniq_file_name(__LINE__);
file f(test_file, file::WRONLY | file::CREATE);
char buf;
// We intentionally read from a file opened in the write-only mode to
// cause error.
@ -426,13 +439,15 @@ TEST(file_test, read_error) {
TEST(file_test, write) {
auto pipe = fmt::pipe();
write(pipe.write_end, "test");
auto test_file = uniq_file_name(__LINE__);
write(pipe.write_end, test_file);
pipe.write_end.close();
EXPECT_READ(pipe.read_end, "test");
EXPECT_READ(pipe.read_end, test_file);
}
TEST(file_test, write_error) {
file f("test-file", file::RDONLY);
auto test_file = uniq_file_name(__LINE__);
file f(test_file, file::RDONLY | file::CREATE);
// 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");