Skip to content

Commit

Permalink
Add option to unify stdout and stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Dec 9, 2020
1 parent 2b98bd1 commit 3d467c0
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ fn test(args: &ArgMatches) -> i32 {
}

fn exec(args: &ArgMatches) -> i32 {
let redirect_err_to_out = args.is_present("redirect_err_to_out");
let command = args
.values_of("command")
.expect("A command to execute has been supplied");
Expand Down Expand Up @@ -305,16 +306,31 @@ fn exec(args: &ArgMatches) -> i32 {
});
let mut child_stderr = BufReader::new(child.stderr.take().unwrap());
let err_piper = thread::spawn(move || {
let out = stderr();
let mut buf = vec![];
while let Ok(count) = child_stderr.read_until(b'\n', &mut buf) {
if count > 0 {
let mut lock = out.lock();
lock.write(&buf[0..count]).unwrap_or_default();
buf.clear();
lock.flush().unwrap_or_default();
} else {
break;
if redirect_err_to_out {
let out = stdout();
let mut buf = vec![];
while let Ok(count) = child_stderr.read_until(b'\n', &mut buf) {
if count > 0 {
let mut lock = out.lock();
lock.write(&buf[0..count]).unwrap_or_default();
buf.clear();
lock.flush().unwrap_or_default();
} else {
break;
}
}
} else {
let out = stderr();
let mut buf = vec![];
while let Ok(count) = child_stderr.read_until(b'\n', &mut buf) {
if count > 0 {
let mut lock = out.lock();
lock.write(&buf[0..count]).unwrap_or_default();
buf.clear();
lock.flush().unwrap_or_default();
} else {
break;
}
}
}
});
Expand Down Expand Up @@ -557,6 +573,12 @@ impl LocalizedArgs {
)
.subcommand(
self.add_export_args(SubCommand::with_name("exec"))
.arg(
Arg::with_name("redirect_err_to_out")
.long("redirect-err-to-out")
.takes_value(false)
.help("Redirects the child processes STDERR to STDOUT, useful in cases where buffering is corrupting JUXR's export")
)
.about("Runs a command that generates JUnit XML Reports and exports them (and any referenced attachments) to STDOUT before propagating the invoked command's exit code")
.arg(
Arg::with_name("command")
Expand Down

0 comments on commit 3d467c0

Please sign in to comment.