如何递归测试目录下的所有包装箱?

ide*_*n42 13 testing rust rust-cargo

有些项目包括多个包,这使得在每个包中手动运行所有测试变得麻烦.

是否有一种方便的递归运行方式cargo test

ide*_*n42 7

更新:自添加此答案1.15发布后,添加cargo test --all,将与自定义脚本进行比较.


这个shell脚本在包含Cargo.toml文件的所有目录的git存储库上递归运行测试(很容易编辑其他VCS).

  • 退出第一个错误.
  • 使用nocapture如此标准显示
    (取决于个人喜好,易于调整).
  • 使用RUST_BACKTRACEset 运行测试,以获得更有用的输出.
  • 构建并运行在两个单独的步骤
    (1.14稳定中的此错误的解决方法).
  • 可选的CARGO_BIN环境变量,用于覆盖货物命令
    (如果您想使用货物包装物,例如货物外包装生成器,则可以使用).

脚本:

#!/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
Run Code Online (Sandbox Code Playgroud)

感谢@набиячлэвэли的回答,这是一个扩展版本.


наб*_*эли 6

您可以使用shell脚本.根据这个答案,这个

find . -name Cargo.toml -printf '%h\n'
Run Code Online (Sandbox Code Playgroud)

将打印出包含的目录Cargo.toml,因此,将其与标准shell utils的其余部分组合在一起产生我们

for f in $(find . -name Cargo.toml -printf '%h\n' | sort -u); do
  pushd $f > /dev/null;
  cargo test;
  popd > /dev/null;
done
Run Code Online (Sandbox Code Playgroud)

这将迭代所有包含Cargo.toml(对于板条箱的好赌注)的目录并cargo test在其中运行.