Skip to content

Commit 6ae66ec

Browse files
authored
[nextest-runner] switch reporter to WriteStr (#2737)
I'd like to try using indicatif's println, which requires a string. Complete this long-standing migration.
1 parent 5619aae commit 6ae66ec

File tree

12 files changed

+152
-157
lines changed

12 files changed

+152
-157
lines changed

cargo-nextest/src/dispatch/execution.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use owo_colors::OwoColorize;
4747
use semver::Version;
4848
use std::{
4949
env::VarError,
50-
io::Write,
5150
sync::{Arc, OnceLock},
5251
};
5352
use tracing::{Level, info, warn};
@@ -805,7 +804,7 @@ impl ArchiveApp {
805804
output_file,
806805
|event| {
807806
reporter.report_event(event, &mut writer)?;
808-
writer.flush()
807+
writer.write_str_flush()
809808
},
810809
redactor.clone(),
811810
)

cargo-nextest/src/output.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ pub enum OutputWriter {
334334
#[cfg(test)]
335335
Test {
336336
/// stdout capture
337-
stdout: Vec<u8>,
337+
stdout: String,
338338
/// stderr capture
339-
stderr: Vec<u8>,
339+
stderr: String,
340340
},
341341
}
342342

@@ -378,33 +378,15 @@ pub(crate) enum StdoutWriter<'a> {
378378
_lifetime: PhantomData<&'a ()>,
379379
},
380380
#[cfg(test)]
381-
Test { buf: &'a mut Vec<u8> },
382-
}
383-
384-
impl Write for StdoutWriter<'_> {
385-
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
386-
match self {
387-
Self::Normal { buf, .. } => buf.write(data),
388-
#[cfg(test)]
389-
Self::Test { buf } => buf.write(data),
390-
}
391-
}
392-
393-
fn flush(&mut self) -> std::io::Result<()> {
394-
match self {
395-
Self::Normal { buf, .. } => buf.flush(),
396-
#[cfg(test)]
397-
Self::Test { .. } => Ok(()),
398-
}
399-
}
381+
Test { buf: &'a mut String },
400382
}
401383

402384
impl WriteStr for StdoutWriter<'_> {
403385
fn write_str(&mut self, s: &str) -> io::Result<()> {
404386
match self {
405-
Self::Normal { buf, .. } => buf.write_all(s.as_bytes()),
387+
Self::Normal { buf, .. } => buf.write_str(s),
406388
#[cfg(test)]
407-
Self::Test { buf } => buf.write_all(s.as_bytes()),
389+
Self::Test { buf } => buf.write_str(s),
408390
}
409391
}
410392

@@ -423,19 +405,22 @@ pub(crate) enum StderrWriter<'a> {
423405
_lifetime: PhantomData<&'a ()>,
424406
},
425407
#[cfg(test)]
426-
Test { buf: &'a mut Vec<u8> },
408+
Test { buf: &'a mut String },
427409
}
428410

429-
impl Write for StderrWriter<'_> {
430-
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
411+
impl WriteStr for StderrWriter<'_> {
412+
fn write_str(&mut self, s: &str) -> io::Result<()> {
431413
match self {
432-
Self::Normal { buf, .. } => buf.write(data),
414+
Self::Normal { buf, .. } => buf.write_str(s),
433415
#[cfg(test)]
434-
Self::Test { buf } => buf.write(data),
416+
Self::Test { buf } => {
417+
buf.push_str(s);
418+
Ok(())
419+
}
435420
}
436421
}
437422

438-
fn flush(&mut self) -> std::io::Result<()> {
423+
fn write_str_flush(&mut self) -> io::Result<()> {
439424
match self {
440425
Self::Normal { buf, .. } => buf.flush(),
441426
#[cfg(test)]

cargo-nextest/src/reuse_build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use nextest_runner::{
1212
ArchiveFormat, ArchiveReporter, ExtractDestination, MetadataKind, MetadataWithRemap,
1313
PathMapper, ReuseBuildInfo, ReusedBinaryList, ReusedCargoMetadata,
1414
},
15+
write_str::WriteStr,
1516
};
16-
use std::io::Write;
1717
use tracing::warn;
1818

1919
#[derive(Debug, Default, Args)]
@@ -168,7 +168,7 @@ impl ReuseBuildOpts {
168168
dest,
169169
|event| {
170170
reporter.report_event(event, &mut writer)?;
171-
writer.flush()
171+
writer.write_str_flush()
172172
},
173173
workspace_remap.as_deref(),
174174
)

nextest-runner/src/indenter.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ impl<'a, D: ?Sized> Indented<'a, D> {
100100
self
101101
}
102102

103+
/// Skip indenting the initial line.
104+
///
105+
/// This is useful when you've already written some content on the current line
106+
/// and want to start indenting only from the next line onwards.
107+
pub fn skip_initial(mut self) -> Self {
108+
self.needs_indent = false;
109+
self
110+
}
111+
103112
/// Returns the inner writer.
104113
pub fn into_inner(self) -> &'a mut D {
105114
self.inner

nextest-runner/src/reporter/displayer/formatters.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ use crate::{
1111
events::{CancelReason, FinalRunStats, RunStatsFailureKind},
1212
helpers::Styles,
1313
},
14+
write_str::WriteStr,
1415
};
1516
use owo_colors::OwoColorize;
16-
use std::{
17-
fmt,
18-
io::{self, Write},
19-
time::Duration,
20-
};
17+
use std::{fmt, io, time::Duration};
2118

2219
pub(super) struct DisplayBracketedHhMmSs(pub(super) Duration);
2320

@@ -102,7 +99,7 @@ pub(super) fn write_skip_counts(
10299
skip_counts: &SkipCounts,
103100
default_filter: &CompiledDefaultFilter,
104101
styles: &Styles,
105-
writer: &mut dyn Write,
102+
writer: &mut dyn WriteStr,
106103
) -> io::Result<()> {
107104
if skip_counts.skipped_tests > 0 || skip_counts.skipped_binaries > 0 {
108105
write!(writer, " (")?;
@@ -154,7 +151,7 @@ fn write_skip_counts_impl(
154151
skipped_tests: usize,
155152
skipped_binaries: usize,
156153
styles: &Styles,
157-
writer: &mut dyn Write,
154+
writer: &mut dyn WriteStr,
158155
) -> io::Result<()> {
159156
// X tests and Y binaries skipped, or X tests skipped, or Y binaries skipped.
160157
if skipped_tests > 0 && skipped_binaries > 0 {
@@ -188,7 +185,7 @@ fn write_skip_counts_impl(
188185
pub(super) fn write_final_warnings(
189186
final_stats: FinalRunStats,
190187
styles: &Styles,
191-
writer: &mut dyn Write,
188+
writer: &mut dyn WriteStr,
192189
) -> io::Result<()> {
193190
match final_stats {
194191
FinalRunStats::Failed(RunStatsFailureKind::Test {
@@ -224,7 +221,7 @@ fn write_final_warnings_for_failure(
224221
not_run: usize,
225222
reason: Option<CancelReason>,
226223
styles: &Styles,
227-
writer: &mut dyn Write,
224+
writer: &mut dyn WriteStr,
228225
) -> io::Result<()> {
229226
match reason {
230227
Some(reason @ CancelReason::TestFailure | reason @ CancelReason::TestFailureImmediate) => {
@@ -379,7 +376,7 @@ mod tests {
379376
}
380377

381378
fn skip_counts_str(skip_counts: &SkipCounts, override_section: bool) -> String {
382-
let mut buf = Vec::new();
379+
let mut buf = String::new();
383380
write_skip_counts(
384381
skip_counts,
385382
&CompiledDefaultFilter {
@@ -395,7 +392,7 @@ mod tests {
395392
&mut buf,
396393
)
397394
.unwrap();
398-
String::from_utf8(buf).unwrap()
395+
buf
399396
}
400397

401398
#[test]
@@ -449,9 +446,9 @@ mod tests {
449446
}
450447

451448
fn final_warnings_for(stats: FinalRunStats) -> String {
452-
let mut buf: Vec<u8> = Vec::new();
449+
let mut buf = String::new();
453450
let styles = Styles::default();
454451
write_final_warnings(stats, &styles, &mut buf).unwrap();
455-
String::from_utf8(buf).unwrap()
452+
buf
456453
}
457454
}

0 commit comments

Comments
 (0)