Skip to content

ParseAndAddCatchTests: Ignore cmake object libraries#1601

Merged
horenmar merged 1 commit intocatchorg:masterfrom
v1tzl1:master
Apr 14, 2019
Merged

ParseAndAddCatchTests: Ignore cmake object libraries#1601
horenmar merged 1 commit intocatchorg:masterfrom
v1tzl1:master

Conversation

@v1tzl1
Copy link
Contributor

@v1tzl1 v1tzl1 commented Apr 14, 2019

Description

This PR will make the ParseAndAddCatchTests function ignore any "files" that match \$<TARGET_OBJECTS:.+>, so when you use cmake object libraries the script will not try to parse that "file" for the Catch2 TEST_CASE macros.

I am putting file in quotes here because in the case of object libraries this is clearly a cmake variable and not a file.

Why

I am using a cmake object library to compile the sources of my target binary and re-use them in my Catch2 unittests without recompiling them.

add_executable(mytest $<TARGET_OBJECTS:myobj> test_main.cc test1.cc test2.cc)
target_link_libraries(mytest Catch2::Catch2)

include(CTest)
include(ParseAndAddCatchTests)
ParseAndAddCatchTests(mytest)

This will give the following warnings (PARSE_CATCH_TESTS_VERBOSE is set during cmake configuration)

-- ParseAndAddCatchTests: Started parsing mytest
-- ParseAndAddCatchTests: Found the following sources: $<TARGET_OBJECTS:myobjobj>;test_main.cc;test_1.cc;test_2.cc
CMake Warning at /usr/local/lib64/cmake/Catch2/ParseAndAddCatchTests.cmake:98 (message):
  Cannot find source file:
  /home/v1tzl1/myproj/tests/$<TARGET_OBJECTS:myobj>
Call Stack (most recent call first):
  /usr/local/lib64/cmake/Catch2/ParseAndAddCatchTests.cmake:221 (ParseAndAddCatchTests_ParseFile)
  tests/CMakeLists.txt:18 (ParseAndAddCatchTests)
-- ParseAndAddCatchTests: parsing /home/v1tzl1/myproj/tests/test_main.cc
-- ParseAndAddCatchTests: parsing /home/v1tzl1/myproj/tests/test_1.cc
-- ParseAndAddCatchTests: Adding test "mytest:Test 1"
-- ParseAndAddCatchTests: parsing /home/v1tzl1/myproj/tests/test_2.cc
-- ParseAndAddCatchTests: Adding test "mytest:Test 2"
-- ParseAndAddCatchTests: Setting labels to mytest
-- ParseAndAddCatchTests: Finished parsing mytest

While the parser will keep working and finding all my tests, I don't want to have warnings in my cmake runs. With the proposed change the output will look like this:

-- ParseAndAddCatchTests: Started parsing mytest
-- ParseAndAddCatchTests: Found the following sources: $<TARGET_OBJECTS:myobjobj>;test_main.cc;test_1.cc;test_2.cc
-- ParseAndAddCatchTests: Detected OBJECT library: $<TARGET_OBJECTS:myobj> this will not be scanned for tests.
-- ParseAndAddCatchTests: parsing /home/v1tzl1/myproj/tests/test_main.cc
-- ParseAndAddCatchTests: parsing /home/v1tzl1/myproj/tests/test_1.cc
-- ParseAndAddCatchTests: Adding test "mytest:Test 1"
-- ParseAndAddCatchTests: parsing /home/v1tzl1/myproj/tests/test_2.cc
-- ParseAndAddCatchTests: Adding test "mytest:Test 2"
-- ParseAndAddCatchTests: Setting labels to mytest
-- ParseAndAddCatchTests: Finished parsing mytest

Note that this does not change the behaviour of Catch2. Before it ignored the "file" because it could not find it on the file system, now it skips all files that look like object library variables before checking that the remaining files are actually there. There is also an output to ParseAndAddCatchTests_PrintDebugMessage so the behaviour can be traced if there ever is a problem with this.

Possible side effects

With this change, any tests defined in a file which's name matches \$<TARGET_OBJECTS:.+> will no longer be detected.

I have tested these changes locally with cmake 3.9.6 on Linux, but since the change is minimal there should not be much issues with other systems. For the sake of completeness I also ran the tests described in /docs/contributing.md which all passed.

Edit: removed some double copy lines from the output and fixed some typos.

@codecov
Copy link

codecov bot commented Apr 14, 2019

Codecov Report

Merging #1601 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #1601   +/-   ##
=======================================
  Coverage   80.72%   80.72%           
=======================================
  Files         121      121           
  Lines        3424     3424           
=======================================
  Hits         2764     2764           
  Misses        660      660

1 similar comment
@codecov
Copy link

codecov bot commented Apr 14, 2019

Codecov Report

Merging #1601 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #1601   +/-   ##
=======================================
  Coverage   80.72%   80.72%           
=======================================
  Files         121      121           
  Lines        3424     3424           
=======================================
  Hits         2764     2764           
  Misses        660      660

@horenmar horenmar added the Extras Touches utility scripts outside of Catch2 proper, e.g. CMake integration. label Apr 14, 2019
@horenmar
Copy link
Member

Thanks, the changes look good to me.

@horenmar horenmar merged commit ac94bd0 into catchorg:master Apr 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Extras Touches utility scripts outside of Catch2 proper, e.g. CMake integration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments