Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions extras/CatchAddTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ function(catch_discover_tests_impl)
endforeach()
endif()

# The test executable might print to stdout before Catch2 emits its JSON,
# e.g. a third-party library logging from a static initializer. Such text
# would break the JSON parser, so we strip everything before the first '{',
# which is where Catch2's JSON output starts.
string(FIND "${listing_output}" "{" json_start)
if(json_start EQUAL -1)
message(FATAL_ERROR
"Could not find the start of JSON output when listing tests from executable '${_TEST_EXECUTABLE}':\n"
" Output: ${listing_output}\n"
)
endif()
string(SUBSTRING "${listing_output}" ${json_start} -1 listing_output)

# Parse JSON output for list of tests/class names/tags
string(JSON version GET "${listing_output}" "version")
if(NOT version STREQUAL "1")
Expand Down
7 changes: 6 additions & 1 deletion tests/TestScripts/DiscoverTests/VerifyRegistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def get_test_names(build_path: str) -> List[TestInfo]:
check = True,
text = True)

test_listing = json.loads(result.stdout)
# The executable might print to stdout before Catch2's JSON output, e.g.
# from a third-party library's static initializer, so we skip everything
# before the first '{', just like CatchAddTests.cmake does.
json_start = result.stdout.find('{')
assert json_start != -1, f"Could not find JSON output in:\n{result.stdout}"
test_listing = json.loads(result.stdout[json_start:])

assert test_listing['version'] == 1

Expand Down
14 changes: 14 additions & 0 deletions tests/TestScripts/DiscoverTests/register-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

#include <catch2/catch_test_macros.hpp>

#include <iostream>

namespace {
// Emulate a third-party library that writes to stdout from a static
// initializer. This text precedes Catch2's JSON output during test
// discovery, and `catch_discover_tests` has to ignore it. See #3162.
struct PrintsDuringStaticInit {
PrintsDuringStaticInit() {
std::cout << "Some third-party library started up successfully\n";
}
};
const PrintsDuringStaticInit printsDuringStaticInit{};
}

TEST_CASE("@Script[C:\\EPM1A]=x;\"SCALA_ZERO:\"", "[script regressions]"){}
TEST_CASE("Some test") {}
TEST_CASE( "Let's have a test case with a long name. Longer. No, even longer. "
Expand Down