Runs the tests of a Rust project with cargo test or
cargo-nextest. On request, it measures line
coverage with
cargo-llvm-cov, writes
JUnit XML, lcov and Cobertura reports, and uploads them as one
workflow artefact, also when tests fail.
The action takes no secrets and needs no id-token permission. It
runs the tests, and everything else that executes project code,
without the registry, OIDC and runtime credentials or the runner's
command files listed under Security. The artefact upload
step, which runs actions/upload-artifact, is the one step that uses
the runner's own token.
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: "Test Rust project"
id: test
# Pin to a commit SHA
uses: lfreleng-actions/rust-test-action@<commit-sha>
with:
test_runner: "nextest"
coverage: "true"
- name: "Show the coverage"
shell: bash
env:
COVERAGE: ${{ steps.test.outputs.coverage_percent }}
run: echo "Line coverage: ${COVERAGE}%"In a matrix, give each leg its own artefact_name, for example
rust-test-results-${{ matrix.os }}. Two uploads with the same name
in one workflow run conflict, and the second fails.
| Name | Required | Default | Description |
|---|---|---|---|
| path_prefix | False | . |
Directory holding the project; must resolve inside the workspace |
| manifest_path | False | Cargo.toml |
Path to Cargo.toml, relative to path_prefix; must not be a symlink |
| workspace | False | true |
Test every workspace member (--workspace); a non-empty packages replaces it |
| packages | False | Whitespace-separated packages to test (-p) |
|
| exclude | False | Whitespace-separated packages to leave out; needs workspace and no packages |
|
| features | False | Features to enable, separated by whitespace or commas | |
| all_features | False | false |
Enable every feature (--all-features) |
| no_default_features | False | false |
Disable the default features (--no-default-features) |
| toolchain | False | rustup toolchain to use; empty uses the one rustup selects for the project | |
| toolchain_components | False | rustup components to install into the toolchain, separated by whitespace or commas | |
| toolchain_targets | False | Target triples to install into the toolchain, separated by whitespace or commas | |
| lockfile_required | False | false |
Fail when Cargo.lock is missing, rather than generating one with a warning |
| setup_script | False | Script, relative to path_prefix, run with bash from path_prefix before testing |
|
| test_runner | False | cargo |
cargo (cargo test) or nextest (cargo nextest run) |
| nextest_version | False | 0.9.146 |
cargo-nextest version to install for the nextest runner |
| test_args | False | Extra runner arguments, split on spaces and tabs, never run through a shell | |
| doc_tests | False | true |
Run doc tests |
| coverage | False | false |
Run the tests under cargo-llvm-cov and write lcov and Cobertura reports |
| llvm_cov_version | False | 0.9.1 |
cargo-llvm-cov version to install for coverage |
| junit | False | Write JUnit XML (true/false); empty means true for nextest, false for cargo |
|
| artefact_upload | False | true |
Upload the JUnit and coverage reports as a workflow artefact |
| artefact_name | False | rust-test-results |
Name of the report artefact; give each matrix leg its own |
| artefact_path | False | Report directory, relative to path_prefix; see Reports |
|
| permit_fail | False | false |
Report success with a warning when tests fail; see Notes for the scope |
| summary | False | true |
Write a results table to the job summary |
Boolean inputs accept true or false and nothing else.
| Name | Description |
|---|---|
| toolchain | Toolchain used: a rustup channel, a path, or empty without rustup |
| toolchain_kind | How the action found the toolchain: channel, path or none |
| cargo_version | Cargo version that ran the tests; the action fails when it cannot read it |
| rustc_version | rustc version that built the tests; the action fails when it cannot read it |
| tests_outcome | passed or failed; failed also when the action stopped before the tests |
| coverage_percent | Line coverage to two decimals, such as 87.50; empty without coverage |
| junit_path | Absolute path of the JUnit XML report; empty when none |
| coverage_lcov_path | Absolute path of the lcov report; empty when none |
| coverage_cobertura_path | Absolute path of the Cobertura XML report; empty when none |
| artefact_name | Name of the uploaded artefact; empty when the action uploaded nothing |
| artefact_path | Absolute path of the report directory; empty when stopped before the tests |
A first step checks every input, then taiki-e/install-action installs cargo-nextest and cargo-llvm-cov when the run needs them. It installs the prebuilt, checksummed binaries that its pinned release lists, and fails rather than building a tool from source. The test step then runs these stages:
- Check toolchain. With an empty
toolchain,rustup show active-toolchainnames the toolchain thatpath_prefixselects, without running it. The action pins a channel throughRUSTUP_TOOLCHAINfor every later call. A path-based toolchain runs unpinned frompath_prefix, with a warning. Without rustup, the action uses thecargoonPATHand reportstoolchain_kindasnone. ARUSTUP_TOOLCHAINin the job environment outranksrust-toolchain.toml, as in rustup; the action reports and uses that toolchain. - Install toolchain. Installs a missing channel toolchain named
by
toolchain, and thetoolchain_componentsandtoolchain_targetsfor any channel; see Toolchain components and targets. The action then reads the Cargo and rustc versions, and fails when either--versioncommand fails or prints something other than a Rust release number. - Run setup script. Runs
setup_scriptwith bash frompath_prefix, for example to install the native libraries that-syscrates need. - Check lockfile. Without
Cargo.lockbeside the workspace root, the action fails whenlockfile_requiredistrue. Otherwise it runscargo generate-lockfileand warns. Every later Cargo command runs with--locked. - Prepare coverage. Adds the
llvm-tools-previewcomponent to a channel toolchain, clears old profiling data, and lists the packages the selection inputs pick withcargo tree. - Prepare reports. Creates the report directory, after the setup
script has run, and checks a set
artefact_pathagain. - Run tests. Runs
cargo test,cargo nextest run,cargo llvm-cov testorcargo llvm-cov nextest, with the selection inputs and thentest_args. - Run doc tests. nextest cannot run doc tests and cargo-llvm-cov
leaves them out on stable Rust, so those runs get a separate,
uninstrumented
cargo test --doc. A selection without a library target has no doc tests, and the action skips them. A plaincargo testrun includes its doc tests last and stops at the first failing test binary, so when it fails the summary reports the doc tests as unknown. - Write coverage reports. Writes
lcov.infoandcobertura.xmlfor the packages listed in stage 5, naming each with-p, sincecargo llvm-cov reportaccepts neitherexcludenor the feature inputs. It computescoverage_percentas the lines hit over the lines found in the lcov report. - Collect JUnit report. For nextest, checks that
junit.xmlexists.
A failing test or doc test run does not stop the later stages, so the reports describe the failure, and the upload step still runs. Any other failure stops the action. The job summary shows the outcome, a table of the stages, the report sizes, and any warnings.
A toolchain input overrides rust-toolchain.toml, so rustup no
longer installs the components and targets that file lists. Pass
them through toolchain_components and toolchain_targets instead:
- uses: lfreleng-actions/rust-test-action@<commit-sha>
with:
toolchain: "1.90.0"
toolchain_components: "clippy, rust-src"
toolchain_targets: "wasm32-unknown-unknown"Names may contain A-Z a-z 0-9 _ . - and start with a letter or
digit; whitespace, newlines and commas separate them. What the action
does depends on toolchain_kind:
channel, with either list naming anything: the action runs onerustup toolchain install <toolchain> --profile minimal --no-self-updatecall, adding--componentand--targetwith comma-separated lists when they name anything. It installs a missing toolchain with the minimal profile, or adds the components and targets to the installed one. An exact version such as1.90.0keeps its version; a moving channel such asstableupdates to its newest release.channel, withtoolchainset and both lists empty: the action installs that toolchain the same way whenrustup which --toolchain <toolchain> rustc, run withRUSTUP_AUTO_INSTALL=0, finds it missing, and otherwise leaves the installed one untouched. It does not rely on rustup's auto-install, which installs thedefaultprofile and whichRUSTUP_AUTO_INSTALL=0turns off. A toolchain fromrustup toolchain linkworks here, though rustup cannot add components or targets to one. A channel that the project selects, with both lists empty, gets no rustup call at all.path: the action ignores both inputs, with a warning.none: the action fails when either input names anything.
These are the rules
rust-build-action
follows, so a workflow can pass the same inputs to both. The job
summary lists what the action installed. Coverage still adds
llvm-tools-preview in its own rustup component add call, which
never updates the toolchain.
-
The action removes these variables from its own environment before it runs anything, so Cargo, its plugins, rustc, rustup,
setup_script, and any program that project code places onPATHrun without them:- registry tokens:
CARGO_REGISTRY_TOKENand everyCARGO_REGISTRIES_<NAME>_TOKEN; otherCARGO_REGISTRIES_<NAME>_*settings, such as_INDEX, stay; - GitHub OIDC and runtime tokens:
ACTIONS_ID_TOKEN_REQUEST_TOKEN,ACTIONS_ID_TOKEN_REQUEST_URLandACTIONS_RUNTIME_TOKEN; - the runner's command files:
GITHUB_OUTPUT,GITHUB_ENV,GITHUB_PATH,GITHUB_STATEandGITHUB_STEP_SUMMARY.
Tests run project code: give this job no credentials it does not need. The action keeps the command file paths as unexported shell variables and writes its own outputs and summary through them.
- registry tokens:
-
path_prefix,manifest_path,setup_scriptand the Cargo workspace root must resolve inside the workspace after following symlinks.manifest_path, the workspace rootCargo.toml,setup_scriptandCargo.lockmust not be symlinks themselves. -
test_argsmust be one line. The action splits it on spaces and tabs and passes each word as one argument, so quotes, globs and$carry no meaning. -
The action validates every output as a single line before writing it, and escapes values it shows in the job summary.
-
Removing the command file variables is defence in depth, not a boundary. It stops build scripts, proc-macros, tests and setup scripts from setting this step's outputs, or the environment,
PATHand summary of later steps, through the documented variables. Their paths follow a predictable pattern belowRUNNER_TEMP, and project code runs as the runner user, so code that goes looking can still write to them. Trust this action's outputs, and anything later steps read from the job environment, no more than the project under test. -
Before naming or uploading the reports directory, the action checks that it holds regular files alone and is no symlink. Project code could plant a symlink there to make the uploader read files from outside it; the action then fails and uploads nothing.
With an empty artefact_path, the action writes the reports to a new
directory below RUNNER_TEMP and nothing into the checkout. The paths
in the outputs stay valid for later steps in the same job.
A non-empty artefact_path names a directory, relative to
path_prefix, that keeps the reports in place, for example for a
later step that publishes them. It must resolve inside the workspace,
must not be a symlink, and must be empty or absent, so the artefact
holds this run's reports and nothing else. The action creates it
right before the tests run, after setup_script, and checks it again
then. An earlier stop creates no report directory and leaves the
artefact_path output empty. With artefact_upload on, its path
must not hold *, ?, [, ] or a backslash, which
actions/upload-artifact reads as glob syntax.
When artefact_upload is true and the run wrote any report, the
action uploads the directory as the artefact_name artefact with
if-no-files-found: warn. The upload runs after failing tests too,
with permit_fail true or false, so the reports explain the
failure. The artefact_name output is empty when nothing reached the
upload.
-
The action runs on Linux runners, which its CI tests. It refuses Windows runners, whose native paths its workspace checks do not handle. Its CI does not cover macOS runners.
-
The
cargorunner cannot write JUnit XML;junit: "true"needstest_runner: "nextest". -
test_argsgo to the main test run and not to the separate doc test run. Forcargo test, put test harness arguments after--, for example-- --test-threads=1. -
Coverage figures leave out doc tests.
-
permit_failcovers failing tests and doc tests, and a coverage or JUnit report that a failing main test run left unwritten: the failure may stop the run before it writes them. Invalid inputs, a failing setup script, a missing lockfile, or a report missing after the main test run passed, even with failing doc tests, still fail the step. -
nextest fails a run that finds no tests to run. Pass
test_args: "--no-tests=pass"to accept that. -
For JUnit XML, the action runs nextest with its own
rust-test-actionprofile from a tool configuration file. A.config/nextest.tomlin the repository still applies and takes precedence where both set a value. -
See Reports for where the reports live.
-
Cargo runs from
path_prefix, so it reads.cargo/config.tomlfiles frompath_prefixand its parents. -
The action does not cache Cargo's registry or build directory.