Skip to content

Commit f75711f

Browse files
authored
Merge pull request #314 from korpling/local-merge-request-checks
Add script for local merge request checks
2 parents a3bfe99 + 46663fc commit f75711f

4 files changed

Lines changed: 76 additions & 54 deletions

File tree

.github/workflows/static_code_analysis.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Code Coverage
1+
name: Verify
22

33
on:
44
push:
@@ -7,13 +7,17 @@ on:
77
pull_request:
88

99
jobs:
10-
coverage:
11-
name: Execute tests with code coverage
10+
merge-request-checks:
11+
name: Run
1212
runs-on: ubuntu-latest
13-
env:
14-
CARGO_TERM_COLOR: always
1513
steps:
1614
- uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
- name: Install diff-cover
18+
uses: threeal/pipx-install-action@v1.0.0
19+
with:
20+
packages: diff_cover
1721
- uses: actions-rust-lang/setup-rust-toolchain@v1.4.4
1822
with:
1923
toolchain: stable
@@ -42,21 +46,24 @@ jobs:
4246
run: ./target/release/annis data --cmd 'import relannis/subtok.demo'
4347
- name: Install cargo-llvm-cov
4448
uses: taiki-e/install-action@cargo-llvm-cov
45-
- name: Remove old code coverage artifacts
46-
run: cargo llvm-cov clean --workspace
47-
- name: Run tests code coverage (standard tests)
48-
run: cargo llvm-cov --no-report --release
49-
env:
50-
RUSTFLAGS: "-C link-dead-code"
51-
- name: Run tests code coverage (ignored tests)
52-
run: cargo llvm-cov --no-report --release --tests -- --ignored
49+
- name: Run verify.sh
50+
run: ./verify.sh
51+
- name: Generate code coverage comment
52+
id: pr
53+
run: |
54+
{
55+
echo 'COVERAGE_INFO<<EOF'
56+
diff-cover target/llvm-cov/tests.lcov
57+
echo EOF
58+
} >> "$GITHUB_ENV"
5359
env:
54-
RUSTFLAGS: "-C link-dead-code"
55-
- name: Merge code coverage reports
56-
run: cargo llvm-cov report --ignore-filename-regex '(tests?\.rs)|(capi/.*)' --release --codecov --output-path codecov.json
57-
- name: Upload coverage to Codecov
58-
uses: codecov/codecov-action@v4
60+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
COMMENT_OUTPUT: TRUE
62+
- uses: mshick/add-pr-comment@v2
5963
with:
60-
token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
61-
files: codecov.json
62-
fail_ci_if_error: true
64+
message: |
65+
Commit ${{ github.event.pull_request.head.sha }}
66+
```
67+
${{env.COVERAGE_INFO}}
68+
```
69+

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,29 @@ Notable differences/enhancements compared to the thesis are:
2424
You need to install Rust to compile the project.
2525
We recommend installing the following Cargo subcommands for developing annis-web:
2626

27+
- [cargo-llvm-cov](https://crates.io/crates/cargo-llvm-cov) for determining the code coverage
2728
- [cargo-release](https://crates.io/crates/cargo-release) for creating releases
2829
- [cargo-about](https://crates.io/crates/cargo-about) for re-generating the
2930
third party license file
30-
- [cargo-llvm-cov](https://crates.io/crates/cargo-llvm-cov) for determining the code coverage
3131
- [cargo-dist](https://crates.io/crates/cargo-dist) for configuring the GitHub actions that create the release binaries.
3232

33+
Also install the [`diff-cover`](https://github.com/Bachmann1234/diff_cover) command line tool to run merge the merge request checks locally.
34+
3335
### Execute tests
3436

3537
You can run the tests with the default `cargo test` command.
36-
To calculate the code coverage, you can use `cargo-llvm-cov`:
38+
39+
To run all checks that have to pass for a merge requests locally, first make sure you have the `diff-cover` and `cargo-llvm-cov` tools installed.
40+
41+
```bash
42+
cargo install cargo-llvm-cov
43+
pipx install diff_cover
44+
```
45+
46+
Then, run the following command to run all checks including code coverage:
3747

3848
```bash
39-
cargo llvm-cov --open --all-features --ignore-filename-regex '(tests?\.rs)|(capi/.*)'
49+
./verify.sh
4050
```
4151

4252

verify.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# Stop the script if any command exits with a non-zero return code
4+
set -e
5+
6+
# Run static code checks
7+
cargo fmt --check
8+
cargo clippy
9+
10+
# Execute tests and calculate the code coverage both as lcov and HTML report
11+
cargo llvm-cov clean --workspace
12+
cargo llvm-cov --no-report --release
13+
cargo llvm-cov --no-report --release --tests -- --ignored
14+
mkdir -p target/llvm-cov/
15+
cargo llvm-cov report --ignore-filename-regex '(tests?\.rs)|(capi/.*)' --release --lcov --output-path target/llvm-cov/tests.lcov
16+
17+
# Use diff-cover (https://github.com/Bachmann1234/diff_cover) and output code coverage compared to main branch
18+
mkdir -p target/llvm-cov/html/
19+
OUTPUT="$(diff-cover target/llvm-cov/tests.lcov --html-report target/llvm-cov/html/patch.html)"
20+
echo "$OUTPUT"
21+
if [ -z "${CI}" ]; then
22+
echo "HTML report available at $PWD/target/llvm-cov/html/patch.html"
23+
fi
24+
25+
# Extract the code coverage percentage and exit with error code if threshold is not reached
26+
PERC_REGEX='.*Coverage: ([0-9]+)(\.[0-9]*)?\%.*'
27+
if [[ $OUTPUT =~ $PERC_REGEX ]]; then
28+
PERCENTAGE="$((${BASH_REMATCH[1]}))"
29+
if [[ $PERCENTAGE -lt 80 ]]
30+
then
31+
>&2 echo "Code coverage threshold not reached"
32+
exit 3
33+
fi
34+
fi
35+
exit 0

0 commit comments

Comments
 (0)