Ignore non-JSON stdout preamble in catch_discover_tests (#3162)#3166
Open
ShivaPriyanShanmuga wants to merge 1 commit into
Open
Ignore non-JSON stdout preamble in catch_discover_tests (#3162)#3166ShivaPriyanShanmuga wants to merge 1 commit into
ShivaPriyanShanmuga wants to merge 1 commit into
Conversation
catch_discover_tests runs the test executable with --list-tests
--reporter json and parses the whole stdout as JSON. If a third-party
library writes to stdout from a static initializer, that text precedes
Catch2's JSON output and breaks the parser.
Strip everything before the first '{' (where Catch2's JSON starts)
before parsing, so such startup output is ignored. The registration
test now prints from a static initializer to cover this, and the
Python verifier skips the preamble the same way.
c8b5efb to
7991d1c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
catch_discover_tests()runs the test executable with--list-tests --reporter jsonand feeds the entire stdout into CMake'sstring(JSON ...). If anything writes to stdout beforemainruns - mostcommonly a third-party library logging from a static initializer - that text
precedes Catch2's JSON and the parser fails with:
string sub-command JSON failed parsing json string: * Line 1, Column 1
Syntax error: value, object or array expected.
Older versions used the plain
--list-teststext output, which tolerated suchnoise, so this is a regression people hit when upgrading.
Closes #3162.
How
Before parsing, strip everything before the first
{(where Catch2's JSONoutput begins) in
extras/CatchAddTests.cmake. If no{is present at all,fail with a clear error that includes the captured output.
This intentionally covers only the case described in the issue - output printed
before discovery starts (e.g. static-init constructors). It does not attempt
to handle output interleaved into the middle of the JSON.
Tests
Extended the existing
CMakeHelper::DiscoverTestsintegration test:register-tests.cppnow prints to stdout from a static initializer, so thediscovered executable reproduces the scenario.
VerifyRegistration.pyparses the executable's raw stdout directly, so itskips to the first
{the same way the CMake script does.Verified locally: the test fails on
devel(JSON syntax error duringdiscovery) and passes with this change (
5 tests matched).