Some projects include multiple crates, which makes it a hassle to run all tests manually in each.
Is there a convenient way to recursively run cargo test
?
Update: since adding this answer 1.15 was released, adding cargo test --all
, will compare this with a custom script.
This shell-script runs tests recursively on a git repository for all directories containing a Cargo.toml
file (easy enough to edit for other VCS).
nocapture
so stdout is shownRUST_BACKTRACE
set, for more useful output.CARGO_BIN
environment variable to override the cargo commandScript:
#!/bin/bash
# exit on first error, see: http://stackoverflow.com/a/185900/432509
error() {
local parent_lineno="$1"
local message="$2"
local code="${3:-1}"
if [[ -n "$message" ]] ; then
echo "Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}"
else
echo "Error on or near line ${parent_lineno}; exiting with status ${code}"
fi
exit "${code}"
}
trap 'error ${LINENO}' ERR
# done with trap
# support cargo command override
if [[ -z $CARGO_BIN ]]; then
CARGO_BIN=cargo
fi
# toplevel git repo
ROOT=$(git rev-parse --show-toplevel)
for cargo_dir in $(find "$ROOT" -name Cargo.toml -printf '%h\n'); do
echo "Running tests in: $cargo_dir"
pushd "$cargo_dir"
RUST_BACKTRACE=0 $CARGO_BIN test --no-run
RUST_BACKTRACE=1 $CARGO_BIN test -- --nocapture
popd
done
Thanks to @набиячлэвэли's answer, this is an expanded version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With