Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/agent/agy/bridged_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)]
{
Expand Down
9 changes: 4 additions & 5 deletions src/agent/agy_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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![
Expand All @@ -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"),
Expand Down
39 changes: 9 additions & 30 deletions src/agent/claude_code/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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*) \
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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");
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/agent/codex/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)]
{
Expand Down
9 changes: 2 additions & 7 deletions src/hooks/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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
}
}
Expand Down Expand Up @@ -967,15 +964,13 @@ mod tests {
let bin = {
// The shared helper is `sh -c <script>`; as an engine it has to be
// one runnable file.
use std::os::unix::fs::PermissionsExt;
let script = command
.get_args()
.nth(1)
.map(|a| a.to_string_lossy().into_owned())
.expect("the script");
let path = dir.path().join("slow-engine.sh");
std::fs::write(&path, format!("#!/bin/sh\n{script}\n")).expect("write");
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).expect("chmod");
crate::test_support::write_script(&path, &format!("#!/bin/sh\n{script}\n"));
path
};
let engine = fake::engine(bin).capped(Duration::from_secs(3));
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub mod task_run;
pub mod team;
pub mod team_mcp;
pub mod telemetry;
#[cfg(all(test, not(windows)))]
pub(crate) mod test_support;
pub mod text;
pub mod tool_bridge;
pub mod toolbox;
Expand Down
43 changes: 43 additions & 0 deletions src/test_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! What the crate's tests share off Windows: writing a script a test then
//! runs.
//!
//! Used by every test that writes a stand-in program on Unix: the agents'
//! (`agent/agy_tests.rs`, `agent/agy/bridged_tests.rs`,
//! `agent/claude_code/tests.rs`, `agent/codex/tests.rs`), the engine's
//! (`hooks/engine.rs`) and the document reader's (`toolbox/reading_tests.rs`).
//! Test code only; `self_update.rs` makes its downloaded binary runnable in
//! its own way. On Windows the same tests write `.cmd` files and do not use
//! this.

use std::io::Write as _;
use std::path::Path;
use std::process::{Command, Stdio};

/// Write `body` to `path` as a runnable script, for a test to start.
///
/// The file is written by a short-lived `sh`, not by this process. A test
/// binary runs its tests on many threads at once, and a program another
/// thread starts while this process holds a file open for writing is handed
/// that open file until it has started; starting the script in that moment
/// fails with "Text file busy" (ETXTBSY) — a test that fails one run in
/// many, for no reason in the code under test. A file this process never
/// opens cannot be handed on.
///
/// Panics, as a test's setup does, when the script cannot be written.
pub(crate) fn write_script(path: &Path, body: &str) {
let mut sh = Command::new("sh")
.arg("-c")
.arg(r#"cat > "$1" && chmod 755 "$1""#)
.arg("sh")
.arg(path)
.stdin(Stdio::piped())
.spawn()
.expect("start sh to write the script");
sh.stdin
.take()
.expect("sh's stdin")
.write_all(body.as_bytes())
.expect("hand the script to sh");
let status = sh.wait().expect("sh ends");
assert!(status.success(), "sh could not write {}", path.display());
}
7 changes: 2 additions & 5 deletions src/toolbox/reading_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,11 @@ fn fake_soffice(dir: &Path) -> PathBuf {
}
#[cfg(not(windows))]
{
use std::os::unix::fs::PermissionsExt;
let path = dir.join("soffice");
std::fs::write(
crate::test_support::write_script(
&path,
"#!/bin/sh\nall=\"$*\"\nwhile [ $# -gt 0 ]; do\n if [ \"$1\" = --outdir ]; then out=\"$2\"; shift; fi\n last=\"$1\"; shift\ndone\nstem=$(basename \"$last\"); stem=${stem%.*}\necho \"$all\" > \"$out/$stem.pdf\"\n",
)
.expect("fake");
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).expect("chmod");
);
path
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2039,14 +2039,10 @@ mod tests {
#[cfg(not(windows))]
{
let script_path = dir.path().join("check.sh");
std::fs::write(&script_path, "#!/bin/sh\necho verified_ok\nexit 0\n").expect("write");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = std::fs::metadata(&script_path).unwrap().permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&script_path, perms).unwrap();
}
crate::test_support::write_script(
&script_path,
"#!/bin/sh\necho verified_ok\nexit 0\n",
);
let argv = vec![script_path.to_string_lossy().to_string()];
let outcome = run(&argv, dir.path()).expect("script runs");
assert_eq!(outcome.exit(), Some(0));
Expand Down
Loading