]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | compiletest-rs |
2 | ============== | |
3 | ||
4 | This project is an attempt at extracting [the `compiletest` utility][upstream] | |
5 | from the Rust compiler. | |
6 | ||
7 | The `compiletest` utility is useful for library and plugin developers, who want | |
8 | to include test programs that should fail to compile, issue warnings or | |
9 | otherwise produce compile-time output. | |
10 | ||
11 | To use in your project | |
12 | ---------------------- | |
13 | To use `compiletest-rs` in your application, add the following to `Cargo.toml` | |
14 | ||
15 | ```toml | |
16 | [dev-dependencies] | |
17 | compiletest_rs = "*" | |
18 | ``` | |
19 | ||
20 | By default, `compiletest-rs` should be able to run on both stable, beta and | |
21 | nightly 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 | |
23 | and want to use Rust's `test` crate directly, you need to have the rustc development | |
24 | libraries install (which you can get by running `rustup component add rustc-dev | |
25 | --toolchain nightly`). Once you have the rustc development libraries installed, you | |
26 | can use the `rustc` feature to make compiletest use them instead of the `tester` | |
27 | crate. | |
28 | ||
29 | ```toml | |
30 | [dev-dependencies] | |
31 | compiletest_rs = { version = "*", features = [ "rustc" ] } | |
32 | ``` | |
33 | ||
34 | Create a `tests` folder in the root folder of your project. Create a test file | |
35 | with something like the following: | |
36 | ||
37 | ```rust | |
38 | extern crate compiletest_rs as compiletest; | |
39 | ||
40 | use std::path::PathBuf; | |
41 | ||
42 | fn 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] | |
54 | fn compile_test() { | |
55 | run_mode("compile-fail"); | |
56 | run_mode("run-pass"); | |
57 | } | |
58 | ||
59 | ``` | |
60 | ||
61 | Each mode corresponds to a folder with the same name in the `tests` folder. That | |
62 | is for the `compile-fail` mode the test runner looks for the | |
63 | `tests/compile-fail` folder. | |
64 | ||
65 | Adding flags to the Rust compiler is a matter of assigning the correct field in | |
66 | the 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 | |
71 | config.target_rustcflags = Some("-L target/debug".to_string()); | |
72 | ``` | |
73 | ||
74 | This is useful (and necessary) for library development. Note that other | |
75 | secondary library dependencies may have their build artifacts placed in | |
76 | different (non-obvious) locations and these locations must also be | |
77 | added. | |
78 | ||
79 | For convenience, `Config` provides a `link_deps()` method that | |
80 | populates `target_rustcflags` with all the dependencies found in the | |
81 | `PATH` variable (which is OS specific). For most cases, it should be | |
82 | sufficient to do: | |
83 | ||
84 | ```rust | |
85 | let mut config = compiletest::Config::default(); | |
86 | config.link_deps(); | |
87 | ``` | |
88 | ||
89 | Note that `link_deps()` should not be used if any of the added paths contain | |
90 | spaces, as these are currently not handled correctly. | |
91 | ||
92 | Example | |
93 | ------- | |
94 | See 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 | |
96 | tests run. The annotation syntax is documented in the [rustc-guide][tests]. | |
97 | ||
98 | TODO | |
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 | ||
103 | Contributing | |
104 | ------------ | |
105 | ||
106 | Thank you for your interest in improving this utility! Please consider | |
107 | submitting your patch to [the upstream source][src] instead, as it will | |
108 | be incorporated into this repo in due time. Still, there are some supporting | |
109 | files that are specific to this repo, for example: | |
110 | ||
111 | - src/lib.rs | |
112 | - src/uidiff.rs | |
113 | - test-project/ | |
114 | ||
115 | If 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 |