Skip to content

Commit c163d4e

Browse files
committed
Output subprocess stderr on failure
To make this work, have the test programs flush stdout before abort. stdout is block-buffered if it is a pipe.
1 parent 6ab5fa4 commit c163d4e

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

tests/helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ static int trace(int rc, const char *str) {
1717
static int require(int expect_rc, int got_rc, const char *str) {
1818
if (expect_rc != got_rc) {
1919
printf("REQUIRED(%s) failed: wanted=%d, got=%d\n", str, expect_rc, got_rc);
20+
fflush(stdout);
21+
fflush(stderr);
2022
abort();
2123
}
2224
return got_rc;

tests/runner.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn server() {
352352
.spawn()
353353
.unwrap(),
354354
));
355-
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
355+
wait_for_stdout(&mut openssl_server, "openssl server", b"listening\n");
356356
curl(port);
357357

358358
let openssl_output = print_output(openssl_server.wait_with_timeout());
@@ -371,7 +371,7 @@ fn server() {
371371
.spawn()
372372
.unwrap(),
373373
));
374-
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
374+
wait_for_stdout(&mut rustls_server, "rustls server", b"listening\n");
375375
curl(port);
376376

377377
let rustls_output = print_output(rustls_server.wait_with_timeout());
@@ -392,7 +392,7 @@ fn server() {
392392
.spawn()
393393
.unwrap(),
394394
));
395-
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
395+
wait_for_stdout(&mut openssl_server, "openssl server", b"listening\n");
396396
curl(port);
397397

398398
let openssl_output = print_output(openssl_server.wait_with_timeout());
@@ -411,7 +411,7 @@ fn server() {
411411
.spawn()
412412
.unwrap(),
413413
));
414-
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
414+
wait_for_stdout(&mut rustls_server, "rustls server", b"listening\n");
415415
curl(port);
416416

417417
let rustls_output = print_output(rustls_server.wait_with_timeout());
@@ -457,7 +457,7 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
457457
.spawn()
458458
.unwrap(),
459459
));
460-
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
460+
wait_for_stdout(&mut openssl_server, "openssl server", b"listening\n");
461461
connect(port, key_type, sig_algs, version_flag);
462462

463463
let openssl_output = print_output(openssl_server.wait_with_timeout());
@@ -476,7 +476,7 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
476476
.spawn()
477477
.unwrap(),
478478
));
479-
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
479+
wait_for_stdout(&mut rustls_server, "rustls server", b"listening\n");
480480
connect(port, key_type, sig_algs, version_flag);
481481

482482
let rustls_output = print_output(rustls_server.wait_with_timeout());
@@ -798,13 +798,18 @@ fn wait_for_port(port: u16) -> Option<()> {
798798
/// To ensure this function can be used several times in succession
799799
/// on a given `Child`, this must not read bytes from its `stdout`
800800
/// that appear after `expected`.
801-
fn wait_for_stdout(stream: &mut Child, expected: &[u8]) {
801+
fn wait_for_stdout(proc: &mut KillOnDrop, which: &str, expected: &[u8]) {
802802
let mut buffer = Vec::with_capacity(128);
803+
let child = proc.0.as_mut().unwrap();
803804

804805
loop {
805806
let mut input = [0u8];
806-
let new = stream.stdout.as_mut().unwrap().read(&mut input).unwrap();
807-
assert_eq!(new, 1, "stdout EOF -- read so far {buffer:?}");
807+
let new = child.stdout.as_mut().unwrap().read(&mut input).unwrap();
808+
if new != 1 {
809+
println!("{which} child stdout premature EOF -- read so far {buffer:?}");
810+
print_output(proc.take_inner().wait_with_output().unwrap());
811+
panic!();
812+
}
808813
buffer.push(input[0]);
809814

810815
if buffer.ends_with(expected) {
@@ -813,13 +818,13 @@ fn wait_for_stdout(stream: &mut Child, expected: &[u8]) {
813818

814819
assert!(
815820
buffer.len() < 128,
816-
"{expected:?} did not appear in first part of {stream:?} (instead it was {buffer:?}"
821+
"{expected:?} did not appear in first part of {which} {child:?} (instead it was {buffer:?}"
817822
);
818823

819-
match stream.try_wait() {
820-
Ok(Some(status)) => panic!("process exited already with {status}"),
824+
match child.try_wait() {
825+
Ok(Some(status)) => panic!("{which} process exited already with {status}"),
821826
Ok(None) => {}
822-
Err(e) => panic!("subprocess broken {e:?}"),
827+
Err(e) => panic!("{which} subprocess broken {e:?}"),
823828
};
824829
}
825830
}

0 commit comments

Comments
 (0)