MCPcopy Index your code
hub / github.com/Manishearth/compiletest-rs

github.com/Manishearth/compiletest-rs @v0.11.12

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.11.12 ↗ · + Follow
278 symbols 677 edges 36 files 31 documented · 11% updated 4mo agov0.9.0 · 2022-09-23★ 23635 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

compiletest-rs

This project is an attempt at extracting the compiletest utility from the Rust compiler.

The compiletest utility is useful for library and plugin developers, who want to include test programs that should fail to compile, issue warnings or otherwise produce compile-time output.

To use in your project

To use compiletest-rs in your application, add the following to Cargo.toml

[dev-dependencies]
compiletest_rs = "0.11.0"

By default, compiletest-rs should be able to run on both stable, beta and nightly channels of rust. We use the tester fork of Rust's builtin test crate, so that we don't have require nightly. If you are running nightly and want to use Rust's test crate directly, you need to have the rustc development libraries install (which you can get by running rustup component add rustc-dev --toolchain nightly). Once you have the rustc development libraries installed, you can use the rustc feature to make compiletest use them instead of the tester crate.

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

Create a tests folder in the root folder of your project. Create a test file with something like the following:

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");
}

Each mode corresponds to a folder with the same name in the tests folder. That is for the compile-fail mode the test runner looks for the tests/compile-fail folder.

Adding flags to the Rust compiler is a matter of assigning the correct field in the config. The most common flag to populate is the target_rustcflags to include the link dependencies on the path.

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

This is useful (and necessary) for library development. Note that other secondary library dependencies may have their build artifacts placed in different (non-obvious) locations and these locations must also be added.

For convenience, Config provides a link_deps() method that populates target_rustcflags with all the dependencies found in the PATH variable (which is OS specific). For most cases, it should be sufficient to do:

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

Note that link_deps() panics if any of the added paths contain spaces, as these are currently not handled correctly.

Example

See the test-project folder for a complete working example using the compiletest-rs utility. Simply cd test-project and cargo test to see the tests run. The annotation syntax is documented in the rustc-guide.

TODO

  • The run-pass mode is strictly not necessary since it's baked right into Cargo, but I haven't bothered to take it out

Contributing

Thank you for your interest in improving this utility! Please consider submitting your patch to the upstream source instead, as it will be incorporated into this repo in due time. Still, there are some supporting files that are specific to this repo, for example:

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

If you are unsure, open a pull request anyway and we would be glad to help!

Extension points exported contracts — how you extend this code

Foo (Interface)
(no doc) [2 implementers]
test-project/tests/run-pass/associated-types-binding-in-where-clause.rs
PathBufExt (Interface)
(no doc) [1 implementers]
src/util.rs
PathExt (Interface)
(no doc) [1 implementers]
tests/test_support/mod.rs
Parser (Interface)
(no doc) [1 implementers]
test-project/tests/run-fail/issue-20971.rs
Bar (Interface)
(no doc) [1 implementers]
test-project/tests/compile-fail/where-for-self.rs
Foo (Interface)
(no doc)
test-project/tests/compile-fail/trait-bounds-cant-coerce.rs

Core symbols most depended-on inside this repo

next
called by 23
src/uidiff.rs
parse_name_value_directive
called by 14
src/header.rs
parse_name_directive
called by 13
src/header.rs
run_tests
called by 5
src/lib.rs
logv
called by 5
src/util.rs
read
called by 5
src/read2.rs
expected_output_path
called by 3
src/common.rs
load_errors
called by 3
src/errors.rs

Shape

Method 155
Function 88
Class 22
Enum 7
Interface 6

Languages

Rust100%

Modules by API surface

src/runtest.rs99 symbols
src/header.rs43 symbols
src/common.rs19 symbols
src/lib.rs12 symbols
tests/test_support/mod.rs10 symbols
src/util.rs10 symbols
src/json.rs10 symbols
src/errors.rs7 symbols
test-project/tests/run-pass/associated-types-binding-in-where-clause.rs6 symbols
test-project/tests/compile-fail/trait-bounds-cant-coerce.rs6 symbols
src/read2.rs6 symbols
src/uidiff.rs5 symbols

For agents

$ claude mcp add compiletest-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page