From 2cd566a17b06e3f3cc769186156acdd5f03f554f Mon Sep 17 00:00:00 2001 From: Stefano Cunego <93382903+kukkok3@users.noreply.github.com> Date: Wed, 27 Dec 2023 15:37:31 +0100 Subject: [PATCH] feat: UNIT_TEST UDC (#138) * feat: UNIT_TEST UDC target * fix: add llvm-tools to stable rust * fix: add cspell words * feat: add test target to rust example * fix: artifact path --- earthly/rust/Earthfile | 24 +++++++++++++++---- earthly/rust/scripts/std_build.sh | 4 ---- earthly/rust/stdcfgs/cargo_config.toml | 4 +--- examples/rust/.cargo/config.toml | 4 +--- examples/rust/Earthfile | 32 ++++++++++++++++++++++---- 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/earthly/rust/Earthfile b/earthly/rust/Earthfile index 29848415b..ef3264440 100644 --- a/earthly/rust/Earthfile +++ b/earthly/rust/Earthfile @@ -1,7 +1,7 @@ # Common Rust UDCs and Builders. VERSION 0.7 -# cspell: words rustup miri nextest ripgrep colordiff rustfmt stdcfgs toolset readelf depgraph +# cspell: words rustup miri nextest ripgrep colordiff rustfmt stdcfgs toolset readelf depgraph lcov # cspell: words TARGETPLATFORM TARGETOS TARGETARCH TARGETVARIANT USERPLATFORM USEROS USERARCH USERVARIANT # Base Rustup build container. @@ -28,11 +28,11 @@ rust-base: echo "USERPLATFORM = $USERPLATFORM"; \ echo "USEROS = $USEROS"; \ echo "USERARCH = $USERARCH"; \ - echo "USERVARIANT = $USERVARIANT"; + echo "USERVARIANT = $USERVARIANT"; WORKDIR /root - # Install necessary packages + # Install necessary packages # Expand this list as needed, rather than adding more tools in later containers. RUN apk add --no-cache \ musl-dev \ @@ -46,6 +46,9 @@ rust-base: # Make sure we have the clippy linter. RUN rustup component add clippy + # Needed to generate code coverage. + RUN rustup component add llvm-tools-preview + # Install a nightly toolchain which matches. RUN rustup toolchain install nightly --component miri --component rust-src --component rustfmt --component clippy @@ -61,7 +64,8 @@ rust-base: cargo install refinery_cli --version=0.8.11 && \ cargo install cargo-deny --version=0.14.3 && \ cargo install cargo-modules --version=0.10.2 && \ - cargo install cargo-depgraph --version=1.5.0 + cargo install cargo-depgraph --version=1.5.0 && \ + cargo install cargo-llvm-cov --version=0.5.39 SAVE ARTIFACT $CARGO_HOME/bin/refinery refinery @@ -75,7 +79,7 @@ rust-base: # Builds all the rust-base targets for each supported DOCKER architecture. # Currently only used for multi-platform cross build testing. -# This will ONLY work if you have `qemu` properly setup on linux and `rosetta` for +# This will ONLY work if you have `qemu` properly setup on linux and `rosetta` for # docker enabled on Mac. # Again, this is just a test target, and not for general use. rust-base-all-hosts: @@ -123,6 +127,16 @@ BUILD: RUN /scripts/std_build.sh "$libs" "$bins" +# Run unit tests and generates test and coverage report artifacts +UNIT_TEST: + COMMAND + # remove artifacts that may affect the coverage results + RUN cargo llvm-cov clean --workspace + # run unit tests and display test result and test coverage + RUN cargo llvm-cov nextest --release --bins --lib --workspace --locked -P ci + # generate lcov report + RUN cargo llvm-cov report --release --output-path coverage-report.info + # Check if the build executable, isn't a busted mess. SMOKE_TEST: COMMAND diff --git a/earthly/rust/scripts/std_build.sh b/earthly/rust/scripts/std_build.sh index 4b1c0b1b9..ffaa44abf 100755 --- a/earthly/rust/scripts/std_build.sh +++ b/earthly/rust/scripts/std_build.sh @@ -30,10 +30,6 @@ status $rc "Checking all Clippy Lints in the workspace" \ status $rc "Checking Documentation can be generated OK" \ cargo +nightly docs; rc=$? -## Check if all Self contained tests pass (Test that need no external resources). -status $rc "Checking Self contained Unit tests all pass" \ - cargo testunit; rc=$? - ## Check if all documentation tests pass. status $rc "Checking Documentation tests all pass" \ cargo +nightly testdocs; rc=$? diff --git a/earthly/rust/stdcfgs/cargo_config.toml b/earthly/rust/stdcfgs/cargo_config.toml index 2541bf49f..0eae0eb21 100644 --- a/earthly/rust/stdcfgs/cargo_config.toml +++ b/earthly/rust/stdcfgs/cargo_config.toml @@ -115,9 +115,7 @@ lint-vscode = "clippy --workspace --message-format=json-diagnostic-rendered-ansi docs = "doc --workspace -r --all-features --no-deps --bins --document-private-items --examples --locked" # nightly docs build broken... when they are'nt we can enable these docs... --unit-graph --timings=html,json -Z unstable-options" -testfast = "nextest run --release --workspace --locked" -testunit = "nextest run --release --bins --lib --workspace --locked -P ci" -testci = "nextest run --release --workspace --locked -P ci" +testfast = "nextest --release --workspace --locked" testdocs = "test --doc --release --workspace --locked" # Rust formatting, MUST be run with +nightly diff --git a/examples/rust/.cargo/config.toml b/examples/rust/.cargo/config.toml index 2541bf49f..0eae0eb21 100644 --- a/examples/rust/.cargo/config.toml +++ b/examples/rust/.cargo/config.toml @@ -115,9 +115,7 @@ lint-vscode = "clippy --workspace --message-format=json-diagnostic-rendered-ansi docs = "doc --workspace -r --all-features --no-deps --bins --document-private-items --examples --locked" # nightly docs build broken... when they are'nt we can enable these docs... --unit-graph --timings=html,json -Z unstable-options" -testfast = "nextest run --release --workspace --locked" -testunit = "nextest run --release --bins --lib --workspace --locked -P ci" -testci = "nextest run --release --workspace --locked -P ci" +testfast = "nextest --release --workspace --locked" testdocs = "test --doc --release --workspace --locked" # Rust formatting, MUST be run with +nightly diff --git a/examples/rust/Earthfile b/examples/rust/Earthfile index 4ac2d3dac..303574184 100644 --- a/examples/rust/Earthfile +++ b/examples/rust/Earthfile @@ -1,6 +1,6 @@ VERSION 0.7 -# cspell: words TARGETARCH USERARCH toolsets rustfmt +# cspell: words TARGETARCH USERARCH toolsets rustfmt nextest # Set up our target toolchains, and copy our files. builder: @@ -20,14 +20,14 @@ check-hosted: # Test which runs check with all supported host tooling. Needs qemu or rosetta to run. # Only used to validate tooling is working across host toolsets. -check-all-hosts: +check-all-hosts: BUILD --platform=linux/amd64 --platform=linux/arm64 +check-hosted # Build the service. build-hosted: FROM +builder - + DO ./../../earthly/rust+BUILD --libs="bar" --bins="foo/foo" DO ./../../earthly/rust+SMOKE_TEST --bin="foo" @@ -37,9 +37,17 @@ build-hosted: # Test which runs check with all supported host tooling. Needs qemu or rosetta to run. # Only used to validate tooling is working across host toolsets. -build-all-hosts: +build-all-hosts: BUILD --platform=linux/amd64 --platform=linux/arm64 +build-hosted +test-hosted: + FROM +builder + + DO ./../../earthly/rust+UNIT_TEST + + SAVE ARTIFACT target/nextest/ci/junit.xml junit-report.xml + SAVE ARTIFACT coverage-report.info coverage-report.info + ## ----------------------------------------------------------------------------- ## ## Standard CI targets. @@ -76,7 +84,21 @@ build: BUILD --platform=linux/amd64 +build-hosted END +test: + FROM busybox + # This is necessary to pick the correct architecture build to suit the native machine. + # It primarily ensures that Darwin/Arm builds work as expected without needing x86 emulation. + # All target implementation of this should follow this pattern. + ARG USERARCH + + IF [ "$USERARCH" == "arm64" ] + BUILD --platform=linux/arm64 +test-hosted + ELSE + BUILD --platform=linux/amd64 +test-hosted + END + # This step simulates the full CI run for local purposes only. local-ci-run: BUILD +check - BUILD +build \ No newline at end of file + BUILD +build + BUILD +test \ No newline at end of file