From ac2ebe8d761d0b16ef887f33cd0495e2eb44fb19 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 22 Nov 2026 19:51:05 +0100 Subject: [PATCH] Use a unique temp file in the diagnostic positions ifstream test Since #6139, the C++11 and C++20 builds of unit-class_parser with JSON_DIAGNOSTIC_POSITIONS run in parallel in the same directory. Both wrote and removed the same fixed-name temp file, so one could read an empty file ("attempting to parse an empty input"). Add a random suffix, as unit-file_input does. Signed-off-by: Niels Lohmann --- tests/src/unit-class_parser.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/src/unit-class_parser.cpp b/tests/src/unit-class_parser.cpp index c1a86ca5f..c64a380cf 100644 --- a/tests/src/unit-class_parser.cpp +++ b/tests/src/unit-class_parser.cpp @@ -41,6 +41,7 @@ using nlohmann::json; #include #include #include +#include #include #include #include @@ -2875,13 +2876,16 @@ TEST_CASE("diagnostic positions: value lifetime, input adapters, and SAX") SECTION("std::ifstream: positions are consistent, not npos") { const std::string s = R"({"a":1,"b":2})"; + // the C++11 and C++20 builds of this test run in parallel in the same directory + std::random_device rd; + const std::string temp_name = "unit-class_parser_diagnostic_positions-" + std::to_string(rd()) + ".tmp"; { - std::ofstream file("unit-class_parser_diagnostic_positions.tmp"); + std::ofstream file(temp_name); file << s; } { - std::ifstream f("unit-class_parser_diagnostic_positions.tmp"); + std::ifstream f(temp_name); const json j = json::parse(f); CHECK(j.start_pos() == 0); @@ -2889,7 +2893,7 @@ TEST_CASE("diagnostic positions: value lifetime, input adapters, and SAX") CHECK(j["a"].start_pos() == 5); } - static_cast(std::remove("unit-class_parser_diagnostic_positions.tmp")); + static_cast(std::remove(temp_name.c_str())); } SECTION("iterator-pair input: positions are consistent, not npos")