Repository navigation
Refactor/cpp lab overhaul #149
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
| # ------------------------------------------------------------- | |
| # GitHub Actions Workflow: C++ Tests, Static Analysis, and Coverage | |
| # ------------------------------------------------------------- | |
| # Purpose: | |
| # Runs cppcheck (static analysis), builds your C++ project, | |
| # runs the unit tests and one smoke test per example, | |
| # generates a coverage report, and repeats the tests with | |
| # AddressSanitizer + UndefinedBehaviorSanitizer. | |
| # ------------------------------------------------------------- | |
| name: C++ Tests and Coverage | |
| # ------------------------------------------------------------- | |
| # Triggers: | |
| # Run this workflow automatically whenever code is pushed | |
| # or a pull request is opened against the master branch. | |
| # ------------------------------------------------------------- | |
| on: | |
| push: | |
| branches: [master] | |
| pull_request: | |
| branches: [master] | |
| jobs: | |
| build-and-test: | |
| # --------------------------------------------------------- | |
| # The GitHub-hosted runner type to use (the host machine). | |
| # Ubuntu 24.04 provides Docker, Git, and build tools. | |
| # --------------------------------------------------------- | |
| runs-on: ubuntu-24.04 | |
| # --------------------------------------------------------- | |
| # Run all job steps inside this Docker container. | |
| # This ensures a consistent C++ build environment | |
| # with specific compiler and tool versions. | |
| # --------------------------------------------------------- | |
| container: | |
| image: urboob21/cpp-lab:latest | |
| steps: | |
| # ------------------------------------------------------- | |
| # Step 1: Checkout the repository source code | |
| # ------------------------------------------------------- | |
| - name: Checkout source | |
| uses: actions/checkout@v4 | |
| # ------------------------------------------------------- | |
| # Step 2: Run Cppcheck (Static Analysis) | |
| # - Scans for common C++ issues (style, memory, logic) | |
| # - -I include lets cppcheck understand the LAB_EXAMPLE macro | |
| # - https://cppcheck.sourceforge.io/manual.pdf | |
| # ------------------------------------------------------- | |
| - name: Run static analysis with Cppcheck | |
| run: | | |
| echo "Running Cppcheck..." | |
| cppcheck --enable=warning,style,performance,portability \ | |
| --inconclusive \ | |
| --quiet \ | |
| --inline-suppr \ | |
| --error-exitcode=1 \ | |
| -I include \ | |
| --suppressions-list=.cppcheck-suppressions \ | |
| ./src ./include | |
| # ------------------------------------------------------- | |
| # Step 3: Configure and build the project | |
| # ------------------------------------------------------- | |
| - name: Build | |
| run: | | |
| cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=ON | |
| cmake --build build -j "$(nproc)" | |
| # ------------------------------------------------------- | |
| # Step 4: Run unit tests and example smoke tests | |
| # ------------------------------------------------------- | |
| - name: Run tests | |
| run: ctest --test-dir build --output-on-failure -j "$(nproc)" | |
| # ------------------------------------------------------- | |
| # Step 5: Generate code coverage report | |
| # ------------------------------------------------------- | |
| - name: Generate coverage report | |
| run: | | |
| cd build | |
| lcov --rc geninfo_unexecuted_blocks=1 --ignore-errors mismatch -d . -c -o coverage.info | |
| lcov -r coverage.info "*/_deps/*" "/usr/*" "*/tests/*" -o coverageFiltered.info | |
| lcov --list coverageFiltered.info | |
| # ------------------------------------------------------- | |
| # Step 6: Upload coverage summary to GitHub summary | |
| # ------------------------------------------------------- | |
| - name: Upload coverage to GitHub summary | |
| run: | | |
| cd build | |
| echo "## Test Coverage Summary" >> $GITHUB_STEP_SUMMARY | |
| lcov --summary coverageFiltered.info >> $GITHUB_STEP_SUMMARY | |
| # # ------------------------------------------------------- | |
| # # Step 7: Run Clang-Tidy (experimenting, not enforced) | |
| # # - Modern C++ static analysis, configured by .clang-tidy | |
| # # ------------------------------------------------------- | |
| # - name: Run clang-tidy | |
| # run: | | |
| # clang-tidy -p build -header-filter='^src/.*' $(find src -name "*.cpp") | |
| release: | |
| # --------------------------------------------------------- | |
| # A Release build with warnings as errors: optimizations enable extra | |
| # compiler diagnostics that a Debug build never reports. | |
| # --------------------------------------------------------- | |
| runs-on: ubuntu-24.04 | |
| container: | |
| image: urboob21/cpp-lab:latest | |
| steps: | |
| - name: Checkout source | |
| uses: actions/checkout@v4 | |
| - name: Build and test in Release | |
| run: | | |
| cmake -S . -B build-release -DCMAKE_BUILD_TYPE=Release \ | |
| -DCPPLAB_WARNINGS_AS_ERRORS=ON -DCPPLAB_BUILD_GUI=OFF | |
| cmake --build build-release -j "$(nproc)" | |
| ctest --test-dir build-release --output-on-failure -j "$(nproc)" | |
| sanitizers: | |
| # --------------------------------------------------------- | |
| # Same tests with AddressSanitizer + UndefinedBehaviorSanitizer: | |
| # memory errors, leaks and undefined behavior fail the job. | |
| # --------------------------------------------------------- | |
| runs-on: ubuntu-24.04 | |
| container: | |
| image: urboob21/cpp-lab:latest | |
| steps: | |
| - name: Checkout source | |
| uses: actions/checkout@v4 | |
| - name: Build with sanitizers | |
| run: | | |
| cmake -S . -B build-asan -DCMAKE_BUILD_TYPE=Debug -DCPPLAB_ENABLE_SANITIZERS=ON -DCPPLAB_BUILD_GUI=OFF | |
| cmake --build build-asan -j "$(nproc)" | |
| - name: Run tests with sanitizers | |
| run: ctest --test-dir build-asan --output-on-failure -j "$(nproc)" |