Skip to content

Commit

Permalink
Change Pipe to always use the test (uncolored) semantics
Browse files Browse the repository at this point in the history
Pipe only worked in test contexts before.
  • Loading branch information
niklas authored and jplatte committed Nov 7, 2022
1 parent 9edf205 commit 84b701d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl Builder {
let writer = match mem::take(&mut self.target) {
WritableTarget::Stderr => BufferWriter::stderr(self.is_test, color_choice),
WritableTarget::Stdout => BufferWriter::stdout(self.is_test, color_choice),
WritableTarget::Pipe(pipe) => BufferWriter::pipe(self.is_test, color_choice, pipe),
WritableTarget::Pipe(pipe) => BufferWriter::pipe(color_choice, pipe),
};

Writer {
Expand Down
23 changes: 9 additions & 14 deletions src/fmt/writer/termcolor/extern_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ impl Formatter {

pub(in crate::fmt::writer) struct BufferWriter {
inner: termcolor::BufferWriter,
test_target: Option<WritableTarget>,
uncolored_target: Option<WritableTarget>,
}

pub(in crate::fmt) struct Buffer {
inner: termcolor::Buffer,
has_test_target: bool,
has_uncolored_target: bool,
}

impl BufferWriter {
pub(in crate::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self {
BufferWriter {
inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()),
test_target: if is_test {
uncolored_target: if is_test {
Some(WritableTarget::Stderr)
} else {
None
Expand All @@ -94,7 +94,7 @@ impl BufferWriter {
pub(in crate::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self {
BufferWriter {
inner: termcolor::BufferWriter::stdout(write_style.into_color_choice()),
test_target: if is_test {
uncolored_target: if is_test {
Some(WritableTarget::Stdout)
} else {
None
Expand All @@ -103,30 +103,25 @@ impl BufferWriter {
}

pub(in crate::fmt::writer) fn pipe(
is_test: bool,
write_style: WriteStyle,
pipe: Box<Mutex<dyn io::Write + Send + 'static>>,
) -> Self {
BufferWriter {
// The inner Buffer is never printed from, but it is still needed to handle coloring and other formating
inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()),
test_target: if is_test {
Some(WritableTarget::Pipe(pipe))
} else {
None
},
uncolored_target: Some(WritableTarget::Pipe(pipe)),
}
}

pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
Buffer {
inner: self.inner.buffer(),
has_test_target: self.test_target.is_some(),
has_uncolored_target: self.uncolored_target.is_some(),
}
}

pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
if let Some(target) = &self.test_target {
if let Some(target) = &self.uncolored_target {
// This impl uses the `eprint` and `print` macros
// instead of `termcolor`'s buffer.
// This is so their output can be captured by `cargo test`
Expand Down Expand Up @@ -164,7 +159,7 @@ impl Buffer {

fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
// Ignore styles for test captured logs because they can't be printed
if !self.has_test_target {
if !self.has_uncolored_target {
self.inner.set_color(spec)
} else {
Ok(())
Expand All @@ -173,7 +168,7 @@ impl Buffer {

fn reset(&mut self) -> io::Result<()> {
// Ignore styles for test captured logs because they can't be printed
if !self.has_test_target {
if !self.has_uncolored_target {
self.inner.reset()
} else {
Ok(())
Expand Down

0 comments on commit 84b701d

Please sign in to comment.