Skip to content

Commit e21ed6c

Browse files
committed
Print messges to stdout int a way to make cargo test happy
Use `println!` to print messages to stdout, or `cargo test` would not hide the output, see #23
1 parent 92d2c68 commit e21ed6c

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/child.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::CmdResult;
22
use log::info;
3-
use os_pipe::PipeReader;
4-
use std::io::{self, BufRead, BufReader, Error, ErrorKind, Read, Result, Write};
3+
use os_pipe::{self, PipeReader};
4+
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Result};
55
use std::process::{Child, ExitStatus};
66
use std::thread::JoinHandle;
77
use CmdChild::{ProcChild, SyncChild, ThreadChild};
@@ -43,6 +43,12 @@ impl CmdChild {
4343
cmd,
4444
} => {
4545
Self::log_stderr_output(stderr);
46+
if let Some(stdout) = child.stdout.take() {
47+
BufReader::new(stdout)
48+
.lines()
49+
.filter_map(|line| line.ok())
50+
.for_each(|line| println!("{}", line));
51+
}
4652
let status = child.wait()?;
4753
if !status.success() && (is_last || pipefail) {
4854
return Err(Self::status_to_io_error(
@@ -73,7 +79,7 @@ impl CmdChild {
7379
if let Some(mut out) = output {
7480
let mut buf = vec![];
7581
check_result(out.read_to_end(&mut buf).map(|_| ()))?;
76-
check_result(io::stdout().write_all(&buf[..]))?;
82+
println!("{}", String::from_utf8_lossy(&buf));
7783
}
7884
}
7985
}

src/process.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl GroupCmds {
140140
pub fn spawn(mut self) -> Result<WaitCmd> {
141141
assert_eq!(self.group_cmds.len(), 1);
142142
let mut cmds = self.group_cmds.pop().unwrap().0;
143-
let ret = cmds.spawn(&mut self.current_dir, false);
143+
let ret = cmds.spawn(&mut self.current_dir);
144144
if let Err(ref err) = ret {
145145
error!("Spawning {} failed, Error: {}", cmds.get_full_cmds(), err);
146146
}
@@ -150,7 +150,7 @@ impl GroupCmds {
150150
pub fn spawn_with_output(mut self) -> Result<WaitFun> {
151151
assert_eq!(self.group_cmds.len(), 1);
152152
let mut cmds = self.group_cmds.pop().unwrap().0;
153-
match cmds.spawn(&mut self.current_dir, true) {
153+
match cmds.spawn(&mut self.current_dir) {
154154
Ok(ret) => Ok(WaitFun(ret.0)),
155155
Err(err) => {
156156
error!("Spawning {} failed, Error: {}", cmds.get_full_cmds(), err);
@@ -181,7 +181,7 @@ impl Cmds {
181181
&self.full_cmds
182182
}
183183

184-
fn spawn(&mut self, current_dir: &mut String, with_output: bool) -> Result<WaitCmd> {
184+
fn spawn(&mut self, current_dir: &mut String) -> Result<WaitCmd> {
185185
if std::env::var("CMD_LIB_DEBUG") == Ok("1".into()) {
186186
debug!("Running {} ...", self.get_full_cmds());
187187
}
@@ -200,19 +200,19 @@ impl Cmds {
200200
} else {
201201
cmd.setup_redirects(&mut last_pipe_in, None)?;
202202
}
203-
let child = cmd.spawn(current_dir, with_output)?;
203+
let child = cmd.spawn(current_dir)?;
204204
children.push(child);
205205
}
206206

207207
Ok(WaitCmd(children))
208208
}
209209

210210
fn run_cmd(&mut self, current_dir: &mut String) -> CmdResult {
211-
self.spawn(current_dir, false)?.wait_result_nolog()
211+
self.spawn(current_dir)?.wait_result_nolog()
212212
}
213213

214214
fn run_fun(&mut self, current_dir: &mut String) -> FunResult {
215-
WaitFun(self.spawn(current_dir, true)?.0).wait_result_nolog()
215+
WaitFun(self.spawn(current_dir)?.0).wait_result_nolog()
216216
}
217217
}
218218

@@ -439,7 +439,7 @@ impl Cmd {
439439
}
440440
}
441441

442-
fn spawn(mut self, current_dir: &mut String, with_output: bool) -> Result<CmdChild> {
442+
fn spawn(mut self, current_dir: &mut String) -> Result<CmdChild> {
443443
let arg0 = self.arg0();
444444
let full_cmd = self.debug_str();
445445
if arg0 == "cd" {
@@ -463,12 +463,10 @@ impl Cmd {
463463
},
464464
stdout: if let Some(redirect_out) = self.stdout_redirect.take() {
465465
redirect_out
466-
} else if with_output {
466+
} else {
467467
let (pipe_reader, pipe_writer) = os_pipe::pipe()?;
468468
new_pipe_out = Some(pipe_reader);
469469
CmdOut::CmdPipe(pipe_writer)
470-
} else {
471-
CmdOut::CmdPipe(os_pipe::dup_stdout()?)
472470
},
473471
stderr: if let Some(redirect_err) = self.stderr_redirect.take() {
474472
redirect_err
@@ -510,7 +508,7 @@ impl Cmd {
510508
// update stdout
511509
if let Some(redirect_out) = self.stdout_redirect.take() {
512510
cmd.stdout(redirect_out);
513-
} else if with_output {
511+
} else {
514512
cmd.stdout(Stdio::piped());
515513
}
516514

0 commit comments

Comments
 (0)