]> git.proxmox.com Git - rustc.git/blame - vendor/compiletest_rs/README.md
New upstream version 1.52.1+dfsg1
[rustc.git] / vendor / compiletest_rs / README.md
CommitLineData
f20569fa
XL
1compiletest-rs
2==============
3
4This project is an attempt at extracting [the `compiletest` utility][upstream]
5from the Rust compiler.
6
7The `compiletest` utility is useful for library and plugin developers, who want
8to include test programs that should fail to compile, issue warnings or
9otherwise produce compile-time output.
10
11To use in your project
12----------------------
13To use `compiletest-rs` in your application, add the following to `Cargo.toml`
14
15```toml
16[dev-dependencies]
17compiletest_rs = "*"
18```
19
20By default, `compiletest-rs` should be able to run on both stable, beta and
21nightly channels of rust. We use the [`tester`][tester] fork of Rust's builtin
22`test` crate, so that we don't have require nightly. If you are running nightly
23and want to use Rust's `test` crate directly, you need to have the rustc development
24libraries install (which you can get by running `rustup component add rustc-dev
25--toolchain nightly`). Once you have the rustc development libraries installed, you
26can use the `rustc` feature to make compiletest use them instead of the `tester`
27crate.
28
29```toml
30[dev-dependencies]
31compiletest_rs = { version = "*", features = [ "rustc" ] }
32```
33
34Create a `tests` folder in the root folder of your project. Create a test file
35with something like the following:
36
37```rust
38extern crate compiletest_rs as compiletest;
39
40use std::path::PathBuf;
41
42fn run_mode(mode: &'static str) {
43 let mut config = compiletest::Config::default();
44
45 config.mode = mode.parse().expect("Invalid mode");
46 config.src_base = PathBuf::from(format!("tests/{}", mode));
47 config.link_deps(); // Populate config.target_rustcflags with dependencies on the path
48 config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464
49
50 compiletest::run_tests(&config);
51}
52
53#[test]
54fn compile_test() {
55 run_mode("compile-fail");
56 run_mode("run-pass");
57}
58
59```
60
61Each mode corresponds to a folder with the same name in the `tests` folder. That
62is for the `compile-fail` mode the test runner looks for the
63`tests/compile-fail` folder.
64
65Adding flags to the Rust compiler is a matter of assigning the correct field in
66the config. The most common flag to populate is the
67`target_rustcflags` to include the link dependencies on the path.
68
69```rust
70// NOTE! This is the manual way of adding flags
71config.target_rustcflags = Some("-L target/debug".to_string());
72```
73
74This is useful (and necessary) for library development. Note that other
75secondary library dependencies may have their build artifacts placed in
76different (non-obvious) locations and these locations must also be
77added.
78
79For convenience, `Config` provides a `link_deps()` method that
80populates `target_rustcflags` with all the dependencies found in the
81`PATH` variable (which is OS specific). For most cases, it should be
82sufficient to do:
83
84```rust
85let mut config = compiletest::Config::default();
86config.link_deps();
87```
88
89Note that `link_deps()` should not be used if any of the added paths contain
90spaces, as these are currently not handled correctly.
91
92Example
93-------
94See the `test-project` folder for a complete working example using the
95`compiletest-rs` utility. Simply `cd test-project` and `cargo test` to see the
96tests run. The annotation syntax is documented in the [rustc-guide][tests].
97
98TODO
99----
100 - The `run-pass` mode is strictly not necessary since it's baked right into
101 Cargo, but I haven't bothered to take it out
102
103Contributing
104------------
105
106Thank you for your interest in improving this utility! Please consider
107submitting your patch to [the upstream source][src] instead, as it will
108be incorporated into this repo in due time. Still, there are some supporting
109files that are specific to this repo, for example:
110
111- src/lib.rs
112- src/uidiff.rs
113- test-project/
114
115If you are unsure, open a pull request anyway and we would be glad to help!
116
117
118[upstream]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest
119[src]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest/src
120[tests]: https://rustc-dev-guide.rust-lang.org/tests/adding.html#header-commands-configuring-rustc
121[tester]: https://crates.io/crates/tester