Skip to content

Commit 4dc5e61

Browse files
committed
Regression test println!() panic message on ErrorKind::BrokenPipe
No existing test failed if the [`panic!()`][1] of the `println!()` family of functions was removed, or if its message was changed. So add such a test. [1] https://github.com/rust-lang/rust/blob/104f4300cfddbd956e32820ef202a732f06ec848/library/std/src/io/stdio.rs#L1007-L1009
1 parent 81be7b8 commit 4dc5e61

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// run-pass
2+
// check-run-results
3+
// ignore-windows
4+
// ignore-emscripten
5+
// ignore-fuchsia
6+
// ignore-horizon
7+
// ignore-android
8+
// normalize-stderr-test ".rs:\d+:\d+" -> ".rs:LL:CC"
9+
10+
// Test what the error message looks like when `println!()` panics because of
11+
// `std::io::ErrorKind::BrokenPipe`
12+
13+
#![feature(unix_sigpipe)]
14+
15+
use std::env;
16+
use std::process::{Command, Stdio};
17+
18+
#[unix_sigpipe = "sig_ign"]
19+
fn main() {
20+
let mut args = env::args();
21+
let me = args.next().unwrap();
22+
23+
if let Some(arg) = args.next() {
24+
// More than enough iterations to fill any pipe buffer. Normally this
25+
// loop will end with a panic more or less immediately.
26+
for _ in 0..65536 * 64 {
27+
println!("{arg}");
28+
}
29+
unreachable!("should have panicked because of BrokenPipe");
30+
}
31+
32+
// Set up a pipeline with a short-lived consumer and wait for it to finish.
33+
// This will produce the `println!()` panic message on stderr.
34+
let mut producer = Command::new(&me)
35+
.arg("this line shall appear exactly once on stdout")
36+
.stdout(Stdio::piped())
37+
.spawn()
38+
.unwrap();
39+
let mut consumer =
40+
Command::new("head").arg("-n1").stdin(producer.stdout.take().unwrap()).spawn().unwrap();
41+
consumer.wait().unwrap();
42+
producer.wait().unwrap();
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', library/std/src/io/stdio.rs:LL:CC
2+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this line shall appear exactly once on stdout

0 commit comments

Comments
 (0)