Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated "test_all" target and Code Coverage #690

Merged
merged 6 commits into from
Dec 27, 2023

Conversation

program--
Copy link
Contributor

@program-- program-- commented Dec 22, 2023

This PR reworks the test_all target to automatically amalgamate all tests defined through ngen_add_test, and builds on #348 to add code coverage using gcovr instead of lcov. In addition to using gcovr, modifications were made to CodeCoverage.cmake to support using llvm-cov with gcov emulation.

Additions

  • CMake option NGEN_WITH_COVERAGE. Dependent on NGEN_WITH_TESTS, and allows building the ngen_coverage_* targets.
  • cmake/CodeCoverage.cmake module, which provides functions for generating HTML and XML code coverage documents.
  • Targets ngen_coverage_html and ngen_coverage_xml for HTML and XML documents respectively.
    The output files for these targets are outputted to:
    • <build_directory>/<target_name>/index.html
    • <build_directory>/<target_name>.xml.

Changes

  • Target test_all is now automatically generated through ngen_add_test rather than explicitly at the end of the test/CMakeLists.txt file.

Notes

  • When generating code coverage documents, the .gcda files are generated alongside the source object files in CMakeFiles/ directories. As a result, an out-of-source build is necessary, since it is not worth attempting to clean up these files in an in-source build. Moreover, to generate successive coverage documents, the gcov data files need to be deleted beforehand, so deleting the out-of-source build directory is the recommended approach.

  • If using a custom compiler with (e.g.) -DCMAKE_CXX_COMPILER=/path/to/bin/gcc-23, the path to the corresponding coverage analysis tool needs to be set with -DGCOV_PATH=/path/to/bin/gcov-23, or -DLLVM_COV_PATH=/directory/with/llvm/bin/llvm-cov-42 as appropriate

  • Example Coverage:

    image

Testing

  • HTML code coverage generated using GCC, Clang, and IntelLLVM.

Checklist

  • PR has an informative and human-readable title
  • Changes are limited to a single goal (no scope creep)
  • Code can be automatically merged (no conflicts)
  • Code follows project standards (link if applicable)
  • Passes all existing automated tests
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future todos are captured in comments
  • Project documentation has been updated (including the "Unreleased" section of the CHANGELOG)
  • Reviewers requested with the Reviewers tool ➡️

Target Environment support

  • Linux
  • MacOS

@program-- program-- added the build Issues related to CMake and building ngen label Dec 22, 2023
@program-- program-- changed the title Test Amalgamation and Code Coverage Automated "test_all" target and Code Coverage Dec 22, 2023
Copy link
Contributor

@PhilMiller PhilMiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this. It all looks good to me. I'll give it a quick run on macOS, too

CMakeLists.txt Outdated Show resolved Hide resolved
cmake/CodeCoverage.cmake Show resolved Hide resolved
Copy link
Contributor

@PhilMiller PhilMiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible/unreasonable to enable coverage analysis in the CI pipeline builds?

@PhilMiller
Copy link
Contributor

I just tested successfully on macOS with compilers from Homebrew. In addition to setting -DCMAKE_C_COMPILER=$(which gcc-13) -DCMAKE_CXX_COMPILER=$(which g++-13) -DCMAKE_Fortran_COMPILER=$(which gfortran-13), I had to set -DGCOV_PATH=$(which gcov-13) to match. The result without that, of running the system's /usr/bin/gcov is that it will silently run and produce output with no files or functions listed.

@PhilMiller
Copy link
Contributor

Once the gcov/llvm-cov selection logic is fixed, I'm enthusiastically +1 on this

@PhilMiller
Copy link
Contributor

Something to potentially enhance down the line would be to exclude irrelevant bits from the metrics. The worst offender is the Google Test source code in test/googletest/googletest, since we don't care that we use very little of it.

If it's easy to do that, it would be great to fold that in here

@PhilMiller
Copy link
Contributor

It may also make sense to not measure coverage of the test files as such, and only report on src/ and include/, but that's a mixed bag - if there's code in the tests that's not running, it should either be activated or deleted.

@program-- program-- self-assigned this Dec 26, 2023
@PhilMiller
Copy link
Contributor

I discovered in testing this on my Mac that the exclusions that are supposed to limit results to just src/ and include/ get confused if the paths involve a symlink. Building through absolute paths instead resolves that.

I'm trying to patch that by resolving the paths, but it's not working as desired.

@PhilMiller
Copy link
Contributor

There's maybe some race condition if one runs make -j 8 ngen_coverage_html de-novo in a freshly-configured tree, where the data seem not to be fully populated in the output. I'm not sure I can be bothered to care, but again, noting it here to document "don't hold it that way"

@PhilMiller
Copy link
Contributor

My remaining comments are probably fine to ignore. This achieves its purpose, and isn't otherwise making anything else around it worse. Let's just get it in, reap the benefits, and we can fix nitty-gritty issues as they become practical concerns.

PhilMiller
PhilMiller previously approved these changes Dec 26, 2023
@program--
Copy link
Contributor Author

program-- commented Dec 27, 2023

@PhilMiller I've modified the CMake module to include your suggestions, but particularly, removed all the language-specific flags and used add_compile_options/link_libraries as previously done. Should be good now 👍🏾

@PhilMiller PhilMiller merged commit 153c533 into NOAA-OWP:master Dec 27, 2023
19 checks passed
@PhilMiller PhilMiller mentioned this pull request Jan 9, 2024
11 tasks
@program-- program-- deleted the jsm-code-coverage branch February 12, 2024 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Issues related to CMake and building ngen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants