Skip to content

fix(test): Expose '--no-capture' in favor of --nocapture #139224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions library/test/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn optgroups() -> getopts::Options {
.optopt("", "logfile", "Write logs to the specified file (deprecated)", "PATH")
.optflag(
"",
"nocapture",
"no-capture",
"don't capture stdout/stderr of each \
task, allow printing directly",
)
Expand Down Expand Up @@ -172,7 +172,7 @@ tests in the same order again. Note that --shuffle and --shuffle-seed do not
affect whether the tests are run in parallel.

All tests have their standard output and standard error captured by default.
This can be overridden with the --nocapture flag or setting RUST_TEST_NOCAPTURE
This can be overridden with the --no-capture flag or setting RUST_TEST_NOCAPTURE
environment variable to a value other than "0". Logging is not captured by default.

Test Attributes:
Expand All @@ -199,7 +199,10 @@ Test Attributes:
/// otherwise creates a `TestOpts` object and returns it.
pub fn parse_opts(args: &[String]) -> Option<OptRes> {
// Parse matches.
let opts = optgroups();
let mut opts = optgroups();
// Flags hidden from `usage`
opts.optflag("", "nocapture", "Deprecated, use `--no-capture`");
Comment on lines +202 to +204
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From PR description:

Regarding the implementation, I moved --nocapture out of optgroups(), into parse_opts(), out of an abundance of caution in passing the options without a deprecated value to the usage generation. However, the usage does not actually show optional flags, so this could potentially be dropped, simplifying the PR.


let binary = args.first().map(|c| &**c).unwrap_or("...");
let args = args.get(1..).unwrap_or(args);
let matches = match opts.parse(args) {
Expand All @@ -210,7 +213,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
// Check if help was requested.
if matches.opt_present("h") {
// Show help and do nothing more.
usage(binary, &opts);
usage(binary, &optgroups());
return None;
}

Expand Down Expand Up @@ -447,7 +450,7 @@ fn get_color_config(matches: &getopts::Matches) -> OptPartRes<ColorConfig> {
}

fn get_nocapture(matches: &getopts::Matches) -> OptPartRes<bool> {
let mut nocapture = matches.opt_present("nocapture");
let mut nocapture = matches.opt_present("nocapture") || matches.opt_present("no-capture");
if !nocapture {
nocapture = match env::var("RUST_TEST_NOCAPTURE") {
Ok(val) => &val != "0",
Expand Down
10 changes: 6 additions & 4 deletions src/doc/rustc/src/tests/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ behavior.

> Note: When running with [`cargo test`], the libtest CLI arguments must be
> passed after the `--` argument to differentiate between flags for Cargo and
> those for the harness. For example: `cargo test -- --nocapture`
> those for the harness. For example: `cargo test -- --no-capture`

### Filters

Expand Down Expand Up @@ -225,7 +225,7 @@ The following options affect the output behavior.
Displays one character per test instead of one line per test. This is an alias
for [`--format=terse`](#--format-format).

#### `--nocapture`
#### `--no-capture`

Does not capture the stdout and stderr of the test, and allows tests to print
to the console. Usually the output is captured, and only displayed if the test
Expand All @@ -234,11 +234,13 @@ fails.
This may also be specified by setting the `RUST_TEST_NOCAPTURE` environment
variable to anything but `0`.

`--nocapture` is a deprecated alias for `--no-capture`.

#### `--show-output`

Displays the stdout and stderr of successful tests after all tests have run.

Contrast this with [`--nocapture`](#--nocapture) which allows tests to print
Contrast this with [`--no-capture`](#--no-capture) which allows tests to print
*while they are running*, which can cause interleaved output if there are
multiple tests running in parallel, `--show-output` ensures the output is
contiguous, but requires waiting for all tests to finish.
Expand All @@ -247,7 +249,7 @@ contiguous, but requires waiting for all tests to finish.

Control when colored terminal output is used. Valid options:

* `auto`: Colorize if stdout is a tty and [`--nocapture`](#--nocapture) is not
* `auto`: Colorize if stdout is a tty and [`--no-capture`](#--no-capture) is not
used. This is the default.
* `always`: Always colorize the output.
* `never`: Never colorize the output.
Expand Down
Loading