Merge pull request #9822 from dotCooCoo/fix/automation-args-handshake

Tests: automation root tests wait for the app to exit, not for a 30 s deadline
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-27 23:35:58 -08:00
commit c2b8d7ce75
3 files changed
+81 -43

No files matched your search

+7 -17
View File
@@ -1,5 +1,4 @@
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
/// Invalid roots must exit before desktop initialization on every platform, including a CI
/// machine with no display. A regression must fail rather than hang with a window open.
@@ -7,9 +6,14 @@ fn rejects(args: &[&str], message: &str) {
rejects_with_env(args, &[], message);
}
/// Runs the desktop binary without a display, so on Linux a regression exits with a display
/// error, which the exact stderr check rejects, instead of opening a window.
fn rejects_with_env(args: &[&str], env: &[(&str, &str)], message: &str) {
let mut child = Command::new(crate::bin())
let child = Command::new(crate::bin())
.args(args)
.env_remove("DISPLAY")
.env_remove("WAYLAND_DISPLAY")
.env_remove("WAYLAND_SOCKET")
.env_remove("VECTORCRAFT_CONTROL_PORT")
.env_remove("VECTORCRAFT_AUTOMATION_READ_ROOT")
.env_remove("VECTORCRAFT_AUTOMATION_WRITE_ROOT")
@@ -20,21 +24,7 @@ fn rejects_with_env(args: &[&str], env: &[(&str, &str)], message: &str) {
.stderr(Stdio::piped())
.spawn()
.unwrap();
// Long enough for the binary to start on a loaded machine (the workspace's other tests run
// meanwhile), short enough to catch a desktop that started instead of exiting.
let deadline = Instant::now() + Duration::from_secs(30);
let timed_out = loop {
if child.try_wait().unwrap().is_some() {
break false;
}
if Instant::now() >= deadline {
child.kill().unwrap();
break true;
}
std::thread::sleep(Duration::from_millis(10));
};
let out = child.wait_with_output().unwrap();
assert!(!timed_out, "{args:?}: desktop started or did not exit promptly");
let out = crate::exit_output(child, &format!("{args:?}: the desktop started instead of exiting"));
assert!(!out.status.success(), "{args:?}");
assert!(out.stdout.is_empty(), "{args:?}: {:?}", out.stdout);
// Exact stderr excludes unrelated startup warnings or display errors that could otherwise
+2 -26
View File
@@ -1,15 +1,12 @@
//! `vectorcraft --help` prints the usage and exits before the app starts (#2110): run from a
//! terminal, SSH or an agent's shell it used to open the window (or hang without a display).
use std::io::Read;
use std::process::{Command, Output, Stdio};
use std::sync::mpsc;
use std::time::{Duration, Instant};
/// Runs the desktop binary without a display and with a control port it cannot parse, so a
/// regression exits with that error instead of opening a window.
fn run(args: &[&str], env: &[(&str, &str)]) -> Output {
let mut child = Command::new(crate::bin())
let child = Command::new(crate::bin())
.args(args)
.env_remove("DISPLAY")
.env_remove("WAYLAND_DISPLAY")
@@ -22,28 +19,7 @@ fn run(args: &[&str], env: &[(&str, &str)]) -> Output {
.stderr(Stdio::piped())
.spawn()
.unwrap();
// Both pipes end when the binary exits, however long a loaded machine takes to start it. The
// deadline only stops a binary that is still running.
let deadline = Instant::now() + Duration::from_secs(120);
let stdout = read_to_end(child.stdout.take().unwrap());
let stderr = read_to_end(child.stderr.take().unwrap());
let recv = |rx: &mpsc::Receiver<Vec<u8>>| rx.recv_timeout(deadline.saturating_duration_since(Instant::now()));
let (Ok(stdout), Ok(stderr)) = (recv(&stdout), recv(&stderr)) else {
child.kill().unwrap();
panic!("{args:?}: the app started instead of printing its usage");
};
Output { status: child.wait().unwrap(), stdout, stderr }
}
/// Reads `pipe` to its end on another thread.
fn read_to_end(mut pipe: impl Read + Send + 'static) -> mpsc::Receiver<Vec<u8>> {
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let mut buf = Vec::new();
let _ = pipe.read_to_end(&mut buf);
let _ = tx.send(buf);
});
rx
crate::exit_output(child, &format!("{args:?}: the app started instead of printing its usage"))
}
fn prints_usage(args: &[&str], env: &[(&str, &str)]) {
+72
View File
@@ -2,6 +2,11 @@
// Unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md › Robustness).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::io::Read;
use std::process::{Child, Output};
use std::sync::mpsc;
use std::time::{Duration, Instant};
mod automation_args;
mod help;
@@ -12,3 +17,70 @@ fn bin() -> std::path::PathBuf {
.map(std::path::PathBuf::from)
.unwrap_or_else(|| env!("CARGO_BIN_EXE_vectorcraft").into())
}
/// Waits for `child`, spawned with piped stdout and stderr, to exit and returns its output. Both
/// pipes end when the binary exits, however long a loaded machine takes to start it. The deadline
/// only stops a binary that is still running: the test then fails with `started`.
fn exit_output(child: Child, started: &str) -> Output {
exit_output_by(child, started, Instant::now() + Duration::from_secs(120))
}
/// [`exit_output`] with the deadline given. A binary that has exited by the deadline passes even
/// when its output arrives after it; the output then has 10 seconds more to arrive.
fn exit_output_by(mut child: Child, started: &str, deadline: Instant) -> Output {
let stdout = read_to_end(child.stdout.take().unwrap());
let stderr = read_to_end(child.stderr.take().unwrap());
let (mut out, mut err) = (None, None);
if !receive([(&stdout, &mut out), (&stderr, &mut err)], deadline) && child.try_wait().unwrap().is_none() {
child.kill().unwrap();
child.wait().unwrap();
panic!("{started}");
}
receive([(&stdout, &mut out), (&stderr, &mut err)], Instant::now() + Duration::from_secs(10));
let (Some(stdout), Some(stderr)) = (out, err) else {
panic!("{started}: the binary exited, but its output pipes stayed open");
};
Output { status: child.wait().unwrap(), stdout, stderr }
}
/// A pipe's reader and the slot that holds its output once it arrives.
type Pipe<'a> = (&'a mpsc::Receiver<Vec<u8>>, &'a mut Option<Vec<u8>>);
/// Fills each empty slot from its reader, waiting until `until` at the latest. Returns whether
/// every slot is filled.
fn receive(pipes: [Pipe<'_>; 2], until: Instant) -> bool {
let mut filled = true;
for (rx, slot) in pipes {
if slot.is_none() {
*slot = rx.recv_timeout(until.saturating_duration_since(Instant::now())).ok();
}
filled &= slot.is_some();
}
filled
}
/// Reads `pipe` to its end on another thread.
fn read_to_end(mut pipe: impl Read + Send + 'static) -> mpsc::Receiver<Vec<u8>> {
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let mut buf = Vec::new();
let _ = pipe.read_to_end(&mut buf);
let _ = tx.send(buf);
});
rx
}
/// A binary that exits before the deadline passes even when a process it started keeps its
/// output pipes open past the deadline.
#[cfg(unix)]
#[test]
fn exit_output_accepts_an_exit_whose_output_arrives_after_the_deadline() {
use std::process::{Command, Stdio};
let mut child =
Command::new("sh").args(["-c", "sleep 1 & exit 0"]).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap();
// `sh` has exited; the `sleep` it started still holds both pipes for about a second.
child.wait().unwrap();
let out = exit_output_by(child, "an exited binary was reported as running", Instant::now());
assert!(out.status.success());
assert!(out.stdout.is_empty() && out.stderr.is_empty());
}