Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where stdout was copying stderr #3

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Fix stderr copying to stdout bug (https://github.com/schneems/fun_run/pull/3)

## 0.1.0

- First release
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ regex = "1"

[features]
which_problem = ["dep:which_problem"]

[dev-dependencies]
pretty_assertions = "1"
65 changes: 40 additions & 25 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn output_and_write_streams<OW: Write + Send, EW: Write + Send>(
let stdout_thread = mem::take(&mut child.stdout).map(|mut child_stdout| {
scope.spawn(move || std::io::copy(&mut child_stdout, &mut stdout))
});
let stderr_thread = mem::take(&mut child.stdout).map(|mut child_stderr| {
let stderr_thread = mem::take(&mut child.stderr).map(|mut child_stderr| {
scope.spawn(move || std::io::copy(&mut child_stderr, &mut stderr))
});

Expand Down Expand Up @@ -53,14 +53,43 @@ pub(crate) fn output_and_write_streams<OW: Write + Send, EW: Write + Send>(
})
}

/// Constructs a writer that writes to two other writers. Similar to the UNIX `tee` command.
pub(crate) fn tee<A: io::Write, B: io::Write>(a: A, b: B) -> TeeWrite<A, B> {
TeeWrite {
inner_a: a,
inner_b: b,
}
}

/// A tee writer that was created with the [`tee`] function.
#[derive(Debug, Clone)]
pub(crate) struct TeeWrite<A: io::Write, B: io::Write> {
inner_a: A,
inner_b: B,
}

impl<A: io::Write, B: io::Write> io::Write for TeeWrite<A, B> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner_a.write_all(buf)?;
self.inner_b.write_all(buf)?;
Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
self.inner_a.flush()?;
self.inner_b.flush()
}
}

#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_str_eq;
use std::process::Command;

#[test]
#[cfg(unix)]
fn test_output_and_write_streams() {
fn test_output_and_write_streams_stdout() {
let mut stdout_buf = Vec::new();
let mut stderr_buf = Vec::new();

Expand All @@ -76,32 +105,18 @@ mod test {
assert_eq!(output.stdout, "Hello World!".as_bytes());
assert_eq!(output.stderr, Vec::<u8>::new());
}
}

/// Constructs a writer that writes to two other writers. Similar to the UNIX `tee` command.
pub(crate) fn tee<A: io::Write, B: io::Write>(a: A, b: B) -> TeeWrite<A, B> {
TeeWrite {
inner_a: a,
inner_b: b,
}
}
#[test]
#[cfg(unix)]
fn test_output_and_write_streams_stderr() {
let mut stdout_buf = Vec::new();
let mut stderr_buf = Vec::new();

/// A tee writer that was created with the [`tee`] function.
#[derive(Debug, Clone)]
pub(crate) struct TeeWrite<A: io::Write, B: io::Write> {
inner_a: A,
inner_b: B,
}
let mut cmd = Command::new("bash");
cmd.args(["-c", "echo -n Hello World! >&2"]);

impl<A: io::Write, B: io::Write> io::Write for TeeWrite<A, B> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner_a.write_all(buf)?;
self.inner_b.write_all(buf)?;
Ok(buf.len())
}
let _ = output_and_write_streams(&mut cmd, &mut stdout_buf, &mut stderr_buf).unwrap();

fn flush(&mut self) -> io::Result<()> {
self.inner_a.flush()?;
self.inner_b.flush()
assert_str_eq!(&String::from_utf8_lossy(&stderr_buf), "Hello World!");
}
}