Skip to content

Commit 04c8c06

Browse files
committed
Auto merge of #5563 - ehuss:cargotest-doc, r=alexcrichton
cargotest: Add some more docs Hopefully this isn't too wordy. This isn't intended for rustdoc output, but generally to be read in-editor.
2 parents 69d61e4 + 139c750 commit 04c8c06

File tree

5 files changed

+199
-6
lines changed

5 files changed

+199
-6
lines changed

CONTRIBUTING.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ and unexpected behavior.
2222
its users' security, please do not open a public issue on GitHub. Instead,
2323
we ask you to refer to Rust's [security policy].**
2424

25-
Opening an issue is as easy as following [this link][new-issues] and filling out
26-
the fields. Here's a template that you can use to file an issue, though it's not
25+
Opening an issue is as easy as following [this link][new-issues] and filling out
26+
the fields. Here's a template that you can use to file an issue, though it's not
2727
necessary to use it exactly:
2828

2929
<short summary of the problem>
@@ -41,7 +41,7 @@ happened instead. Please use https://gist.github.com/ if your examples run long.
4141

4242
## Working on issues
4343

44-
If you're looking for somewhere to start, check out the [E-easy][E-Easy] and
44+
If you're looking for somewhere to start, check out the [E-easy][E-Easy] and
4545
[E-mentor][E-mentor] tags.
4646

4747
Feel free to ask for guidelines on how to tackle a problem on [IRC] or open a
@@ -76,7 +76,8 @@ working on.
7676
* Include tests that cover all non-trivial code. The existing tests
7777
in `test/` provide templates on how to test Cargo's behavior in a
7878
sandbox-environment. The internal crate `cargotest` provides a vast amount
79-
of helpers to minimize boilerplate.
79+
of helpers to minimize boilerplate. See [`cargotest/mod.rs`] for an
80+
introduction to writing tests.
8081
* Make sure `cargo test` passes. If you do not have the cross-compilers
8182
installed locally, install them using the instructions returned by
8283
`cargo test cross_compile::cross_tests` (twice, with `--toolchain nightly`
@@ -181,3 +182,4 @@ adding labels to triage issues:
181182
[I-nominated]: https://github.com/rust-lang/cargo/labels/I-nominated
182183
[Code of Conduct]: https://www.rust-lang.org/conduct.html
183184
[IRC]: https://kiwiirc.com/client/irc.mozilla.org/cargo
185+
[`cargotest/mod.rs`]: https://github.com/rust-lang/cargo/blob/master/tests/testsuite/cargotest/mod.rs

src/cargo/util/process_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl ProcessBuilder {
113113
.and_then(|s| s)
114114
}
115115

116-
/// Get all environment variables explicitally set or unset for the process (not inherited
116+
/// Get all environment variables explicitly set or unset for the process (not inherited
117117
/// vars).
118118
pub fn get_envs(&self) -> &HashMap<String, Option<OsString>> {
119119
&self.env

tests/testsuite/cargotest/install.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ pub fn cargo_home() -> PathBuf {
1111
paths::home().join(".cargo")
1212
}
1313

14+
/// A `Matcher` used by `cargo install` tests to check if an executable binary
15+
/// has been installed. Example usage:
16+
///
17+
/// assert_that(cargo_home(), has_installed_exe("foo"));
1418
pub struct InstalledExe(pub &'static str);
1519

1620
pub fn exe(name: &str) -> String {

tests/testsuite/cargotest/mod.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,88 @@
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+
186
use std::ffi::OsStr;
287
use std::time::Duration;
388

@@ -19,6 +104,7 @@ pub static RUSTC: Rustc = Rustc::new(
19104
).unwrap()
20105
);
21106

107+
/// The rustc host such as `x86_64-unknown-linux-gnu`.
22108
pub fn rustc_host() -> String {
23109
RUSTC.with(|r| r.host.clone())
24110
}

0 commit comments

Comments
 (0)