Skip to content

Commit 48b0156

Browse files
committed
feat: Respect --color=never Cargo option for mutation diagnostics
We already respected the `--color=never` option passed to rustc-driver directly. However, Cargo always forces colored output for its own internal diagnostic processing. This processing strips colors on the Cargo side if no coloring is requested, however this is not detectible from the driver side, and so this never carried over to the detection diagnostics we embed into the mutated crates. We now indicate the requested lack of colored output using an additional MUTEST_CARGO_EXPLICIT_NO_COLOR environment variable, which disables detection diagnostics coloring for Cargo-based invocations.
1 parent 4efb5ab commit 48b0156

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

cargo-mutest/src/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ fn main() {
107107
.arg(clap::Arg::new("PASSED_ARGS").trailing_var_arg(true).allow_hyphen_values(true))
108108
)
109109
// Cargo options.
110+
.arg(clap::arg!(--color [WHEN] "Output coloring."))
110111
.next_help_heading("Package Selection")
111112
.arg(clap::arg!(--workspace "Test all packages in the workspace."))
112113
.arg(clap::arg!(-p --package [PACKAGE] "Package with the target to analyze."))
@@ -255,6 +256,18 @@ fn main() {
255256
let i = mutest_args.iter().position(|arg| matches.subcommand_name().is_some_and(|subcommand| arg == subcommand)).expect("subcommand not found in args");
256257
mutest_args.splice(i.., [mutest_driver_subcommand.to_owned()]);
257258

259+
if let Some(color) = matches.get_one::<String>("color") {
260+
cmd.args(["--color", color]);
261+
strip_arg(&mut mutest_args, true, None, Some("color"));
262+
if color == "never" {
263+
// HACK: Indicate to mutest-driver that the explicit `--color=never` flag was passed.
264+
// NOTE: This is needed because Cargo always captures colored output, and
265+
// the Cargo --color flag only controls whether the coloring is stripped before final output.
266+
// This is also required for users because Cargo's rustc invocations disallow using RUSTFLAGS="--color=never".
267+
cmd.env("MUTEST_CARGO_EXPLICIT_NO_COLOR", "1");
268+
}
269+
}
270+
258271
let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
259272

260273
if let Some(manifest_path) = matches.get_one::<String>("manifest-path") {

mutest-emit/src/analysis/diagnostic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env;
12
use std::io::{self, Write};
23
use std::sync::{Arc, Mutex};
34

@@ -80,6 +81,7 @@ pub fn raw_output_full<G: EmissionGuarantee>(
8081
// However, this would require mutest_runtime to depend on anstream.
8182
let color_choice = match color_config {
8283
ColorConfig::Never => ColorChoice::Never,
84+
_ if let Ok("1") = env::var("MUTEST_CARGO_EXPLICIT_NO_COLOR").as_deref() => ColorChoice::Never,
8385
_ => ColorChoice::AlwaysAnsi,
8486
};
8587
let dst: Destination = AutoStream::new(Box::new(shared_buffer), color_choice);

0 commit comments

Comments
 (0)