Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

cua-agent-setup tests: write fake agent binaries atomically to avoid ETXTBSY (#6575)

cua-agents[bot]9d34ea8e231d
diff --git a/libs/cua/crates/cua-agent-setup/tests/agents.rs b/libs/cua/crates/cua-agent-setup/tests/agents.rs
index f4968e102f..7df0f788be 100644
--- a/libs/cua/crates/cua-agent-setup/tests/agents.rs
+++ b/libs/cua/crates/cua-agent-setup/tests/agents.rs
@@ -1,6 +1,8 @@
//! End-to-end behaviour of cua-agent-setup against fixture configs for every
//! agent format, always in a temporary HOME (never the real one).
+mod common;
+
use cua_agent_setup::{
AGENTS, AgentSetup, Change, Format, HostEnv, McpServer, Outcome, Parts, Target, registry,
};
@@ -144,19 +146,16 @@ fn one(out: Vec<Outcome>) -> Outcome {
/// (or, when `answers` is false, reads it and never answers).
#[cfg(unix)]
fn fake_driver(path: &Path, answers: bool) -> PathBuf {
- use std::os::unix::fs::PermissionsExt;
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let body = if answers {
"read line\nprintf '%s\\n' 'not json: a log line'\nprintf '%s\\n' '{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"protocolVersion\":\"2025-06-18\",\"capabilities\":{},\"serverInfo\":{\"name\":\"cua-driver\",\"version\":\"9.9.9\"}}}'\n"
} else {
"read line\nexit 3\n"
};
- std::fs::write(
+ common::write_executable(
path,
format!("#!/bin/sh\n[ \"$1\" = mcp ] || exit 2\n{body}"),
- )
- .unwrap();
- std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
+ );
path.to_path_buf()
}
@@ -575,11 +574,9 @@ fn detection_uses_path_markers_and_app_bundles() {
let bin = t.home.join("bin");
std::fs::create_dir_all(&bin).unwrap();
let codex = bin.join(if cfg!(windows) { "codex.exe" } else { "codex" });
- std::fs::write(&codex, "#!/bin/sh\n").unwrap();
#[cfg(unix)]
{
- use std::os::unix::fs::PermissionsExt;
- std::fs::set_permissions(&codex, std::fs::Permissions::from_mode(0o755)).unwrap();
+ common::write_executable(&codex, "#!/bin/sh\n");
// A non-executable file is not a binary.
std::fs::write(bin.join("claude"), "").unwrap();
}
@@ -833,7 +830,6 @@ fn a_skill_the_user_installed_another_way_is_not_outdated() {
#[cfg(unix)]
#[test]
fn claude_code_is_configured_through_its_own_cli_when_present() {
- use std::os::unix::fs::PermissionsExt;
let mut t = T::new();
let bin = t.home.join("bin");
std::fs::create_dir_all(&bin).unwrap();
@@ -849,8 +845,7 @@ fn claude_code_is_configured_through_its_own_cli_when_present() {
config.display()
);
let claude = bin.join("claude");
- std::fs::write(&claude, script).unwrap();
- std::fs::set_permissions(&claude, std::fs::Permissions::from_mode(0o755)).unwrap();
+ common::write_executable(&claude, script);
t.env.path = vec![bin];
t.env.run_agent_clis = true;
let s = t.setup();
@@ -1038,12 +1033,10 @@ fn the_driver_command_resolves_from_path_then_local_bin() {
} else {
"cua-driver"
});
- std::fs::write(&exe, "#!/bin/sh\n").unwrap();
#[cfg(unix)]
- {
- use std::os::unix::fs::PermissionsExt;
- std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
+ common::write_executable(&exe, "#!/bin/sh\n");
+ #[cfg(not(unix))]
+ std::fs::write(&exe, "#!/bin/sh\n").unwrap();
assert_eq!(
McpServer::driver_for(&t.env).command,
exe.display().to_string()
@@ -1179,12 +1172,10 @@ fn cua_driver_is_found_inside_its_app_on_macos() {
.home
.join("Applications/CuaDriver.app/Contents/MacOS/cua-driver");
std::fs::create_dir_all(exe.parent().unwrap()).unwrap();
- std::fs::write(&exe, "#!/bin/sh\n").unwrap();
#[cfg(unix)]
- {
- use std::os::unix::fs::PermissionsExt;
- std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
+ common::write_executable(&exe, "#!/bin/sh\n");
+ #[cfg(not(unix))]
+ std::fs::write(&exe, "#!/bin/sh\n").unwrap();
assert_eq!(cua_agent_setup::driver::locate(&env), Some(exe.clone()));
assert_eq!(
McpServer::driver_for(&env).command,
@@ -1218,12 +1209,10 @@ fn hermes_is_detected_from_its_bin_home_or_app() {
} else {
"hermes"
});
- std::fs::write(&exe, "#!/bin/sh\n").unwrap();
#[cfg(unix)]
- {
- use std::os::unix::fs::PermissionsExt;
- std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
+ common::write_executable(&exe, "#!/bin/sh\n");
+ #[cfg(not(unix))]
+ std::fs::write(&exe, "#!/bin/sh\n").unwrap();
t.env.path = vec![bin];
let st = get(&t.setup());
assert_eq!(st.evidence, [format!("bin:{}", exe.display())]);
@@ -1463,7 +1452,6 @@ fn sign_in_is_refused_without_a_sign_in_an_install_or_agent_commands() {
#[cfg(unix)]
#[test]
fn sign_in_runs_the_agents_own_login_and_detect_sees_it() {
- use std::os::unix::fs::PermissionsExt;
let mut t = T::new();
let bin = t.home.join("bin");
std::fs::create_dir_all(&bin).unwrap();
@@ -1478,8 +1466,7 @@ fn sign_in_runs_the_agents_own_login_and_detect_sees_it() {
// Its PATH is only `bin` below: no mkdir there.
std::fs::create_dir_all(auth.parent().unwrap()).unwrap();
let codex = bin.join("codex");
- std::fs::write(&codex, script).unwrap();
- std::fs::set_permissions(&codex, std::fs::Permissions::from_mode(0o755)).unwrap();
+ common::write_executable(&codex, script);
t.env.path = vec![bin];
t.env.run_agent_clis = true;
let s = t.setup();
diff --git a/libs/cua/crates/cua-agent-setup/tests/common/mod.rs b/libs/cua/crates/cua-agent-setup/tests/common/mod.rs
index f9fe19df8a..649297bb56 100644
--- a/libs/cua/crates/cua-agent-setup/tests/common/mod.rs
+++ b/libs/cua/crates/cua-agent-setup/tests/common/mod.rs
@@ -8,16 +8,36 @@
pub fn answering_driver() -> &'static str {
static PATH: std::sync::OnceLock<String> = std::sync::OnceLock::new();
PATH.get_or_init(|| {
- use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
let dir: PathBuf = tempfile::tempdir().unwrap().keep();
let p = dir.join("cua-driver");
- std::fs::write(
+ write_executable(
&p,
"#!/bin/sh\nread line\nprintf '%s\\n' '{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"serverInfo\":{\"name\":\"cua-driver\",\"version\":\"9.9.9\"}}}'\n",
- )
- .unwrap();
- std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
+ );
p.display().to_string()
})
}
+
+/// Write an executable file without ever leaving a write handle open on the
+/// path that gets exec'd. Writing in place and then running the file races
+/// with any other test thread that forks: the child inherits the open write
+/// fd until it execs, and executing the file meanwhile fails with ETXTBSY
+/// ("Text file busy") on Linux. So: write a uniquely named sibling, close it,
+/// set the mode, then rename it into place atomically.
+#[cfg(unix)]
+pub fn write_executable(path: &std::path::Path, contents: impl AsRef<[u8]>) {
+ use std::io::Write;
+ use std::os::unix::fs::PermissionsExt;
+ let dir = path.parent().unwrap();
+ std::fs::create_dir_all(dir).unwrap();
+ let mut tmp = tempfile::Builder::new()
+ .prefix(".write-exec-")
+ .tempfile_in(dir)
+ .unwrap();
+ tmp.write_all(contents.as_ref()).unwrap();
+ std::fs::set_permissions(tmp.path(), std::fs::Permissions::from_mode(0o755)).unwrap();
+ // Close the write handle before the file gets its final name.
+ let tmp_path = tmp.into_temp_path();
+ tmp_path.persist(path).unwrap();
+}
diff --git a/libs/cua/crates/cua-agent-setup/tests/connection_matrix.rs b/libs/cua/crates/cua-agent-setup/tests/connection_matrix.rs
index a9b8da0516..36acaaeffe 100644
--- a/libs/cua/crates/cua-agent-setup/tests/connection_matrix.rs
+++ b/libs/cua/crates/cua-agent-setup/tests/connection_matrix.rs
@@ -8,13 +8,14 @@
//! "not connected" while it works.
#![cfg(unix)]
+mod common;
+
use cua_agent_setup::{
AgentSetup, ConnectionRow, ConnectionState, HostEnv, ImportState, ImportedSignIn, McpServer,
McpState, Parts, ProblemAction, SignInCopy, SignInVault, SignedInState, StatusOptions,
connection::ConnectParts,
};
use std::{
- os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
sync::Mutex,
time::Duration,
@@ -166,8 +167,7 @@ impl World {
fn script(&self, name: &str, body: &str) -> PathBuf {
let p = self.bin.join(name);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
- std::fs::write(&p, body).unwrap();
- std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
+ common::write_executable(&p, body);
p
}
@@ -917,8 +917,7 @@ fn a_second_agent_of_the_same_harness() {
let second = w._dir.path().join("bin2");
std::fs::create_dir_all(&second).unwrap();
let p = second.join("codex");
- std::fs::write(&p, agent_script("codex-cli 0.55.0", "")).unwrap();
- std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
+ common::write_executable(&p, agent_script("codex-cli 0.55.0", ""));
let mut env = w.env.clone();
env.path.push(second.clone());
let w2 = World { env, ..w };
diff --git a/libs/cua/crates/cua-agent-setup/tests/local_channel.rs b/libs/cua/crates/cua-agent-setup/tests/local_channel.rs
index 868af591b2..b29e8d87da 100644
--- a/libs/cua/crates/cua-agent-setup/tests/local_channel.rs
+++ b/libs/cua/crates/cua-agent-setup/tests/local_channel.rs
@@ -6,6 +6,8 @@
//! `~/.cua-local`, and leave everything Stable wrote byte for byte as it was.
//! Always in a temporary HOME, never the real one.
+mod common;
+
use cua_agent_setup::{
AgentSetup, Change, CuaChannel, HostEnv, McpServer, Outcome, Parts, Target, hash_dir,
};
@@ -114,12 +116,10 @@ fn json(text: &str) -> Value {
/// executable, in `~/.local/bin`.
fn install_cua_local(home: &Path) -> PathBuf {
let p = home.join(".local/bin/cua-local");
- write(home, ".local/bin/cua-local", "#!/bin/sh\n");
#[cfg(unix)]
- {
- use std::os::unix::fs::PermissionsExt;
- std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
- }
+ common::write_executable(&p, "#!/bin/sh\n");
+ #[cfg(not(unix))]
+ write(home, ".local/bin/cua-local", "#!/bin/sh\n");
p
}