Skip to content

Commit

Permalink
[cargo-nextest] switch to supports-color v2
Browse files Browse the repository at this point in the history
supports-color 2.1.0 has fixes for truecolor support.
  • Loading branch information
sunshowers committed Sep 20, 2023
1 parent 147fb59 commit d99a897
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 52 deletions.
17 changes: 9 additions & 8 deletions cargo-nextest/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{
cargo_cli::{CargoCli, CargoOptions},
output::{OutputContext, OutputOpts, OutputWriter},
output::{OutputContext, OutputOpts, OutputWriter, SupportsColorsV2},
reuse_build::{make_path_mapper, ArchiveFormatOpt, ReuseBuildOpts},
ExpectedError, Result, ReuseBuildKind,
};
Expand Down Expand Up @@ -36,14 +36,15 @@ use nextest_runner::{
test_filter::{RunIgnored, TestFilterBuilder},
};
use once_cell::sync::OnceCell;
use owo_colors::{OwoColorize, Stream, Style};
use owo_colors::{OwoColorize, Style};
use semver::Version;
use std::{
env::VarError,
fmt::Write as _,
io::{Cursor, Write},
sync::Arc,
};
use supports_color::Stream;

/// A next-generation test runner for Rust.
///
Expand Down Expand Up @@ -1016,8 +1017,8 @@ impl BaseApp {
} => {
log::warn!(
"this repository recommends nextest version {}, but the current version is {}",
required.if_supports_color(Stream::Stderr, |x| x.bold()),
current.if_supports_color(Stream::Stderr, |x| x.bold()),
required.if_supports_color_2(Stream::Stderr, |x| x.bold()),
current.if_supports_color_2(Stream::Stderr, |x| x.bold()),
);
if let Some(tool) = tool {
log::info!(
Expand Down Expand Up @@ -1094,8 +1095,8 @@ impl BaseApp {
} => {
log::warn!(
"this repository recommends nextest version {}, but the current version is {}",
required.if_supports_color(Stream::Stderr, |x| x.bold()),
current.if_supports_color(Stream::Stderr, |x| x.bold()),
required.if_supports_color_2(Stream::Stderr, |x| x.bold()),
current.if_supports_color_2(Stream::Stderr, |x| x.bold()),
);
if let Some(tool) = tool {
log::info!(
Expand Down Expand Up @@ -1796,7 +1797,7 @@ fn log_platform_runner(prefix: &str, runner: &PlatformRunner) {
let runner_command = shell_words::join(std::iter::once(runner.binary()).chain(runner.args()));
log::info!(
"{prefix}using target runner `{}` defined by {}",
runner_command.if_supports_color(Stream::Stderr, |s| s.bold()),
runner_command.if_supports_color_2(Stream::Stderr, |s| s.bold()),
runner.source()
)
}
Expand All @@ -1809,7 +1810,7 @@ fn warn_on_err(thing: &str, err: &(dyn std::error::Error)) -> Result<(), std::fm
write!(
s,
"\n {} {}",
"caused by:".if_supports_color(Stream::Stderr, |s| s.style(Style::new().yellow())),
"caused by:".if_supports_color_2(Stream::Stderr, |s| s.style(Style::new().yellow())),
err
)?;
next_error = err.source();
Expand Down
57 changes: 30 additions & 27 deletions cargo-nextest/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::output::SupportsColorsV2;
use camino::Utf8PathBuf;
use itertools::Itertools;
use nextest_filtering::errors::FilterExpressionParseErrors;
use nextest_metadata::NextestExitCode;
use nextest_runner::errors::*;
use owo_colors::{OwoColorize, Stream};
use owo_colors::OwoColorize;
use semver::Version;
use std::{error::Error, string::FromUtf8Error};
use supports_color::Stream;
use thiserror::Error;

pub(crate) type Result<T, E = ExpectedError> = std::result::Result<T, E>;
Expand Down Expand Up @@ -424,7 +426,7 @@ impl ExpectedError {
Self::CargoMetadataExecFailed { command, err } => {
log::error!(
"failed to execute `{}`",
command.if_supports_color(Stream::Stderr, |x| x.bold())
command.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Expand All @@ -435,7 +437,7 @@ impl ExpectedError {
Self::CargoLocateProjectExecFailed { command, err } => {
log::error!(
"failed to execute `{}`",
command.if_supports_color(Stream::Stderr, |x| x.bold())
command.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Expand All @@ -450,7 +452,7 @@ impl ExpectedError {
Self::WorkspaceRootInvalid { workspace_root } => {
log::error!(
"workspace root `{}` is invalid",
workspace_root.if_supports_color(Stream::Stderr, |x| x.bold())
workspace_root.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
None
}
Expand All @@ -466,7 +468,7 @@ impl ExpectedError {
ReuseBuildKind::ReuseWithWorkspaceRemap { workspace_root } => {
format!(
"\n(hint: ensure that project source is available at {})",
workspace_root.if_supports_color(Stream::Stderr, |x| x.bold())
workspace_root.if_supports_color_2(Stream::Stderr, |x| x.bold())
)
}
ReuseBuildKind::Reuse => {
Expand All @@ -478,14 +480,14 @@ impl ExpectedError {
};
log::error!(
"workspace root manifest at {} does not exist{hint_str}",
path.if_supports_color(Stream::Stderr, |x| x.bold())
path.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
None
}
Self::StoreDirCreateError { store_dir, err } => {
log::error!(
"failed to create store dir at `{}`",
store_dir.if_supports_color(Stream::Stderr, |x| x.bold())
store_dir.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Expand All @@ -504,7 +506,7 @@ impl ExpectedError {
provided_by_tool(err.tool()),
override_error
.profile_name
.if_supports_color(Stream::Stderr, |p| p.bold()),
.if_supports_color_2(Stream::Stderr, |p| p.bold()),
);
for report in override_error.reports() {
log::error!(target: "cargo_nextest::no_heading", "{report:?}");
Expand All @@ -519,17 +521,17 @@ impl ExpectedError {
let known_groups_str = known_groups
.iter()
.map(|group_name| {
group_name.if_supports_color(Stream::Stderr, |x| x.bold())
group_name.if_supports_color_2(Stream::Stderr, |x| x.bold())
})
.join(", ");
let mut errors_str = String::new();
for error in errors {
errors_str.push_str(&format!(
" - group `{}` in overrides for profile `{}`\n",
error.name.if_supports_color(Stream::Stderr, |x| x.bold()),
error.name.if_supports_color_2(Stream::Stderr, |x| x.bold()),
error
.profile_name
.if_supports_color(Stream::Stderr, |x| x.bold())
.if_supports_color_2(Stream::Stderr, |x| x.bold())
));
}

Expand Down Expand Up @@ -563,29 +565,29 @@ impl ExpectedError {
} => {
log::error!(
"argument {} specified file `{}` that couldn't be read",
format!("--{arg_name}").if_supports_color(Stream::Stderr, |x| x.bold()),
file_name.if_supports_color(Stream::Stderr, |x| x.bold()),
format!("--{arg_name}").if_supports_color_2(Stream::Stderr, |x| x.bold()),
file_name.if_supports_color_2(Stream::Stderr, |x| x.bold()),
);
Some(err as &dyn Error)
}
Self::UnknownArchiveFormat { archive_file, err } => {
log::error!(
"failed to autodetect archive format for {}",
archive_file.if_supports_color(Stream::Stderr, |x| x.bold())
archive_file.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Self::ArchiveCreateError { archive_file, err } => {
log::error!(
"error creating archive `{}`",
archive_file.if_supports_color(Stream::Stderr, |x| x.bold())
archive_file.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Self::ArchiveExtractError { archive_file, err } => {
log::error!(
"error extracting archive `{}`",
archive_file.if_supports_color(Stream::Stderr, |x| x.bold())
archive_file.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Expand All @@ -600,24 +602,25 @@ impl ExpectedError {
} => {
log::error!(
"argument {} specified JSON file `{}` that couldn't be deserialized",
format!("--{arg_name}").if_supports_color(Stream::Stderr, |x| x.bold()),
file_name.if_supports_color(Stream::Stderr, |x| x.bold()),
format!("--{arg_name}").if_supports_color_2(Stream::Stderr, |x| x.bold()),
file_name.if_supports_color_2(Stream::Stderr, |x| x.bold()),
);
Some(err as &dyn Error)
}
Self::PathMapperConstructError { arg_name, err } => {
log::error!(
"argument {} specified `{}` that couldn't be read",
format!("--{arg_name}").if_supports_color(Stream::Stderr, |x| x.bold()),
err.input().if_supports_color(Stream::Stderr, |x| x.bold())
format!("--{arg_name}").if_supports_color_2(Stream::Stderr, |x| x.bold()),
err.input()
.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Self::CargoMetadataParseError { file_name, err } => {
let metadata_source = match file_name {
Some(path) => format!(
" from file `{}`",
path.if_supports_color(Stream::Stderr, |x| x.bold())
path.if_supports_color_2(Stream::Stderr, |x| x.bold())
),
None => "".to_owned(),
};
Expand All @@ -635,7 +638,7 @@ impl ExpectedError {
Self::BuildExecFailed { command, err } => {
log::error!(
"failed to execute `{}`",
command.if_supports_color(Stream::Stderr, |x| x.bold())
command.if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Expand All @@ -644,15 +647,15 @@ impl ExpectedError {
Some(code) => {
format!(
" with code {}",
code.if_supports_color(Stream::Stderr, |x| x.bold())
code.if_supports_color_2(Stream::Stderr, |x| x.bold())
)
}
None => "".to_owned(),
};

log::error!(
"command `{}` exited{}",
command.if_supports_color(Stream::Stderr, |x| x.bold()),
command.if_supports_color_2(Stream::Stderr, |x| x.bold()),
with_code_str,
);

Expand Down Expand Up @@ -689,8 +692,8 @@ impl ExpectedError {
} => {
log::error!(
"this repository requires nextest version {}, but the current version is {}",
required.if_supports_color(Stream::Stderr, |x| x.bold()),
current.if_supports_color(Stream::Stderr, |x| x.bold()),
required.if_supports_color_2(Stream::Stderr, |x| x.bold()),
current.if_supports_color_2(Stream::Stderr, |x| x.bold()),
);
if let Some(tool) = tool {
log::info!(
Expand All @@ -715,7 +718,7 @@ impl ExpectedError {
Self::UpdateError { err } => {
log::error!(
"failed to update nextest (please update manually by visiting <{}>)",
"https://get.nexte.st".if_supports_color(Stream::Stderr, |x| x.bold())
"https://get.nexte.st".if_supports_color_2(Stream::Stderr, |x| x.bold())
);
Some(err as &dyn Error)
}
Expand Down
6 changes: 4 additions & 2 deletions cargo-nextest/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

#[cfg(feature = "self-update")]
pub(crate) fn log_needs_update(level: log::Level, extra: &str) {
use owo_colors::{OwoColorize, Stream};
use crate::output::SupportsColorsV2;
use owo_colors::OwoColorize;

log::log!(
level,
"update nextest with {}{}",
"cargo nextest self update".if_supports_color(Stream::Stderr, |x| x.bold()),
"cargo nextest self update"
.if_supports_color_2(supports_color::Stream::Stderr, |x| x.bold()),
extra,
);
}
Expand Down
Loading

0 comments on commit d99a897

Please sign in to comment.