|
| 1 | +# ------------------------------------------------------------- |
| 2 | +# GitHub Actions Workflow: C++ Tests, Static Analysis, and Coverage |
| 3 | +# ------------------------------------------------------------- |
| 4 | +# Purpose: |
| 5 | +# Runs cppcheck (static analysis), builds your C++ project, |
| 6 | +# runs unit tests, and generates a coverage report. |
| 7 | +# ------------------------------------------------------------- |
| 8 | + |
| 9 | +name: C++ Tests and Coverage |
| 10 | + |
| 11 | +# ------------------------------------------------------------- |
| 12 | +# Triggers: |
| 13 | +# Run this workflow automatically whenever code is pushed |
| 14 | +# or a pull request is opened against the master branch. |
| 15 | +# ------------------------------------------------------------- |
| 16 | +on: |
| 17 | + push: |
| 18 | + branches: [master] |
| 19 | + pull_request: |
| 20 | + branches: [master] |
| 21 | + |
| 22 | +jobs: |
| 23 | + build-and-test: |
| 24 | + # --------------------------------------------------------- |
| 25 | + # The GitHub-hosted runner type to use (the host machine). |
| 26 | + # Ubuntu 24.04 provides Docker, Git, and build tools. |
| 27 | + # --------------------------------------------------------- |
| 28 | + runs-on: ubuntu-24.04 |
| 29 | + |
| 30 | + # --------------------------------------------------------- |
| 31 | + # Run all job steps inside this Docker container. |
| 32 | + # This ensures a consistent C++ build environment |
| 33 | + # with specific compiler and tool versions. |
| 34 | + # --------------------------------------------------------- |
| 35 | + container: |
| 36 | + image: urboob21/cpp-lab:latest |
| 37 | + |
| 38 | + steps: |
| 39 | + # ------------------------------------------------------- |
| 40 | + # Step 1: Checkout the repository source code |
| 41 | + # ------------------------------------------------------- |
| 42 | + - name: Checkout source |
| 43 | + uses: actions/checkout@v4 |
| 44 | + |
| 45 | + # ------------------------------------------------------- |
| 46 | + # Step 2: Run Cppcheck (Static Analysis) |
| 47 | + # - Scans for common C++ issues (style, memory, logic) |
| 48 | + # - You can adjust `--enable=` options as needed |
| 49 | + # ------------------------------------------------------- |
| 50 | + - name: Run static analysis with Cppcheck |
| 51 | + run: | |
| 52 | + echo "Running Cppcheck..." |
| 53 | + cppcheck --enable=warning,style,performance,portability \ |
| 54 | + --inconclusive \ |
| 55 | + --quiet \ |
| 56 | + --error-exitcode=1 \ |
| 57 | + ./src ./include |
| 58 | +
|
| 59 | + # ------------------------------------------------------- |
| 60 | + # Step 3: Configure and build the project |
| 61 | + # ------------------------------------------------------- |
| 62 | + - name: Prepare build |
| 63 | + run: | |
| 64 | + rm -rf build |
| 65 | + mkdir build |
| 66 | + cd build |
| 67 | + cmake .. |
| 68 | + cmake --build . |
| 69 | +
|
| 70 | + # ------------------------------------------------------- |
| 71 | + # Step 4: Run unit tests |
| 72 | + # TODO: need to update, I think we should not hard-code the executable name here |
| 73 | + # ------------------------------------------------------- |
| 74 | + - name: Run tests |
| 75 | + run: | |
| 76 | + ls -l |
| 77 | + ./build/cpp_lab_project_test |
| 78 | +
|
| 79 | + # ------------------------------------------------------- |
| 80 | + # Step 5: Generate code coverage report |
| 81 | + # ------------------------------------------------------- |
| 82 | + - name: Generate coverage report |
| 83 | + run: | |
| 84 | + lcov -d . -c -o coverage.info |
| 85 | + lcov -r coverage.info "*/build/*" "*/tests/*" "*/c++/*" -o coverageFiltered.info |
| 86 | + lcov --list coverageFiltered.info |
| 87 | +
|
| 88 | + # ------------------------------------------------------- |
| 89 | + # Step 6: Upload coverage summary to GitHub summary |
| 90 | + # ------------------------------------------------------- |
| 91 | + - name: Upload coverage to GitHub summary |
| 92 | + run: | |
| 93 | + echo "## Test Coverage Summary" >> $GITHUB_STEP_SUMMARY |
| 94 | + lcov --summary coverageFiltered.info >> $GITHUB_STEP_SUMMARY |
0 commit comments