#testing #plugin #test-harness #compiletest

dev compiletest_rs

来自 Rust 编译器的 compiletest 工具,作为独立的测试执行器

62 个版本

使用旧的 Rust 2015

0.11.0 2024 年 5 月 28 日
0.10.2 2023 年 5 月 22 日
0.10.1 2023 年 4 月 27 日
0.9.0 2022 年 9 月 23 日
0.0.7 2015 年 7 月 16 日

428编程语言

Download history 4324/week @ 2024-05-05 7553/week @ 2024-05-12 9247/week @ 2024-05-19 9477/week @ 2024-05-26 5422/week @ 2024-06-02 3354/week @ 2024-06-09 3425/week @ 2024-06-16 2660/week @ 2024-06-23 2466/week @ 2024-06-30 2423/week @ 2024-07-07 3572/week @ 2024-07-14 3336/week @ 2024-07-21 3139/week @ 2024-07-28 2405/week @ 2024-08-04 3106/week @ 2024-08-11 2898/week @ 2024-08-18

11,709 每月下载
少于 89 crates 中使用

MIT/Apache

210KB
4.5K SLoC

compiletest-rs

本项目尝试从 Rust 编译器中提取 compiletest 工具

compiletest 工具对库和插件开发者非常有用,他们希望包含应该无法编译、引发警告或产生编译时输出的测试程序。

在项目中使用

要在您的应用程序中使用 compiletest-rs,请在 Cargo.toml 中添加以下内容

[dev-dependencies]
compiletest_rs = "0.10.0"

默认情况下,compiletest-rs 应该能够在 rust 的稳定版、beta 版和 nightly 版本上运行。我们使用 Rust 内置 test crate 的 tester 分支,因此不需要 nightly。如果您正在运行 nightly 并想直接使用 Rust 的 test crate,则需要安装 rustc 开发库(您可以通过运行 rustup component add rustc-dev --toolchain nightly 获取)。一旦您安装了 rustc 开发库,您可以使用 rustc 特性使 compiletest 使用它们而不是 tester crate。

[dev-dependencies]
compiletest_rs = { version = "0.10.0", features = [ "rustc" ] }

在项目的根目录下创建一个 tests 文件夹。创建一个测试文件,内容大致如下:

extern crate compiletest_rs as compiletest;

use std::path::PathBuf;

fn run_mode(mode: &'static str) {
    let mut config = compiletest::Config::default();

    config.mode = mode.parse().expect("Invalid mode");
    config.src_base = PathBuf::from(format!("tests/{}", mode));
    config.link_deps(); // Populate config.target_rustcflags with dependencies on the path
    config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464

    compiletest::run_tests(&config);
}

#[test]
fn compile_test() {
    run_mode("compile-fail");
    run_mode("run-pass");
}

每种模式对应于 tests 文件夹中同名的文件夹。例如,对于 compile-fail 模式,测试运行器会在 tests/compile-fail 文件夹中查找。

向 Rust 编译器添加标志是分配配置中正确字段的问题。最常用的标志是填充 target_rustcflags 以包含路径上的链接依赖项。

// NOTE! This is the manual way of adding flags
config.target_rustcflags = Some("-L target/debug".to_string());

这对于库开发非常有用(并且是必要的)。注意,其他次要库依赖项可能将其构建工件放置在不同的(不明显)位置,这些位置也必须添加。

为了方便起见,Config 提供了一个 link_deps() 方法,该方法使用在 PATH 变量(这是特定于操作系统的)中找到的所有依赖项填充 target_rustcflags。在大多数情况下,只需执行以下操作即可:

let mut config = compiletest::Config::default();
config.link_deps();

请注意,如果添加的任何路径包含空格,则 link_deps() 将引发恐慌,因为这些目前没有正确处理。

示例

查看 test-project 文件夹以获取使用 compiletest-rs 工具的完整工作示例。只需 cd test-projectcargo test 来查看测试是否运行。注解语法在 rustc-guide 中有记录。

待办事项

  • run-pass 模式严格来说不是必需的,因为它已经内置在 Cargo 中,但我还没有费心将其删除。

贡献

感谢您对改进此工具的兴趣!请考虑将您的补丁提交给 上游源代码,因为它将在适当的时候纳入此存储库。尽管如此,还有一些支持文件是特定于此存储库的,例如

  • src/lib.rs
  • src/uidiff.rs
  • test-project/

如果您不确定,无论如何也请打开一个 pull 请求,我们将很高兴为您提供帮助!

依赖项

~4–19MB
~198K SLoC