Files
anki/tools/coverage/coverage-rust.bat
Fernando Lins a7f3ffb63a PoC: evaluate SonarCloud as a code quality and coverage tool (#4996)
## Linked issue

Closes #4995

## Summary / motivation

Bedges available:

[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=ankitects_anki&metric=coverage)](https://sonarcloud.io/summary/new_code?id=ankitects_anki)
[![Duplicated Lines
(%)](https://sonarcloud.io/api/project_badges/measure?project=ankitects_anki&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=ankitects_anki)
[![Code
Smells](https://sonarcloud.io/api/project_badges/measure?project=ankitects_anki&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=ankitects_anki)

[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=ankitects_anki&metric=bugs)](https://sonarcloud.io/summary/new_code?id=ankitects_anki)
[![Quality Gate
Status](https://sonarcloud.io/api/project_badges/measure?project=ankitects_anki&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=ankitects_anki)

[![Reliability](https://sonarcloud.io/api/project_badges/measure?project=ankitects_anki&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=ankitects_anki)

[![Security](https://sonarcloud.io/api/project_badges/measure?project=ankitects_anki&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=ankitects_anki)


**[SONAR
DASHBOARD](https://sonarcloud.io/project/overview?id=ankitects_anki)**

Integrates SonarCloud into the CI pipeline as a proof of concept to
evaluate whether
it surfaces actionable quality and security insights for this codebase.

Changes:
- Added `sonar-project.properties` configuring sources (`pylib`, `qt`,
`ts`, `rslib`)
  and coverage report paths
- Extended `tools/coverage/coverage-py` (and `.bat`) to emit
`coverage.xml` (Cobertura)
- Extended `tools/coverage/coverage-ts` (and `.bat`) to emit `lcov.info`
via the V8 provider
- Extended `tools/coverage/coverage-rust` (and `.bat`) to emit
`lcov.info` via
  `cargo-llvm-cov report`
- Added a `SonarCloud Scan` step to the `check-linux` CI job, running
after all
  checks pass and before the build cache is saved

## How to test

### Details

**1. Install sonar-scanner**
```bash
brew install sonar-scanner
```

**2. Generate and configure a token**

Go to [sonarcloud.io](https://sonarcloud.io/) → My Account → Security →
Generate Token
Copy the generated token and export it in your shell:
```
export SONAR_TOKEN=your_token_here
```

**3. Generate coverage reports**
```
just test --coverage
```
Expected output files:

 - out/coverage/python-pylib/coverage.xml
 - out/coverage/python-qt/coverage.xml
 - out/coverage/typescript/lcov.info
 - out/coverage/rust/lcov.info

**4. Run the scanner manually**
```
sonar-scanner
```
Results will appear in the SonarCloud dashboard

To test coverage generation locally:
```bash
just coverage
# verify files exist:
# out/coverage/python-pylib/coverage.xml
# out/coverage/python-qt/coverage.xml
# out/coverage/typescript/lcov.info
# out/coverage/rust/lcov.info
```
2026-08-26 15:48:45 -03:00

48 lines
1.6 KiB
Batchfile

@echo off
setlocal
rem cargo-llvm-cov's llvm-profdata uses the host toolchain, which is incompatible
rem with the profraw files produced when running on Windows ARM64.
if "%PROCESSOR_ARCHITECTURE%"=="ARM64" (
echo Rust coverage is not supported on Windows ARM64 ^(llvm-profdata format mismatch^).
echo Run on Linux or let CI enforce coverage.
exit /b 0
)
set "outdir=out\coverage\rust"
set "LLVMCOVPATH=out\bin"
if not exist %outdir% mkdir %outdir%
if "%CARGO_TARGET_DIR%"=="" set "CARGO_TARGET_DIR=out\rust"
if "%CI%"=="true" (
rem prebuilt binary shouldve been installed earlier
set "CARGO_CMD=cargo"
set PROFILE=ci
) else (
if not exist %LLVMCOVPATH% mkdir %LLVMCOVPATH%
if not exist %LLVMCOVPATH%\cargo-llvm-cov.exe (
cargo install cargo-llvm-cov --version 0.8.4 --locked --root out || exit /b 1
)
if not exist %LLVMCOVPATH%\cargo-nextest.exe (
cargo install cargo-nextest --version 0.9.99 --locked --no-default-features --features default-no-update --root out || exit /b 1
)
set "CARGO_CMD=%LLVMCOVPATH%\cargo-llvm-cov.exe"
set PROFILE=dev
)
set "PATH=%LLVMCOVPATH%;%PATH%"
set "ANKI_TEST_MODE=1"
"%CARGO_CMD%" llvm-cov nextest --workspace --locked ^
--cargo-profile %PROFILE% ^
--json --summary-only ^
--output-path %outdir%\coverage-summary.json --fail-under-lines 64 || exit /b 1
"%CARGO_CMD%" llvm-cov report --profile %PROFILE% --lcov --output-path %outdir%\lcov.info || exit /b 1
if "%1"=="--html" (
"%CARGO_CMD%" llvm-cov report --profile %PROFILE% --html --output-dir %outdir% || exit /b 1
echo Rust coverage report: %outdir%\html\index.html
)