1
+ /*
2
+ # Introduction To `cargotest`
3
+
4
+ Cargo has a wide variety of integration tests that execute the `cargo` binary
5
+ and verify its behavior. The `cargotest` module contains many helpers to make
6
+ this process easy.
7
+
8
+ The general form of a test involves creating a "project", running cargo, and
9
+ checking the result. Projects are created with the `ProjectBuilder` where you
10
+ specify some files to create. The general form looks like this:
11
+
12
+ ```
13
+ let p = project("foo")
14
+ .file("Cargo.toml", &basic_bin_manifest("foo"))
15
+ .file("src/main.rs", r#"fn main() { println!("hi!"); }"#)
16
+ .build();
17
+ ```
18
+
19
+ To run cargo, call the `cargo` method and use the `hamcrest` matchers to check
20
+ the output.
21
+
22
+ ```
23
+ assert_that(
24
+ p.cargo("run --bin foo"),
25
+ execs()
26
+ .with_status(0)
27
+ .with_stderr(
28
+ "\
29
+ [COMPILING] foo [..]
30
+ [FINISHED] [..]
31
+ [RUNNING] `target[/]debug[/]foo`
32
+ ",
33
+ )
34
+ .with_stdout("hi!"),
35
+ );
36
+ ```
37
+
38
+ The project creates a mini sandbox under the "cargo integration test"
39
+ directory with each test getting a separate directory such as
40
+ `/path/to/cargo/target/cit/t123/`. Each project appears as a separate
41
+ directory. There is also an empty `home` directory created that will be used
42
+ as a home directory instead of your normal home directory.
43
+
44
+ See `cargotest::support::lines_match` for an explanation of the string pattern
45
+ matching.
46
+
47
+ See the `hamcrest` module for other matchers like
48
+ `is_not(existing_file(path))`. This is not the actual hamcrest library, but
49
+ instead a lightweight subset of matchers that are used in cargo tests.
50
+
51
+ Browse the `pub` functions in the `cargotest` module for a variety of other
52
+ helpful utilities.
53
+
54
+ ## Testing Nightly Features
55
+
56
+ If you are testing a Cargo feature that only works on "nightly" cargo, then
57
+ you need to call `masquerade_as_nightly_cargo` on the process builder like
58
+ this:
59
+
60
+ ```
61
+ p.cargo("build").masquerade_as_nightly_cargo()
62
+ ```
63
+
64
+ If you are testing a feature that only works on *nightly rustc* (such as
65
+ benchmarks), then you should exit the test if it is not running with nightly
66
+ rust, like this:
67
+
68
+ ```
69
+ if !is_nightly() {
70
+ return;
71
+ }
72
+ ```
73
+
74
+ ## Platform-specific Notes
75
+
76
+ When checking output, be sure to use `[/]` when checking paths to
77
+ automatically support backslashes on Windows.
78
+
79
+ Be careful when executing binaries on Windows. You should not rename, delete,
80
+ or overwrite a binary immediately after running it. Under some conditions
81
+ Windows will fail with errors like "directory not empty" or "failed to remove"
82
+ or "access is denied".
83
+
84
+ */
85
+
1
86
use std:: ffi:: OsStr ;
2
87
use std:: time:: Duration ;
3
88
@@ -19,6 +104,7 @@ pub static RUSTC: Rustc = Rustc::new(
19
104
) . unwrap( )
20
105
) ;
21
106
107
+ /// The rustc host such as `x86_64-unknown-linux-gnu`.
22
108
pub fn rustc_host ( ) -> String {
23
109
RUSTC . with ( |r| r. host . clone ( ) )
24
110
}
0 commit comments