From f8690fba078da79f40f12e86ba4453a016358766 Mon Sep 17 00:00:00 2001 From: blockmandev <194144408+blockmandev@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:50:47 -0600 Subject: [PATCH] tests: scripts a test runs are written by sh, not by the test process agent::agy::tests::a_whole_turn_reports_these_events_and_this_outcome failed once on ubuntu-latest (PR 97, run 43751808555) with no events at all, and passed on a rerun of the same commit. The test asserted the events before the turn's result, so the log could not say why the turn produced nothing. Two changes: - That test's event assertion now prints how the turn ended, so a turn that never started says why. - The probable cause is removed everywhere it can happen. A test binary runs its tests on many threads; a program another thread starts while this process holds a script open for writing is handed that open file until it has started, and running the script in that moment fails with ETXTBSY ("Text file busy"). This is inferred, not shown by the log: it fits a turn that fails before its first event. The new crate::test_support::write_script (tests only, off Windows) has a short-lived sh write the file and chmod it, so the test process never opens it. All 13 test sites that wrote a script and then ran it use it: agy_tests, agy/bridged_tests, claude_code/tests (6), codex/tests, hooks/engine (2), toolbox/reading_tests and verify. self_update.rs is production code and is unchanged. The Unix-only sites do not compile on Windows; CI's Linux and macOS jobs are their compile and test check (a local musl cross-check could not build openssl-sys). --- src/agent/agy/bridged_tests.rs | 4 +--- src/agent/agy_tests.rs | 9 ++++--- src/agent/claude_code/tests.rs | 39 +++++++----------------------- src/agent/codex/tests.rs | 4 +--- src/hooks/engine.rs | 9 ++----- src/lib.rs | 2 ++ src/test_support.rs | 43 ++++++++++++++++++++++++++++++++++ src/toolbox/reading_tests.rs | 7 ++---- src/verify.rs | 12 ++++------ 9 files changed, 68 insertions(+), 61 deletions(-) create mode 100644 src/test_support.rs diff --git a/src/agent/agy/bridged_tests.rs b/src/agent/agy/bridged_tests.rs index 4038c87..cde0a6e 100644 --- a/src/agent/agy/bridged_tests.rs +++ b/src/agent/agy/bridged_tests.rs @@ -585,7 +585,6 @@ fn child_script(path: &Path, records_argv: Option<&Path>, marker: &str, value: & let exe = std::env::current_exe().expect("test binary"); #[cfg(unix)] { - use std::os::unix::fs::PermissionsExt; let before = records_argv .map(|argv| format!("printf '%s\\0' \"$@\" > '{}'", argv.display())) .unwrap_or_default(); @@ -594,8 +593,7 @@ fn child_script(path: &Path, records_argv: Option<&Path>, marker: &str, value: & value.display(), exe.display() ); - std::fs::write(path, body).expect("write the script"); - std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).expect("runnable"); + crate::test_support::write_script(path, &body); } #[cfg(windows)] { diff --git a/src/agent/agy_tests.rs b/src/agent/agy_tests.rs index ab2341c..ef111d2 100644 --- a/src/agent/agy_tests.rs +++ b/src/agent/agy_tests.rs @@ -468,7 +468,6 @@ fn stand_in_agy(dir: &std::path::Path, lines: &[&str], stderr: &str, code: i32) } #[cfg(not(windows))] { - use std::os::unix::fs::PermissionsExt; let mut body = String::from("#!/bin/sh\n"); for line in lines { body.push_str(&format!("echo '{line}'\n")); @@ -478,9 +477,7 @@ fn stand_in_agy(dir: &std::path::Path, lines: &[&str], stderr: &str, code: i32) } body.push_str(&format!("exit {code}\n")); let path = dir.join("agy.sh"); - std::fs::write(&path, body).expect("write the stand-in agy"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + crate::test_support::write_script(&path, &body); path } } @@ -524,6 +521,7 @@ fn a_whole_turn_reports_these_events_and_this_outcome() { 0, |b| b.set_models(&strings(&["gemini-3.8-flash-high"])), ); + // With the turn's end, so a turn that never started says why. assert_eq!( seen, vec![ @@ -540,7 +538,8 @@ fn a_whole_turn_reports_these_events_and_this_outcome() { cache_creation_tokens: 0, cache_read_tokens: 5, }, - ] + ], + "the turn ended: {result:?}" ); assert_eq!( result.expect("a result line"), diff --git a/src/agent/claude_code/tests.rs b/src/agent/claude_code/tests.rs index ec26096..b377256 100644 --- a/src/agent/claude_code/tests.rs +++ b/src/agent/claude_code/tests.rs @@ -225,17 +225,13 @@ fn a_loud_failing_agent(dir: &std::path::Path) -> PathBuf { } #[cfg(not(windows))] { - use std::os::unix::fs::PermissionsExt; let path = dir.join("loud-agent.sh"); - std::fs::write( + crate::test_support::write_script( &path, "#!/bin/sh\ni=0\nwhile [ $i -lt 3000 ]; do echo \"stderr line $i padding \ padding padding padding padding padding padding\" >&2; i=$((i+1)); done\n\ echo 'token=hunter2-not-a-real-secret' >&2\nexit 3\n", - ) - .expect("write the fake agent"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + ); path } } @@ -350,7 +346,6 @@ fn a_cli_without_the_thinking_flag(dir: &std::path::Path) -> PathBuf { } #[cfg(not(windows))] { - use std::os::unix::fs::PermissionsExt; let mut body = String::from( "#!/bin/bash\necho x >> \"$(dirname \"$0\")/starts.txt\"\n\ case \"$*\" in *--thinking-display*) \ @@ -361,9 +356,7 @@ fn a_cli_without_the_thinking_flag(dir: &std::path::Path) -> PathBuf { body.push_str(&format!("echo '{line}'\n")); } let path = dir.join("old-agent.sh"); - std::fs::write(&path, body).expect("write the stand-in agent"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + crate::test_support::write_script(&path, &body); path } } @@ -444,7 +437,6 @@ pub(crate) fn stand_in_cli(dir: &std::path::Path, waits: bool) -> PathBuf { } #[cfg(not(windows))] { - use std::os::unix::fs::PermissionsExt; let mut body = format!("#!/bin/bash\n{}echo '{}'\n", lists_the_tools(dir), LINES[0]); if waits { body.push_str("sleep 30\n"); @@ -453,9 +445,7 @@ pub(crate) fn stand_in_cli(dir: &std::path::Path, waits: bool) -> PathBuf { body.push_str(&format!("echo '{line}'\n")); } let path = dir.join(if waits { "slow-agent.sh" } else { "agent.sh" }); - std::fs::write(&path, body).expect("write the stand-in agent"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + crate::test_support::write_script(&path, &body); path } } @@ -554,15 +544,12 @@ fn a_cli_whose_tool_server_never_starts(dir: &std::path::Path) -> PathBuf { } #[cfg(not(windows))] { - use std::os::unix::fs::PermissionsExt; let mut body = String::from("#!/bin/sh\n"); for line in LINES { body.push_str(&format!("echo '{line}'\n")); } let path = dir.join("toolless-agent.sh"); - std::fs::write(&path, body).expect("write the stand-in agent"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + crate::test_support::write_script(&path, &body); path } } @@ -608,18 +595,14 @@ fn a_cli_reporting_the_tool_server_failed(dir: &std::path::Path) -> PathBuf { } #[cfg(not(windows))] { - use std::os::unix::fs::PermissionsExt; let path = dir.join("failed-server-agent.sh"); - std::fs::write( + crate::test_support::write_script( &path, - format!( + &format!( "#!/bin/sh\necho '{INIT}'\nsleep 5\n\ touch \"$(dirname \"$0\")/still-running.txt\"\necho '{RESULT}'\n" ), - ) - .expect("write the stand-in agent"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + ); path } } @@ -689,12 +672,8 @@ fn a_cli_that_ends_without_a_result(dir: &std::path::Path) -> PathBuf { } #[cfg(not(windows))] { - use std::os::unix::fs::PermissionsExt; let path = dir.join("cut-agent.sh"); - std::fs::write(&path, format!("#!/bin/sh\necho '{INIT}'\nexit 0\n")) - .expect("write the stand-in agent"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + crate::test_support::write_script(&path, &format!("#!/bin/sh\necho '{INIT}'\nexit 0\n")); path } } diff --git a/src/agent/codex/tests.rs b/src/agent/codex/tests.rs index 5f6683e..f4b4870 100644 --- a/src/agent/codex/tests.rs +++ b/src/agent/codex/tests.rs @@ -622,7 +622,6 @@ fn child_script(path: &Path, records_argv: Option<&Path>, marker: &str, value: & let exe = std::env::current_exe().expect("test binary"); #[cfg(unix)] { - use std::os::unix::fs::PermissionsExt; let before = records_argv .map(|argv| format!("printf '%s\\0' \"$@\" > '{}'", argv.display())) .unwrap_or_default(); @@ -631,8 +630,7 @@ fn child_script(path: &Path, records_argv: Option<&Path>, marker: &str, value: & value.display(), exe.display() ); - std::fs::write(path, body).expect("write the script"); - std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).expect("runnable"); + crate::test_support::write_script(path, &body); } #[cfg(windows)] { diff --git a/src/hooks/engine.rs b/src/hooks/engine.rs index 0ac0f1b..403c852 100644 --- a/src/hooks/engine.rs +++ b/src/hooks/engine.rs @@ -814,7 +814,6 @@ pub(crate) mod fake { } #[cfg(unix)] { - use std::os::unix::fs::PermissionsExt; let mut script = String::from("#!/bin/sh\nhere=\"$(dirname \"$0\")\"\n"); for (sub, reply) in replies { script.push_str(&format!( @@ -832,9 +831,7 @@ pub(crate) mod fake { } script.push_str("exit 0\n"); let path = dir.join("fake-engine.sh"); - std::fs::write(&path, script).expect("write the fake engine"); - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) - .expect("make it runnable"); + crate::test_support::write_script(&path, &script); path } } @@ -967,15 +964,13 @@ mod tests { let bin = { // The shared helper is `sh -c