mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
test(cli-rs): write fake TypeScript scripts from sh to avoid ETXTBSY
A test wrote a script and executed it right away. When another test thread forked while the write descriptor was open, the child held it until its own exec, and the exec of the script failed with ETXTBSY. This made interop_with_a_typescript_service fail once in a full run. ts_command::write_script now writes the script and sets its mode from a sh child, so the test process never holds a write descriptor to it. Every test that writes and then executes a script uses it.
This commit is contained in:
@@ -113,8 +113,7 @@ process.exit(0)
|
||||
std::fs::write(&script, STANDALONE).unwrap();
|
||||
// `OPENCODE_TS_BIN` names one executable.
|
||||
let program = dir.path().join("opencode-ts");
|
||||
std::fs::write(&program, format!("#!/bin/sh\nexec bun {}\n", script.display())).unwrap();
|
||||
std::fs::set_permissions(&program, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
|
||||
opencode_service::ts_command::write_script(&program, &format!("#!/bin/sh\nexec bun {}\n", script.display()));
|
||||
let root = dir.path().to_owned();
|
||||
let paths = Paths::from_vars(|name| match name {
|
||||
"HOME" => Some(root.clone().into()),
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
//! TypeScript binary records what it got, so nothing real is installed, removed, or served.
|
||||
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Output, Stdio};
|
||||
|
||||
use opencode_service::ts_command::write_script;
|
||||
|
||||
const FAKE: &str = r#"#!/bin/sh
|
||||
printf '%s\n' "$@" > "$DELEGATE_DIR/args"
|
||||
echo "$$" > "$DELEGATE_DIR/pid"
|
||||
@@ -23,8 +24,7 @@ impl Fake {
|
||||
fn new() -> Self {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let bin = dir.path().join("opencode");
|
||||
std::fs::write(&bin, FAKE).unwrap();
|
||||
std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
write_script(&bin, FAKE);
|
||||
Self { dir }
|
||||
}
|
||||
|
||||
@@ -123,12 +123,10 @@ fn an_interrupt_waits_for_the_remote_access_command() {
|
||||
use std::os::unix::process::CommandExt;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let bin = dir.path().join("opencode");
|
||||
std::fs::write(
|
||||
write_script(
|
||||
&bin,
|
||||
"#!/bin/sh\ntrap 'sleep 0.5; echo done > \"$DELEGATE_DIR/teardown\"; exit 130' INT\necho > \"$DELEGATE_DIR/started\"\nwhile :; do sleep 0.05; done\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
);
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_opencode-rs"))
|
||||
.args(["service", "set", "remote", "true"])
|
||||
.env("OPENCODE_TS_BIN", &bin)
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Output};
|
||||
|
||||
use opencode_service::ts_command::write_script;
|
||||
|
||||
const SERVICE: &str = r#"
|
||||
import { appendFile, rename, writeFile } from "node:fs/promises"
|
||||
const [registration, log] = process.argv.slice(2)
|
||||
@@ -73,11 +75,7 @@ esac
|
||||
data = sandbox.path("data").display(),
|
||||
config = sandbox.path("config/opencode/service-local.json").display(),
|
||||
);
|
||||
let program = sandbox.path("opencode-ts");
|
||||
std::fs::write(&program, script).unwrap();
|
||||
let mut permissions = std::fs::metadata(&program).unwrap().permissions();
|
||||
std::os::unix::fs::PermissionsExt::set_mode(&mut permissions, 0o755);
|
||||
std::fs::set_permissions(&program, permissions).unwrap();
|
||||
write_script(&sandbox.path("opencode-ts"), &script);
|
||||
Some(sandbox)
|
||||
}
|
||||
|
||||
@@ -218,7 +216,7 @@ fn set_remote_true_runs_the_typescript_command() {
|
||||
assert_eq!(sandbox.log(), "service set remote true\n");
|
||||
assert_eq!(text(&sandbox.run(&["service", "get", "remote"], &[]).stdout), "true\n");
|
||||
// A failure of the TS command is the exit code.
|
||||
std::fs::write(sandbox.path("opencode-ts"), "#!/bin/sh\necho tunnel failed >&2\nexit 4\n").unwrap();
|
||||
write_script(&sandbox.path("opencode-ts"), "#!/bin/sh\necho tunnel failed >&2\nexit 4\n");
|
||||
let failed = sandbox.run(&["service", "set", "remote", "true"], &[]);
|
||||
assert_eq!(failed.status.code(), Some(4));
|
||||
assert_eq!(text(&failed.stderr), "tunnel failed\n");
|
||||
@@ -302,17 +300,13 @@ fn interop_with_a_typescript_service() {
|
||||
.map(|(name, dir)| format!("{name}='{}'", sandbox.path(dir).display()))
|
||||
.join(" ");
|
||||
let wrapper = sandbox.path("opencode-ts-source");
|
||||
std::fs::write(
|
||||
write_script(
|
||||
&wrapper,
|
||||
format!(
|
||||
&format!(
|
||||
"#!/bin/sh\ncd '{}/packages/cli' || exit 1\nexec env -i PATH=\"$PATH\" {vars} OPENCODE_DISABLE_MODELS_FETCH=1 OPENCODE_DISABLE_AUTOUPDATE=1 bun run --silent src/index.ts \"$@\"\n",
|
||||
root.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
let mut permissions = std::fs::metadata(&wrapper).unwrap().permissions();
|
||||
std::os::unix::fs::PermissionsExt::set_mode(&mut permissions, 0o755);
|
||||
std::fs::set_permissions(&wrapper, permissions).unwrap();
|
||||
);
|
||||
let free_port = || std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
|
||||
let port = free_port();
|
||||
std::fs::write(sandbox.path("config/opencode/service-local.json"), format!("{{\"port\":{port}}}\n")).unwrap();
|
||||
|
||||
@@ -127,10 +127,8 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
fn script(dir: &Path, body: &str) -> TsCommand {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let path = dir.join("opencode");
|
||||
std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap();
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
crate::ts_command::write_script(&path, &format!("#!/bin/sh\n{body}\n"));
|
||||
TsCommand::new(path)
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,21 @@ impl TsCommand {
|
||||
}
|
||||
}
|
||||
|
||||
/// For tests: writes an executable script that stands in for the TypeScript binary. `sh` writes it, because a
|
||||
/// write descriptor open in the test process can leak into a child that another test thread forks, and an exec
|
||||
/// of the script then fails with ETXTBSY.
|
||||
#[doc(hidden)]
|
||||
#[cfg(unix)]
|
||||
pub fn write_script(path: &std::path::Path, script: &str) {
|
||||
let status = std::process::Command::new("sh")
|
||||
.args(["-c", "printf %s \"$1\" > \"$0\" && chmod 755 \"$0\""])
|
||||
.arg(path)
|
||||
.arg(script)
|
||||
.status()
|
||||
.unwrap();
|
||||
assert!(status.success(), "write {}", path.display());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -56,10 +56,7 @@ impl Isolation {
|
||||
ts_root.display()
|
||||
);
|
||||
let wrapper = self.path("opencode-ts");
|
||||
std::fs::write(&wrapper, script).unwrap();
|
||||
let mut permissions = std::fs::metadata(&wrapper).unwrap().permissions();
|
||||
std::os::unix::fs::PermissionsExt::set_mode(&mut permissions, 0o755);
|
||||
std::fs::set_permissions(&wrapper, permissions).unwrap();
|
||||
opencode_service::ts_command::write_script(&wrapper, &script);
|
||||
wrapper
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use opencode_service::connect::{self, Connection, Env};
|
||||
use opencode_service::contender::Delays;
|
||||
use opencode_service::ensure::{self, Ensured, Options, Timing};
|
||||
use opencode_service::stop::{self, Poll, Pty};
|
||||
use opencode_service::ts_command::TsCommand;
|
||||
use opencode_service::ts_command::{TsCommand, write_script};
|
||||
use opencode_service::{follow, handoff, standalone};
|
||||
use rustix::process::{Pid, Signal, kill_process, test_kill_process};
|
||||
use std::ffi::OsString;
|
||||
@@ -637,17 +637,15 @@ fn channel_build(fixture: &Fixture, mode: &str) -> (Options, PathBuf, PathBuf) {
|
||||
let other = root.join("service-oc-y.json");
|
||||
let spawns = root.join("spawns");
|
||||
let program = root.join("opencode-ts");
|
||||
std::fs::write(
|
||||
write_script(
|
||||
&program,
|
||||
format!(
|
||||
&format!(
|
||||
"#!/bin/sh\necho $$ >> {}\nexec bun {} {} {mode}\n",
|
||||
spawns.display(),
|
||||
fixture_script().display(),
|
||||
other.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&program, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
|
||||
);
|
||||
let ts = TsCommand::from_vars(|name| (name == TsCommand::VAR).then(|| program.clone().into()));
|
||||
let mut options = Options::new(fixture.registration.clone(), root.to_owned(), &ts);
|
||||
options.timing = fast();
|
||||
@@ -764,16 +762,14 @@ impl Channel {
|
||||
std::fs::create_dir_all(&paths.state).unwrap();
|
||||
fixture.registration = paths.state.join("service-oc-x.json");
|
||||
let program = root.join("opencode-ts");
|
||||
std::fs::write(
|
||||
write_script(
|
||||
&program,
|
||||
format!(
|
||||
&format!(
|
||||
"#!/bin/sh\nexec bun {} {} record-start\n",
|
||||
fixture_script().display(),
|
||||
root.join("spawned").display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&program, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
|
||||
);
|
||||
let env = Env { ts_bin: Some(program.to_string_lossy().into_owned()), ..Env::default() };
|
||||
Self { fixture, paths, env }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user