Skip to content

Commit

Permalink
fix: Improve debug output
Browse files Browse the repository at this point in the history
Fixes #142
  • Loading branch information
epage committed Jan 13, 2022
1 parent 8d67106 commit 44ba3ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::ffi;
use std::io;
use std::io::{Read, Write};
use std::ops::Deref;
use std::path;
use std::process;

Expand All @@ -22,7 +23,7 @@ use crate::output::OutputResult;
#[derive(Debug)]
pub struct Command {
cmd: process::Command,
stdin: Option<Vec<u8>>,
stdin: Option<bstr::BString>,
timeout: Option<std::time::Duration>,
}

Expand Down Expand Up @@ -82,7 +83,7 @@ impl Command {
where
S: Into<Vec<u8>>,
{
self.stdin = Some(buffer.into());
self.stdin = Some(bstr::BString::from(buffer.into()));
self
}

Expand Down Expand Up @@ -436,7 +437,7 @@ impl Command {
/// ```
pub fn output(&mut self) -> io::Result<process::Output> {
let spawn = self.spawn()?;
Self::wait_with_input_output(spawn, self.stdin.clone(), self.timeout)
Self::wait_with_input_output(spawn, self.stdin.as_deref().cloned(), self.timeout)
}

/// If `input`, write it to `child`'s stdin while also reading `child`'s
Expand Down Expand Up @@ -518,7 +519,7 @@ impl<'c> OutputOkExt for &'c mut Command {
} else {
let error = OutputError::new(output).set_cmd(format!("{:?}", self.cmd));
let error = if let Some(stdin) = self.stdin.as_ref() {
error.set_stdin(stdin.clone())
error.set_stdin(stdin.deref().clone())
} else {
error
};
Expand Down Expand Up @@ -559,7 +560,7 @@ impl<'c> OutputAssertExt for &'c mut Command {
};
let assert = Assert::new(output).append_context("command", format!("{:?}", self.cmd));
if let Some(stdin) = self.stdin.as_ref() {
assert.append_context("stdin", DebugBuffer::new(stdin.clone()))
assert.append_context("stdin", DebugBuffer::new(stdin.deref().clone()))
} else {
assert
}
Expand Down
10 changes: 6 additions & 4 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub type OutputResult = Result<process::Output, OutputError>;
#[derive(Debug)]
pub struct OutputError {
cmd: Option<String>,
stdin: Option<Vec<u8>>,
stdin: Option<bstr::BString>,
cause: OutputCause,
}

Expand Down Expand Up @@ -206,7 +206,7 @@ impl OutputError {

/// Add the `stdin` for additional context.
pub fn set_stdin(mut self, stdin: Vec<u8>) -> Self {
self.stdin = Some(stdin);
self.stdin = Some(bstr::BString::from(stdin));
self
}

Expand Down Expand Up @@ -336,12 +336,14 @@ impl<'a> fmt::Display for DebugBytes<'a> {

#[derive(Debug)]
pub(crate) struct DebugBuffer {
buffer: Vec<u8>,
buffer: bstr::BString,
}

impl DebugBuffer {
pub(crate) fn new(buffer: Vec<u8>) -> Self {
DebugBuffer { buffer }
DebugBuffer {
buffer: buffer.into(),
}
}
}

Expand Down

0 comments on commit 44ba3ec

Please sign in to comment.