Elffuss BraidHub

utopia-ia/elffuss-braid public

verify: the watchdog still takes the arguments braid 0.1.1 gives it

Braid 0.1.1 starts its watchdog after the test, with the test's process
group before the directory, and tells it nothing but done. If the braid
command is replaced by a newer one while a 0.1.1 verify runs, that verify
now still gets a watchdog for its run, instead of an argument error.
Changeset8e35586609e34af5ce718cc0bce07cf978f6e3e264e3f8ee212bfe1ebc7cef83
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 16:45 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 16:45 +02:00
Parents5f27b5ba2310

Browse the files at this changeset

3 files changed

modified crates/cli/src/main.rs +9 −3
@@ -327,8 +327,11 @@
327327 /// Kill a test run's process group when verify goes away (internal).
328328 #[command(name = verify::WATCHDOG, hide = true)]
329329 VerifyWatchdog {
330- /// The run's scratch directory.
331- dir: PathBuf,
330+ /// The run's scratch directory; or first the test's process group,
331+ /// as braid 0.1.1 starts its watchdog.
332+ first: std::ffi::OsString,
333+ /// The run's scratch directory, after the process group.
334+ dir: Option<PathBuf>,
332335 },
333336 }
334337
@@ -542,7 +545,10 @@
542545 Command::Change { action } => work::change(cli, action),
543546 Command::Revert(args) => revert::revert(cli, args),
544547 Command::Verify(args) => verify::verify(cli, args),
545- Command::VerifyWatchdog { dir } => verify::watchdog(dir),
548+ Command::VerifyWatchdog { first, dir } => match dir {
549+ None => verify::watchdog(None, Path::new(first)),
550+ Some(dir) => verify::watchdog(Some(first), dir),
551+ },
546552 }
547553 }
548554
modified crates/cli/src/verify.rs +15 −7
@@ -741,23 +741,23 @@
741741 })
742742 }
743743
744-/// The watchdog of one test run in `dir`, as [`Watchdog`] starts it.
745-pub(crate) fn watchdog(dir: &Path) -> Result<ExitCode> {
744+/// The watchdog of one test run in `dir`, as [`Watchdog`] starts it. Braid
745+/// 0.1.1 started its watchdog with the test's process group, `given`,
746+/// before the directory, after the test, and said nothing but `done`: a
747+/// newer braid put in its place mid-run still watches that run.
748+pub(crate) fn watchdog(given: Option<&std::ffi::OsStr>, dir: &Path) -> Result<ExitCode> {
746749 {
747750 let mut out = io::stdout().lock();
748751 let _ = out.write_all(READY).and_then(|()| out.flush());
749752 }
750- let mut group = None;
753+ let mut group = given.and_then(|id| group_id(id.as_encoded_bytes()));
751754 for line in io::stdin().lock().split(b'\n') {
752755 let Ok(line) = line else { break };
753756 if line == b"done" {
754757 return Ok(ExitCode::SUCCESS);
755758 }
756759 if let Some(id) = line.strip_prefix(b"group ") {
757- group = std::str::from_utf8(id)
758- .ok()
759- .and_then(|id| id.parse::<i32>().ok())
760- .filter(|id| *id > 1);
760+ group = group_id(id);
761761 }
762762 }
763763 // Braid went away while the tests ran, or before they started.
@@ -783,6 +783,14 @@
783783 Ok(ExitCode::SUCCESS)
784784 }
785785
786+/// A process group's id, as the watchdog is given it: a number above 1.
787+fn group_id(text: &[u8]) -> Option<i32> {
788+ std::str::from_utf8(text)
789+ .ok()
790+ .and_then(|id| id.parse::<i32>().ok())
791+ .filter(|id| *id > 1)
792+}
793+
786794 /// Whether an environment variable is one of the hub's credentials or
787795 /// settings, which the tests never see: `BRAID_TOKEN` and every variable
788796 /// whose name starts with `BRAID_HUB`.
modified crates/cli/tests/verify.rs +58 −0
@@ -911,3 +911,61 @@
911911 "the test's shell {shell} outlived its watchdog"
912912 );
913913 }
914+
915+#[cfg(unix)]
916+#[test]
917+fn the_watchdog_still_takes_the_arguments_braid_0_1_1_gives() {
918+ use std::io::Write;
919+ use std::os::unix::process::{CommandExt, ExitStatusExt};
920+ // As braid 0.1.1 starts it: after the test, with the test's process
921+ // group and then the directory, and told nothing but `done`.
922+ let start = |test: &std::process::Child, dir: &Path| {
923+ Command::new(env!("CARGO_BIN_EXE_braid"))
924+ .arg("__verify-watchdog")
925+ .arg(test.id().to_string())
926+ .arg(dir)
927+ .stdin(std::process::Stdio::piped())
928+ .stdout(std::process::Stdio::null())
929+ .stderr(std::process::Stdio::null())
930+ .process_group(0)
931+ .spawn()
932+ .unwrap()
933+ };
934+ let sleeper = || {
935+ Command::new("sleep")
936+ .arg("60")
937+ .process_group(0)
938+ .spawn()
939+ .unwrap()
940+ };
941+ let base = std::env::temp_dir();
942+ let dir = |n: u32| {
943+ let dir = base.join(format!("braid-verify-old-{}-{n}", std::process::id()));
944+ std::fs::create_dir(&dir).unwrap();
945+ dir
946+ };
947+
948+ // That braid went away.
949+ let gone = dir(1);
950+ let mut test = sleeper();
951+ let mut watchdog = start(&test, &gone);
952+ drop(watchdog.stdin.take());
953+ watchdog.wait().unwrap();
954+ assert_eq!(test.wait().unwrap().signal(), Some(9));
955+ assert!(!gone.exists());
956+
957+ // That braid finished its run.
958+ let kept = dir(2);
959+ let mut test = sleeper();
960+ let mut watchdog = start(&test, &kept);
961+ let mut input = watchdog.stdin.take().unwrap();
962+ input.write_all(b"done\n").unwrap();
963+ drop(input);
964+ watchdog.wait().unwrap();
965+ assert!(test.try_wait().unwrap().is_none());
966+ assert!(kept.is_dir());
967+ // This test's own process, by its id.
968+ test.kill().unwrap();
969+ test.wait().unwrap();
970+ std::fs::remove_dir(&kept).unwrap();
971+}