Skip to content

Commit 920f5a1

Browse files
Packit Buildsay-paul
authored andcommitted
ci(code-coverage): Add code coverage in ci
Use of llmv-cov to run test with code coerage. Add code coverage github page when push to mian. Added verb:test-coverage to Makefile to generate report locally. Signed-off-by: Packit Build <packit@fedoraproject.org>
1 parent 279f2f0 commit 920f5a1

3 files changed

Lines changed: 58 additions & 35 deletions

File tree

.github/workflows/ci.yml

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ jobs:
4040
container: fedora:latest
4141
steps:
4242
- uses: actions/checkout@v4
43-
- name: Cache
44-
uses: actions/cache@v3
45-
with:
46-
path: |
47-
~/.cargo/bin/
48-
~/.cargo/registry/index/
49-
~/.cargo/registry/cache/
50-
~/.cargo/git/db/
51-
target/
52-
key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }}
5343
- name: Install deps
5444
run: |
5545
dnf install -y make gcc git clippy cargo rust
@@ -58,42 +48,54 @@ jobs:
5848
command: clippy
5949
args: -- -D warnings -D clippy::panic -D clippy::todo
6050

61-
build_and_test:
51+
build_and_test_with_coverage:
52+
name: Build, test with coverage
6253
runs-on: ubuntu-latest
63-
container:
54+
container:
6455
image: fedora:latest
6556
options: --user root
57+
env:
58+
CARGO_TERM_COLOR: always
59+
LLVM_COV: /usr/bin/llvm-cov
60+
LLVM_PROFDATA: /usr/bin/llvm-profdata
6661
steps:
6762
- name: Install deps
6863
run: |
69-
dnf install -y make gcc git cargo rust git grub2-efi grub2-efi-modules shim
64+
dnf install -y make gcc git cargo rust git grub2-efi grub2-efi-modules shim llvm
7065
- uses: actions/checkout@v4
7166
with:
7267
persist-credentials: false
7368
- name: Fix git trust
7469
run: git config --global --add safe.directory /__w/greenboot-rs/greenboot-rs
75-
- name: Cache
76-
uses: actions/cache@v3
77-
with:
78-
path: |
79-
~/.cargo/bin/
80-
~/.cargo/registry/index/
81-
~/.cargo/registry/cache/
82-
~/.cargo/git/db/
83-
target/
84-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
70+
- name: Install cargo-llvm-cov
71+
run: cargo install cargo-llvm-cov
8572
- name: Build
86-
uses: actions-rs/cargo@v1
87-
with:
88-
command: build
89-
- name: Run tests with mock mounts
90-
uses: actions-rs/cargo@v1
73+
run: cargo build
74+
- name: Run tests with coverage
75+
run: |
76+
cargo llvm-cov test -- --test-threads=1
77+
cargo llvm-cov report --html --output-dir tests/coverage
78+
- name: Upload coverage report (HTML artifact)
79+
uses: actions/upload-artifact@v4
9180
with:
92-
command: test
93-
args: --features test-remount -- --test-threads=1
94-
- name: Run mount logic tests
95-
uses: actions-rs/cargo@v1
81+
name: coverage-report
82+
path: tests/coverage/
83+
retention-days: 14
84+
- name: Upload coverage report (GitHub Pages)
85+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
86+
uses: actions/upload-pages-artifact@v3
9687
with:
97-
command: test
98-
# matches 2 remount tests
99-
args: test_remount_boot
88+
path: tests/coverage/
89+
90+
deploy-pages:
91+
name: Deploy coverage to GitHub Pages
92+
needs: build_and_test
93+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
94+
runs-on: ubuntu-latest
95+
permissions:
96+
pages: write
97+
id-token: write
98+
environment: github-pages
99+
steps:
100+
- name: Deploy to GitHub Pages
101+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
debug/
88
target/
99
rpmbuild/
10+
tests/coverage/
11+
vendor/
1012

1113
Cargo.lock
1214
*.json

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ VERSION = $(shell grep -oP '^Version:\s+\K\S+' greenboot-rs.spec)
66
COMMIT = $(shell (cd "$(SRCDIR)" && git rev-parse --short HEAD))
77
TIMESTAMP = $(shell date +%Y%m%d%H%M%S)
88
PLATFORMS = $(shell (echo {x86_64,aarch64,powerpc64le,s390x}-unknown-linux-gnu))
9+
10+
LLVM_COV ?= /usr/bin/llvm-cov
11+
LLVM_PROFDATA ?= /usr/bin/llvm-profdata
12+
COVERAGE_OUTPUT_DIR ?= tests/coverage
13+
CARGO_LLVM_COV ?= $(shell command -v cargo-llvm-cov 2>/dev/null)
914
GREENBOOT_RUST_DEPENDENCIES = rust-anyhow+default-devel \
1015
rust-clap+derive-devel \
1116
rust-clap+default-devel \
@@ -83,6 +88,20 @@ build:
8388
check:
8489
cargo test "--target-dir=${TARGETDIR}" ${CARGO_ARGS}
8590

91+
.PHONY: test-coverage
92+
test-coverage:
93+
@if [ ! -x "$(LLVM_COV)" ] || [ ! -x "$(LLVM_PROFDATA)" ]; then \
94+
echo "ERROR: llvm-cov or llvm-profdata not found"; \
95+
echo " Install it with: sudo dnf install llvm"; \
96+
exit 1; \
97+
fi
98+
99+
@echo "Running test coverage (results will be in $(COVERAGE_OUTPUT_DIR))..."
100+
sudo LLVM_COV=$(LLVM_COV) LLVM_PROFDATA=$(LLVM_PROFDATA) cargo llvm-cov test -- --test-threads=1
101+
sudo LLVM_COV=$(LLVM_COV) LLVM_PROFDATA=$(LLVM_PROFDATA) cargo llvm-cov report --html --output-dir $(COVERAGE_OUTPUT_DIR)
102+
sudo chmod -R 777 $(COVERAGE_OUTPUT_DIR)
103+
@echo "Coverage report generated at $(COVERAGE_OUTPUT_DIR)/html/index.html"
104+
86105
.PHONY: fmt
87106
fmt:
88107
cargo fmt

0 commit comments

Comments
 (0)