Skip to content

Commit

Permalink
Merge pull request #29 from TomVer99/feature/mem-leak-check
Browse files Browse the repository at this point in the history
feature/mem-leak-check
  • Loading branch information
TomVer99 authored Jun 8, 2024
2 parents 1b39036 + 7f98e66 commit 7857992
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 30 deletions.
189 changes: 164 additions & 25 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,185 @@ env:
SKIP_CI: false

jobs:
check:
build:
runs-on: ubuntu-latest
outputs:
skip-ci: ${{ env.SKIP_CI }}
env:
BUILD_TYPE: Debug
steps:
- id: name-check
name: Check PR name
run: |
if [[ "${{ github.event.pull_request.title }}" =~ ^doc\/.*$|^docs\/.*$|^no-ci\/.*$ ]]; then
echo "Skipping CI for documentation-only PR"
echo "SKIP_CI=true" >> $GITHUB_ENV
exit 0
fi
build_debug_test_report:
needs: check
if: ${{ needs.check.outputs.skip-ci == 'false' }}
runs-on: ubuntu-latest
- uses: actions/checkout@v4

- name: configure
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: build
path: ${{github.workspace}}/build

test:
needs: build
runs-on: ubuntu-latest
env:
BUILD_TYPE: Debug
steps:
- uses: actions/checkout@v4

- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: build
path: ${{github.workspace}}/build

- name: Just make everything have every permission
run: chmod -R +x ${{github.workspace}}/build/*

- name: configure
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -j18 -C ${{env.BUILD_TYPE}} -T test --output-on-failure --progress --output-log testReport.txt

- name: Report test by adding a comment
if: ${{ success() || failure() }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const issueNumber = context.issue.number;
const repo = context.repo;
const testReport = fs.readFileSync('./build/testReport.txt');
const commit_sha = context.payload.pull_request.head.sha;
const commit_sha_short = commit_sha.substr(0, 7);
const body_string = `### Test report of commit [<code>${commit_sha_short}</code>](${context.payload.pull_request.number}/commits/${commit_sha}) during [${context.workflow} #${context.runNumber}](../actions/runs/${context.runId})\n\n\`\`\`\n${testReport}\n\`\`\``;
// Get all the comments on the issue
const comments = await github.rest.issues.listComments({
...repo,
issue_number: issueNumber,
});
// Loop through all the comments and check if '### Test report' is on the first line
const existingComment = comments.data.find((comment) => comment.body.startsWith('### Test report'));
// If the comment already exists, update it
if (existingComment) {
await github.rest.issues.updateComment({
...repo,
comment_id: existingComment.id,
body: body_string,
});
return;
// If the comment does not exist, create it
} else {
await github.rest.issues.createComment({
...repo,
issue_number: issueNumber,
body: body_string,
});
}
- name: Overwrite build artifact with test results
uses: actions/upload-artifact@v4
with:
name: test
path: ${{github.workspace}}/build

memory:
needs: test
runs-on: ubuntu-latest
env:
BUILD_TYPE: Debug
steps:
- uses: actions/checkout@v4

- name: Install dependencies
run: sudo apt-get install lcov
run: sudo apt-get install -y valgrind

- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: test
path: ${{github.workspace}}/build

- name: Just make everything have every permission
run: chmod -R +x ${{github.workspace}}/build/*

- name: configure
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test

- name: Memory leaks
working-directory: ${{github.workspace}}/build
run: ctest -j18 -C ${{env.BUILD_TYPE}} -T test --output-on-failure --progress
run: ctest -D ExperimentalMemCheck ${{env.BUILD_TYPE}} --output-on-failure --fail-on-leaks --output-log memReport.txt

- name: Report valgrind by adding a comment
if: ${{ success() || failure() }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const issueNumber = context.issue.number;
const repo = context.repo;
const memReport = fs.readFileSync('./build/memReport.txt');
const commit_sha = context.payload.pull_request.head.sha;
const commit_sha_short = commit_sha.substr(0, 7);
const body_string = `### Valgrind report of commit [<code>${commit_sha_short}</code>](${context.payload.pull_request.number}/commits/${commit_sha}) during [${context.workflow} #${context.runNumber}](../actions/runs/${context.runId})\n\n\`\`\`\n${memReport}\n\`\`\``;
// Get all the comments on the issue
const comments = await github.rest.issues.listComments({
...repo,
issue_number: issueNumber,
});
// Loop through all the comments and check if '### Valgrind report' is on the first line
const existingComment = comments.data.find((comment) => comment.body.startsWith('### Valgrind report'));
// If the comment already exists, update it
if (existingComment) {
await github.rest.issues.updateComment({
...repo,
comment_id: existingComment.id,
body: body_string,
});
return;
// If the comment does not exist, create it
} else {
await github.rest.issues.createComment({
...repo,
issue_number: issueNumber,
body: body_string,
});
}
coverage:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install dependencies
run: sudo apt-get install -y lcov

- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: test
path: ${{github.workspace}}/build

- name: Just make everything have every permission
run: chmod -R +x ${{github.workspace}}/build/*

- name: Coverage
run: make -C ${{github.workspace}}/build coverage
working-directory: ${{github.workspace}}/build
run: make coverage

- name: Setup LCOV
uses: hrishikesh-kadam/setup-lcov@v1
Expand Down
22 changes: 17 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ set(CMAKE_CXX_STANDARD 20) # C++ standard is C++20
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(CMAKE_COMPILER_IS_GNUCXX)
add_compile_options(-Wall -Wextra -Wpedantic -fprofile-arcs -ftest-coverage)
link_libraries(gcov)
add_compile_options(-Wall -Wextra -Wpedantic)
# add_compile_options(-Werror)
elseif(MSVC)
add_compile_options(/W4)
# add_compile_options(/WX)
endif()

# Testing
# Test options
if(BUILD_TESTING)
set(MEMORYCHECK_COMMAND_OPTIONS "--show-leak-kinds=all --leak-check=full --error-exitcode=1")
endif()

include(CTest)
enable_testing()

# Test options
if(BUILD_TESTING)
add_compile_options(-Wall -Wextra -Wpedantic -fprofile-arcs -ftest-coverage)
link_libraries(gcov)
set(MEMORYCHECK_COMMAND valgrind)
endif()

# Code coverage
set(COVERAGE_REPORT_DIR ${CMAKE_SOURCE_DIR}/report)
file(MAKE_DIRECTORY ${COVERAGE_REPORT_DIR})
Expand All @@ -32,9 +47,6 @@ add_custom_target(coverage
COMMAND genhtml ${COVERAGE_REPORT_DIR}/coverage.info --output-directory ${COVERAGE_REPORT_DIR}/html
)

include(CTest)
enable_testing()

include(FetchContent)

# get google test
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
![GitHub Actions](https://img.shields.io/badge/github%20actions-%2300599C.svg?style=for-the-badge&logo=githubactions&logoColor=white)
![Google Test](https://img.shields.io/badge/google%20test-%2300599C.svg?style=for-the-badge&logo=google&logoColor=white)
![lcov](https://img.shields.io/badge/lcov-%2300599C.svg?style=for-the-badge&logo=gnu&logoColor=white)
![valgrind](https://img.shields.io/badge/valgrind-%2300599C.svg?style=for-the-badge&logo=valgrind&logoColor=white)

---

Expand Down Expand Up @@ -35,9 +36,12 @@ It includes:
* Code coverage analysis
* Fails under 90%
* Only counts /src/ files
* Valgrind memory leak detection
* Fails if memory leaks are detected

## Requirements

* CMake
* GCC
* lcov
* Valgrind

0 comments on commit 7857992

Please sign in to comment.