Skip to content

Commit 70d0659

Browse files
authored
[cargo-nextest] use stylesheets everywhere, update supports-color (#1699)
This picks up an update to supports-color v3, and also uses that to determine color support everywhere. We stop using owo-colors' `if_supports_color` because that hasn't been updated to v3 yet.
1 parent fa6f676 commit 70d0659

File tree

12 files changed

+201
-193
lines changed

12 files changed

+201
-193
lines changed

Cargo.lock

Lines changed: 4 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ guppy = "0.17.7"
2424
maplit = "1.0.2"
2525
miette = "7.2.0"
2626
once_cell = "1.19.0"
27-
owo-colors = { version = "4.0.0", features = ["supports-colors"] }
27+
# note: we don't use owo-colors' if_supports_color support for now, instead preferring to use our
28+
# own supports-color + stylesheets everywhere.
29+
owo-colors = "4.0.0"
2830
newtype-uuid = { version = "1.1.0", features = ["v4"] }
2931
nextest-metadata = { version = "0.12.1", path = "nextest-metadata" }
3032
nextest-workspace-hack = "0.1.0"

cargo-nextest/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pathdiff = { version = "0.2.1", features = ["camino"] }
3535
quick-junit.workspace = true
3636
semver = "1.0.23"
3737
shell-words = "1.1.0"
38-
supports-color = "2.1.0"
38+
supports-color = "3.0.1"
3939
supports-unicode = "3.0.0"
4040
serde_json = "1.0.128"
4141
swrite.workspace = true

cargo-nextest/src/dispatch.rs

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::{
55
cargo_cli::{CargoCli, CargoOptions},
6-
output::{should_redact, OutputContext, OutputOpts, OutputWriter},
6+
output::{should_redact, OutputContext, OutputOpts, OutputWriter, StderrStyles},
77
reuse_build::{make_path_mapper, ArchiveFormatOpt, ReuseBuildOpts},
88
ExpectedError, Result, ReuseBuildKind,
99
};
@@ -47,7 +47,7 @@ use nextest_runner::{
4747
RustcCli,
4848
};
4949
use once_cell::sync::OnceCell;
50-
use owo_colors::{OwoColorize, Stream, Style};
50+
use owo_colors::OwoColorize;
5151
use quick_junit::XmlString;
5252
use semver::Version;
5353
use std::{
@@ -71,16 +71,32 @@ pub struct CargoNextestApp {
7171
}
7272

7373
impl CargoNextestApp {
74+
/// Initializes the output context.
75+
pub fn init_output(&self) -> OutputContext {
76+
match &self.subcommand {
77+
NextestSubcommand::Nextest(args) => args.common.output.init(),
78+
NextestSubcommand::Ntr(args) => args.common.output.init(),
79+
#[cfg(unix)]
80+
// Double-spawned processes should never use coloring.
81+
NextestSubcommand::DoubleSpawn(_) => OutputContext::color_never_init(),
82+
}
83+
}
84+
7485
/// Executes the app.
75-
pub fn exec(self, cli_args: Vec<String>, output_writer: &mut OutputWriter) -> Result<i32> {
86+
pub fn exec(
87+
self,
88+
cli_args: Vec<String>,
89+
output: OutputContext,
90+
output_writer: &mut OutputWriter,
91+
) -> Result<i32> {
7692
#[cfg(feature = "experimental-tokio-console")]
7793
nextest_runner::console::init();
7894

7995
match self.subcommand {
80-
NextestSubcommand::Nextest(app) => app.exec(cli_args, output_writer),
81-
NextestSubcommand::Ntr(opts) => opts.exec(cli_args, output_writer),
96+
NextestSubcommand::Nextest(app) => app.exec(cli_args, output, output_writer),
97+
NextestSubcommand::Ntr(opts) => opts.exec(cli_args, output, output_writer),
8298
#[cfg(unix)]
83-
NextestSubcommand::DoubleSpawn(opts) => opts.exec(),
99+
NextestSubcommand::DoubleSpawn(opts) => opts.exec(output),
84100
}
85101
}
86102
}
@@ -111,9 +127,12 @@ impl AppOpts {
111127
/// Execute the command.
112128
///
113129
/// Returns the exit code.
114-
fn exec(self, cli_args: Vec<String>, output_writer: &mut OutputWriter) -> Result<i32> {
115-
let output = self.common.output.init();
116-
130+
fn exec(
131+
self,
132+
cli_args: Vec<String>,
133+
output: OutputContext,
134+
output_writer: &mut OutputWriter,
135+
) -> Result<i32> {
117136
match self.command {
118137
Command::List {
119138
cargo_options,
@@ -173,8 +192,8 @@ impl AppOpts {
173192
}
174193
Command::ShowConfig { command } => command.exec(
175194
self.common.manifest_path,
176-
self.common.output,
177195
self.common.config_opts,
196+
output,
178197
output_writer,
179198
),
180199
Command::Self_ { command } => command.exec(self.common.output),
@@ -408,9 +427,12 @@ struct NtrOpts {
408427
}
409428

410429
impl NtrOpts {
411-
fn exec(self, cli_args: Vec<String>, output_writer: &mut OutputWriter) -> Result<i32> {
412-
let output = self.common.output.init();
413-
430+
fn exec(
431+
self,
432+
cli_args: Vec<String>,
433+
output: OutputContext,
434+
output_writer: &mut OutputWriter,
435+
) -> Result<i32> {
414436
let base = BaseApp::new(
415437
output,
416438
self.run_opts.reuse_build,
@@ -1079,9 +1101,11 @@ impl BaseApp {
10791101
let host = HostPlatform::current(PlatformLibdir::from_rustc_stdout(
10801102
RustcCli::print_host_libdir().read(),
10811103
))?;
1082-
let target = if let Some(triple) =
1083-
discover_target_triple(&cargo_configs, cargo_opts.target.as_deref())
1084-
{
1104+
let target = if let Some(triple) = discover_target_triple(
1105+
&cargo_configs,
1106+
cargo_opts.target.as_deref(),
1107+
&output.stderr_styles(),
1108+
) {
10851109
let libdir = PlatformLibdir::from_rustc_stdout(
10861110
RustcCli::print_target_libdir(&triple).read(),
10871111
);
@@ -1189,6 +1213,8 @@ impl BaseApp {
11891213
}
11901214

11911215
fn check_version_config_initial(&self, version_cfg: &NextestVersionConfig) -> Result<()> {
1216+
let styles = self.output.stderr_styles();
1217+
11921218
match version_cfg.eval(
11931219
&self.current_version,
11941220
self.config_opts.override_version_check,
@@ -1210,8 +1236,8 @@ impl BaseApp {
12101236
} => {
12111237
log::warn!(
12121238
"this repository recommends nextest version {}, but the current version is {}",
1213-
required.if_supports_color(Stream::Stderr, |x| x.bold()),
1214-
current.if_supports_color(Stream::Stderr, |x| x.bold()),
1239+
required.style(styles.bold),
1240+
current.style(styles.bold),
12151241
);
12161242
if let Some(tool) = tool {
12171243
log::info!(
@@ -1267,6 +1293,8 @@ impl BaseApp {
12671293
}
12681294

12691295
fn check_version_config_final(&self, version_cfg: &NextestVersionConfig) -> Result<()> {
1296+
let styles = self.output.stderr_styles();
1297+
12701298
match version_cfg.eval(
12711299
&self.current_version,
12721300
self.config_opts.override_version_check,
@@ -1288,8 +1316,8 @@ impl BaseApp {
12881316
} => {
12891317
log::warn!(
12901318
"this repository recommends nextest version {}, but the current version is {}",
1291-
required.if_supports_color(Stream::Stderr, |x| x.bold()),
1292-
current.if_supports_color(Stream::Stderr, |x| x.bold()),
1319+
required.style(styles.bold),
1320+
current.style(styles.bold),
12931321
);
12941322
if let Some(tool) = tool {
12951323
log::info!(
@@ -1303,6 +1331,7 @@ impl BaseApp {
13031331
crate::helpers::log_needs_update(
13041332
log::Level::Info,
13051333
crate::helpers::BYPASS_VERSION_TEXT,
1334+
&styles,
13061335
);
13071336

13081337
Ok(())
@@ -1333,8 +1362,13 @@ impl BaseApp {
13331362
}
13341363

13351364
fn load_runner(&self, build_platforms: &BuildPlatforms) -> &TargetRunner {
1336-
self.target_runner
1337-
.get_or_init(|| runner_for_target(&self.cargo_configs, build_platforms))
1365+
self.target_runner.get_or_init(|| {
1366+
runner_for_target(
1367+
&self.cargo_configs,
1368+
build_platforms,
1369+
&self.output.stderr_styles(),
1370+
)
1371+
})
13381372
}
13391373

13401374
fn exec_archive(
@@ -1798,11 +1832,10 @@ impl ShowConfigCommand {
17981832
fn exec(
17991833
self,
18001834
manifest_path: Option<Utf8PathBuf>,
1801-
output: OutputOpts,
18021835
config_opts: ConfigOpts,
1836+
output: OutputContext,
18031837
output_writer: &mut OutputWriter,
18041838
) -> Result<i32> {
1805-
let output = output.init();
18061839
match self {
18071840
Self::Version {} => {
18081841
let mut cargo_cli =
@@ -1856,13 +1889,15 @@ impl ShowConfigCommand {
18561889
crate::helpers::log_needs_update(
18571890
log::Level::Error,
18581891
crate::helpers::BYPASS_VERSION_TEXT,
1892+
&output.stderr_styles(),
18591893
);
18601894
Ok(nextest_metadata::NextestExitCode::REQUIRED_VERSION_NOT_MET)
18611895
}
18621896
NextestVersionEval::Warn { .. } => {
18631897
crate::helpers::log_needs_update(
18641898
log::Level::Warn,
18651899
crate::helpers::BYPASS_VERSION_TEXT,
1900+
&output.stderr_styles(),
18661901
);
18671902
Ok(nextest_metadata::NextestExitCode::RECOMMENDED_VERSION_NOT_MET)
18681903
}
@@ -2199,6 +2234,7 @@ fn acquire_graph_data(
21992234
fn discover_target_triple(
22002235
cargo_configs: &CargoConfigs,
22012236
target_cli_option: Option<&str>,
2237+
styles: &StderrStyles,
22022238
) -> Option<TargetTriple> {
22032239
match TargetTriple::find(cargo_configs, target_cli_option) {
22042240
Ok(Some(triple)) => {
@@ -2216,7 +2252,7 @@ fn discover_target_triple(
22162252
None
22172253
}
22182254
Err(err) => {
2219-
warn_on_err("target triple", &err);
2255+
warn_on_err("target triple", &err, styles);
22202256
None
22212257
}
22222258
}
@@ -2225,52 +2261,48 @@ fn discover_target_triple(
22252261
fn runner_for_target(
22262262
cargo_configs: &CargoConfigs,
22272263
build_platforms: &BuildPlatforms,
2264+
styles: &StderrStyles,
22282265
) -> TargetRunner {
22292266
match TargetRunner::new(cargo_configs, build_platforms) {
22302267
Ok(runner) => {
22312268
if build_platforms.target.is_some() {
22322269
if let Some(runner) = runner.target() {
2233-
log_platform_runner("for the target platform, ", runner);
2270+
log_platform_runner("for the target platform, ", runner, styles);
22342271
}
22352272
if let Some(runner) = runner.host() {
2236-
log_platform_runner("for the host platform, ", runner);
2273+
log_platform_runner("for the host platform, ", runner, styles);
22372274
}
22382275
} else {
22392276
// If triple is None, then the host and target platforms use the same runner if
22402277
// any.
22412278
if let Some(runner) = runner.target() {
2242-
log_platform_runner("", runner);
2279+
log_platform_runner("", runner, styles);
22432280
}
22442281
}
22452282
runner
22462283
}
22472284
Err(err) => {
2248-
warn_on_err("target runner", &err);
2285+
warn_on_err("target runner", &err, styles);
22492286
TargetRunner::empty()
22502287
}
22512288
}
22522289
}
22532290

2254-
fn log_platform_runner(prefix: &str, runner: &PlatformRunner) {
2291+
fn log_platform_runner(prefix: &str, runner: &PlatformRunner, styles: &StderrStyles) {
22552292
let runner_command = shell_words::join(std::iter::once(runner.binary()).chain(runner.args()));
22562293
log::info!(
22572294
"{prefix}using target runner `{}` defined by {}",
2258-
runner_command.if_supports_color(Stream::Stderr, |s| s.bold()),
2295+
runner_command.style(styles.bold),
22592296
runner.source()
22602297
)
22612298
}
22622299

2263-
fn warn_on_err(thing: &str, err: &(dyn std::error::Error)) {
2300+
fn warn_on_err(thing: &str, err: &(dyn std::error::Error), styles: &StderrStyles) {
22642301
let mut s = String::with_capacity(256);
22652302
swrite!(s, "could not determine {thing}: {err}");
22662303
let mut next_error = err.source();
22672304
while let Some(err) = next_error {
2268-
swrite!(
2269-
s,
2270-
"\n {} {}",
2271-
"caused by:".if_supports_color(Stream::Stderr, |s| s.style(Style::new().yellow())),
2272-
err
2273-
);
2305+
swrite!(s, "\n {} {}", "caused by:".style(styles.warning_text), err);
22742306
next_error = err.source();
22752307
}
22762308

0 commit comments

Comments
 (0)