Skip to content

Commit

Permalink
Auto merge of #14425 - ShashiSugrim:shashi/issue14403, r=epage
Browse files Browse the repository at this point in the history
fix: doctest respects Cargo's color options

### What does this PR try to resolve?

Explain the motivation behind this change.

This commit fixes the issue where cargo's test command doesn't respect the color parameter when it gets passed in like this command: `cargo t --color never --doc -- --color never`

Fixes #14403
### How should we test and review this PR?
Test on a basic rust project, with a file called lib.rs that looks like this
```
/// ```
/// bar
/// ```
pub fn foo() {}
#[cfg(test)]
mod tests {
    #[test]
    fn foo() {
        bar
    }
}
```
You can try to replicate the same commands `@zacknewman` used in the description of this issue.
`cargo t --color never --doc -- --color never`

You will see that compared to the official build of cargo, this build will respect the --color argument
  • Loading branch information
bors committed Aug 20, 2024
2 parents b2430df + 4657015 commit 3ef3f61
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::core::compiler::{Compilation, CompileKind, Doctest, Metadata, Unit, UnitOutput};
use crate::core::profiles::PanicStrategy;
use crate::core::shell::ColorChoice;
use crate::core::shell::Verbosity;
use crate::core::{TargetKind, Workspace};
use crate::ops;
Expand Down Expand Up @@ -176,6 +177,7 @@ fn run_doc_tests(
let gctx = ws.gctx();
let mut errors = Vec::new();
let doctest_xcompile = gctx.cli_unstable().doctest_xcompile;
let color = gctx.shell().color_choice();

for doctest_info in &compilation.to_doc_test {
let Doctest {
Expand Down Expand Up @@ -215,6 +217,14 @@ fn run_doc_tests(
for (var, value) in env {
p.env(var, value);
}

let color_arg = match color {
ColorChoice::Always => "always",
ColorChoice::Never => "never",
ColorChoice::CargoAuto => "auto",
};
p.arg("--color").arg(color_arg);

p.arg("--crate-name").arg(&unit.target.crate_name());
p.arg("--test");

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2871,7 +2871,7 @@ fn env_test() {
[RUNNING] `[ROOT]/foo/target/debug/deps/foo-[HASH][EXE]`
[RUNNING] `[ROOT]/foo/target/debug/deps/test-[HASH][EXE]`
[DOCTEST] foo
[RUNNING] `rustdoc --edition=2015 --crate-type lib --crate-name foo[..]`
[RUNNING] `rustdoc --edition=2015 --crate-type lib --color auto --crate-name foo[..]`
"#]])
.with_stdout_data(str![[r#"
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ fn cdylib_and_rlib() {
[RUNNING] `[ROOT]/foo/target/release/deps/bar-[HASH][EXE]`
[RUNNING] `[ROOT]/foo/target/release/deps/b-[HASH][EXE]`
[DOCTEST] bar
[RUNNING] `rustdoc --edition=2015 --crate-type cdylib --crate-type rlib --crate-name bar --test [..]-C lto [..]
[RUNNING] `rustdoc --edition=2015 --crate-type cdylib --crate-type rlib --color auto --crate-name bar --test [..]-C lto [..]
"#]].unordered())
.run();
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5555,7 +5555,7 @@ fn cargo_test_print_env_verbose() {
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `[..]CARGO_MANIFEST_DIR=[ROOT]/foo[..] [ROOT]/foo/target/debug/deps/foo-[HASH][EXE]`
[DOCTEST] foo
[RUNNING] `[..]CARGO_MANIFEST_DIR=[ROOT]/foo[..] rustdoc --edition=2015 --crate-type lib --crate-name foo[..]`
[RUNNING] `[..]CARGO_MANIFEST_DIR=[ROOT]/foo[..] rustdoc --edition=2015 --crate-type lib --color auto --crate-name foo[..]`
"#]]).run();
}
Expand Down

0 comments on commit 3ef3f61

Please sign in to comment.