Skip to content

Commit

Permalink
[nextest-runner] accept should_colorize as part of TestReporterBuilder
Browse files Browse the repository at this point in the history
This makes life simpler for some upcoming work.
  • Loading branch information
sunshowers committed Dec 5, 2024
1 parent bb5159a commit 1833ee8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
19 changes: 9 additions & 10 deletions cargo-nextest/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,9 +1013,11 @@ struct TestReporterOpts {
}

impl TestReporterOpts {
fn to_builder(&self, no_capture: bool) -> TestReporterBuilder {
fn to_builder(&self, no_capture: bool, should_colorize: bool) -> TestReporterBuilder {
let mut builder = TestReporterBuilder::default();
builder.set_no_capture(no_capture);
builder.set_colorize(should_colorize);

if let Some(failure_output) = self.failure_output {
builder.set_failure_output(failure_output.into());
}
Expand Down Expand Up @@ -1772,19 +1774,16 @@ impl App {
let test_list = self.build_test_list(&ctx, binary_list, test_filter_builder, &ecx)?;

let output = output_writer.reporter_output();
let should_colorize = self
.base
.output
.color
.should_colorize(supports_color::Stream::Stderr);

let mut reporter = reporter_opts
.to_builder(no_capture)
.to_builder(no_capture, should_colorize)
.set_verbose(self.base.output.verbose)
.build(&test_list, &profile, output, structured_reporter);
if self
.base
.output
.color
.should_colorize(supports_color::Stream::Stderr)
{
reporter.colorize();
}

let handler = SignalHandlerKind::Standard;
let runner_builder = match runner_opts.to_builder(cap_strat) {
Expand Down
17 changes: 11 additions & 6 deletions nextest-runner/src/reporter/displayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ pub enum ReporterStderr<'a> {
#[derive(Debug, Default)]
pub struct TestReporterBuilder {
no_capture: bool,
should_colorize: bool,
failure_output: Option<TestOutputDisplay>,
success_output: Option<TestOutputDisplay>,
status_level: Option<StatusLevel>,
Expand All @@ -187,6 +188,12 @@ impl TestReporterBuilder {
self
}

/// Set to true if the reporter should colorize output.
pub fn set_colorize(&mut self, should_colorize: bool) -> &mut Self {
self.should_colorize = should_colorize;
self
}

/// Sets the conditions under which test failures are output.
pub fn set_failure_output(&mut self, failure_output: TestOutputDisplay) -> &mut Self {
self.failure_output = Some(failure_output);
Expand Down Expand Up @@ -234,7 +241,10 @@ impl TestReporterBuilder {
output: ReporterStderr<'a>,
structured_reporter: StructuredReporter<'a>,
) -> TestReporter<'a> {
let styles = Box::default();
let mut styles: Box<Styles> = Box::default();
if self.should_colorize {
styles.colorize();
}

let aggregator = EventAggregator::new(profile);

Expand Down Expand Up @@ -491,11 +501,6 @@ pub struct TestReporter<'a> {
}

impl<'a> TestReporter<'a> {
/// Colorizes output.
pub fn colorize(&mut self) {
self.inner.styles.colorize();
}

/// Report a test event.
pub fn report_event(&mut self, event: TestEvent<'a>) -> Result<(), WriteEventError> {
self.write_event(event)
Expand Down

0 comments on commit 1833ee8

Please sign in to comment.