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

时间:2017-02-01 02:01:17

标签: testing rust rust-cargo

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

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

4 个答案:

答案 0 :(得分:7)

更新:由于添加此答案1.15已发布,添加cargo test --all会将其与自定义脚本进行比较。

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

  • 退出第一个错误。
  • 使用nocapture所以stdout显示为(取决于个人喜好,易于调整)
  • 使用RUST_BACKTRACE设置运行测试,以获得更有用的输出。
  • 在两个单独的步骤中构建和运行(1.14稳定版中this bug的解决方法)。
  • 可选CARGO_BIN环境变量,用于覆盖货物命令
    (如果您想使用诸如cargo-out-of-source builder之类的货物包装,则很方便。

脚本:

#!/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

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

答案 1 :(得分:6)

您可以使用shell脚本。根据{{​​3}},这个

find . -name Cargo.toml -printf '%h\n'

将打印出包含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

将遍历包含Cargo.toml的所有目录(对于包装箱来说是一个不错的选择)并在其中运行cargo test

答案 2 :(得分:4)

我现在无法测试,但我相信你可以使用cargo test --all来做到这一点。

答案 3 :(得分:1)

您可以使用货物工作区功能。 This crate集合将其与Makefile结合使用,可用于自行编译每个包。

(工作区功能有助于不多次编译公共依赖项)