diff options
-rw-r--r-- | .github/workflows/ci.yml | 30 | ||||
-rw-r--r-- | noxfile.py | 75 |
2 files changed, 80 insertions, 25 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 20229b2b1..a92223a3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,7 @@ on: branches: - main - '*.*.x' + - coverage-nox tags: - '*.*' - '*.*.*' @@ -306,41 +307,20 @@ jobs: uses: ./.github/actions/wycheproof - run: python -m pip install -c ci-constraints-requirements.txt 'nox' coverage[toml] cffi - name: Create nox environment - run: nox -v --install-only -s tests + run: nox -v --install-only -s rust_coverage env: CARGO_TARGET_DIR: ${{ format('{0}/src/rust/target/', github.workspace) }} - RUSTFLAGS: "-Cinstrument-coverage" - LLVM_PROFILE_FILE: "rust-cov/cov-%p.profraw" - name: Tests - run: nox --no-install -s tests -- --color=yes --wycheproof-root=wycheproof + run: nox --no-install -s rust_coverage -- --color=yes --wycheproof-root=wycheproof env: COLUMNS: 80 CARGO_TARGET_DIR: ${{ format('{0}/src/rust/target/', github.workspace) }} - RUSTFLAGS: "-Cinstrument-coverage" - LLVM_PROFILE_FILE: "rust-cov/cov-%p.profraw" - - name: Rust Tests - run: | - cd src/rust - cargo test --no-default-features --all - env: - RUSTFLAGS: "-Cinstrument-coverage" - LLVM_PROFILE_FILE: "rust-cov/cov-%m-%p.profraw" - name: Process coverage data run: | set -xe - cd src/rust/ - cargo profdata -- merge -sparse $(find ../.. -iname "*.profraw") -o rust-cov.profdata + sed -E -i 's/SF:(.*)\/src\/rust\/(.*)/SF:src\/rust\/\2/g' cov.lcov COV_UUID=$(python3 -c "import uuid; print(uuid.uuid4())") - - cargo cov -- export \ - ../../.nox/tests/lib/python${{ matrix.PYTHON }}/site-packages/cryptography/hazmat/bindings/_rust.abi3.so \ - $(env RUSTFLAGS="-Cinstrument-coverage" cargo test --no-default-features --all --tests --no-run --message-format=json | jq -r "select(.profile.test == true) | .filenames[]" | awk '{print "-object " $0}') \ - -instr-profile=rust-cov.profdata \ - --ignore-filename-regex='/.cargo/' \ - --ignore-filename-regex='/rustc/' \ - --ignore-filename-regex='/.rustup/toolchains/' --format=lcov > "../../${COV_UUID}.lcov" - - sed -E -i 's/SF:(.*)\/src\/rust\/(.*)/SF:src\/rust\/\2/g' "../../${COV_UUID}.lcov" + mv cov.lcov "${COV_UUID}.lcov" - uses: ./.github/actions/upload-coverage macos: diff --git a/noxfile.py b/noxfile.py index 8f1b94a50..b742a2f47 100644 --- a/noxfile.py +++ b/noxfile.py @@ -4,6 +4,9 @@ from __future__ import annotations +import glob +import json + import nox nox.options.reuse_existing_virtualenvs = True @@ -144,3 +147,75 @@ def rust(session: nox.Session) -> None: session.run("cargo", "fmt", "--all", "--", "--check", external=True) session.run("cargo", "clippy", "--", "-D", "warnings", external=True) session.run("cargo", "test", "--no-default-features", external=True) + + +@nox.session +def rust_coverage(session: nox.Session) -> None: + session.env.update( + { + "RUSTFLAGS": "-Cinstrument-coverage", + "LLVM_PROFILE_FILE": "rust-cov/cov-%p.profraw", + } + ) + # cryptography will be install command in the tests function + tests(session) + + with session.chdir("src/rust/"): + session.run("cargo", "test", "--no-default-features", external=True) + paths = glob.glob("**/*.profraw", recursive=True) + session.run( + "cargo", + "profdata", + "--", + "merge", + "-sparse", + *paths, + "-o", + "rust-cov.profdata", + external=True, + ) + + json_blobs = session.run( + "cargo", + "test", + "--no-default-features", + "--all", + "--tests", + "--no-run", + "-q", + "--message-format=json", + external=True, + silent=True, + ) + filenames = [] + if json_blobs: + assert isinstance(json_blobs, str) + for blob in json_blobs.splitlines(): + data = json.loads(blob) + try: + if data["profile"]["test"]: + assert len(data["filenames"]) == 1 + filenames.append("-object") + filenames.extend(data["filenames"]) + except KeyError: + pass + rust_so = glob.glob( + f"{session.virtualenv.location}/lib/**/site-packages" + f"/cryptography/hazmat/bindings/_rust_abi3.so" + ) + with open("../../cov.lcov", "wb") as f: + session.run( + "cargo", + "cov", + "--", + "export", + *rust_so, + *filenames, + "-instr-profile=rust-cov.profdata", + "--ignore-filename-regex='/.cargo/'", + "--ignore-filename-regex='/rustc/'", + "--ignore-filename-regex='/.rustup/toolchains/'", + "--format=lcov", + external=True, + stdout=f, + ) |