use std::fs;
use tempfile::tempdir;
use super::RAGGED;
#[cfg(unix)]
const CLOSED_PIPE_FILES: usize = 500;
#[cfg(unix)]
const CLOSED_PIPE_PADDING: usize = 180;
#[cfg(not(unix))]
const CLOSED_PIPE_FILES: usize = 250;
#[cfg(not(unix))]
const CLOSED_PIPE_PADDING: usize = 80;
#[test]
fn a_closed_pipe_is_a_successful_early_exit() {
use std::{
io::Read as _,
process::{Command, Stdio},
};
let dir = tempdir().expect("create temporary directory");
let names: Vec<String> = (0..CLOSED_PIPE_FILES)
.map(|index| format!("{index:0>3}-{}.md", "p".repeat(CLOSED_PIPE_PADDING)))
.collect();
for name in &names {
fs::write(dir.path().join(name), RAGGED).expect("write fixture");
}
let mut child = Command::new(env!("CARGO_BIN_EXE_mdtablefix"))
.arg("--check")
.args(&names)
.current_dir(dir.path())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn mdtablefix");
let mut stdout = child.stdout.take().expect("child stdout is piped");
let mut first = [0u8; 1];
stdout.read_exact(&mut first).expect("read one byte");
drop(stdout);
let output = child.wait_with_output().expect("wait for mdtablefix");
let stderr = String::from_utf8_lossy(&output.stderr);
#[cfg(unix)]
assert_eq!(
output.status.code(),
Some(0),
"a closed pipe is an early exit, not a failure: {stderr}"
);
#[cfg(not(unix))]
assert!(
matches!(output.status.code(), Some(0 | 1)),
"a closed pipe is an early exit or a completed run, never an undocumented status: {stderr}"
);
assert!(
!stderr.contains("panicked"),
"the write failure is handled, not panicked on: {stderr}"
);
}