mirror of
https://github.com/instructkr/claw-code.git
synced 2026-11-16 00:00:40 +08:00
fix(windows): fix native runtime compatibility and enable workspace t… (#3378)
* fix(windows): fix native runtime compatibility and enable workspace tests Fix shell discovery, hook quoting, native path handling and OAuth randomness on Windows, make the test fixtures portable, and run the full workspace test suite in the Windows CI job. * test(plugins): make lifecycle fixtures Windows-compatible * fix(windows): address workspace test failures --------- Co-authored-by: clawdbot <clawdbot@ultraworkers.dev>
This commit is contained in:
1 parent
ca48ec7500
commit
407a6c62f8
31 files changed
+873
-371
No files matched your search
@@ -1,5 +1,4 @@
|
||||
use std::ffi::OsStr;
|
||||
#[cfg(not(windows))]
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
@@ -282,8 +281,18 @@ fn format_hook_warning(command: &str, code: i32, stdout: Option<&str>, stderr: &
|
||||
fn shell_command(command: &str) -> CommandWithStdin {
|
||||
#[cfg(windows)]
|
||||
let command_builder = {
|
||||
use std::os::windows::process::CommandExt;
|
||||
|
||||
// This is a configured shell command, not a single argv value. cmd
|
||||
// does not understand the C-runtime backslash escaping used by arg().
|
||||
let command = if Path::new(command).is_file() {
|
||||
format!("\"{command}\"")
|
||||
} else {
|
||||
command.to_string()
|
||||
};
|
||||
let mut command_builder = Command::new("cmd");
|
||||
command_builder.arg("/C").arg(command);
|
||||
command_builder.args(["/D", "/S", "/C"]);
|
||||
command_builder.raw_arg(format!("\"{command}\""));
|
||||
CommandWithStdin::new(command_builder)
|
||||
};
|
||||
|
||||
@@ -381,6 +390,14 @@ mod tests {
|
||||
std::env::temp_dir().join(format!("plugins-hook-runner-{label}-{nanos}"))
|
||||
}
|
||||
|
||||
fn plugin_hook(message: &str, code: i32) -> String {
|
||||
if cfg!(windows) {
|
||||
format!("echo {message}& exit /b {code}")
|
||||
} else {
|
||||
format!("printf '%s' '{message}'; exit {code}")
|
||||
}
|
||||
}
|
||||
|
||||
fn make_executable(path: &Path) {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
@@ -403,34 +420,34 @@ mod tests {
|
||||
fs::create_dir_all(root.join(".claude-plugin")).expect("manifest dir");
|
||||
fs::create_dir_all(root.join("hooks")).expect("hooks dir");
|
||||
|
||||
let pre_path = root.join("hooks").join("pre.sh");
|
||||
fs::write(
|
||||
&pre_path,
|
||||
format!("#!/bin/sh\nprintf '%s\\n' '{pre_message}'\n"),
|
||||
)
|
||||
.expect("write pre hook");
|
||||
make_executable(&pre_path);
|
||||
|
||||
let post_path = root.join("hooks").join("post.sh");
|
||||
fs::write(
|
||||
&post_path,
|
||||
format!("#!/bin/sh\nprintf '%s\\n' '{post_message}'\n"),
|
||||
)
|
||||
.expect("write post hook");
|
||||
make_executable(&post_path);
|
||||
|
||||
let failure_path = root.join("hooks").join("failure.sh");
|
||||
fs::write(
|
||||
&failure_path,
|
||||
format!("#!/bin/sh\nprintf '%s\\n' '{failure_message}'\n"),
|
||||
)
|
||||
.expect("write failure hook");
|
||||
make_executable(&failure_path);
|
||||
let extension = if cfg!(windows) { "cmd" } else { "sh" };
|
||||
for (phase, message) in [
|
||||
("pre", pre_message),
|
||||
("post", post_message),
|
||||
("failure", failure_message),
|
||||
] {
|
||||
let path = root.join("hooks").join(format!("{phase}.{extension}"));
|
||||
let script = if cfg!(windows) {
|
||||
format!("@echo off\r\necho {message}\r\n")
|
||||
} else {
|
||||
format!("#!/bin/sh\nprintf '%s\\n' '{message}'\n")
|
||||
};
|
||||
fs::write(&path, script).expect("write hook");
|
||||
make_executable(&path);
|
||||
}
|
||||
fs::write(
|
||||
root.join(".claude-plugin").join("plugin.json"),
|
||||
format!(
|
||||
"{{\n \"name\": \"{name}\",\n \"version\": \"1.0.0\",\n \"description\": \"hook plugin\",\n \"hooks\": {{\n \"PreToolUse\": [\"./hooks/pre.sh\"],\n \"PostToolUse\": [\"./hooks/post.sh\"],\n \"PostToolUseFailure\": [\"./hooks/failure.sh\"]\n }}\n}}"
|
||||
),
|
||||
serde_json::json!({
|
||||
"name": name,
|
||||
"version": "1.0.0",
|
||||
"description": "hook plugin",
|
||||
"hooks": {
|
||||
"PreToolUse": [format!("./hooks/pre.{extension}")],
|
||||
"PostToolUse": [format!("./hooks/post.{extension}")],
|
||||
"PostToolUseFailure": [format!("./hooks/failure.{extension}")]
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("write plugin manifest");
|
||||
}
|
||||
@@ -439,7 +456,7 @@ mod tests {
|
||||
fn collects_and_runs_hooks_from_enabled_plugins() {
|
||||
// given
|
||||
let config_home = temp_dir("config");
|
||||
let first_source_root = temp_dir("source-a");
|
||||
let first_source_root = temp_dir("source a");
|
||||
let second_source_root = temp_dir("source-b");
|
||||
write_hook_plugin(
|
||||
&first_source_root,
|
||||
@@ -500,7 +517,7 @@ mod tests {
|
||||
fn pre_tool_use_denies_when_plugin_hook_exits_two() {
|
||||
// given
|
||||
let runner = HookRunner::new(crate::PluginHooks {
|
||||
pre_tool_use: vec!["printf 'blocked by plugin'; exit 2".to_string()],
|
||||
pre_tool_use: vec![plugin_hook("blocked by plugin", 2)],
|
||||
post_tool_use: Vec::new(),
|
||||
post_tool_use_failure: Vec::new(),
|
||||
});
|
||||
@@ -513,13 +530,28 @@ mod tests {
|
||||
assert_eq!(result.messages(), &["blocked by plugin".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_quoted_arguments_in_plugin_hooks() {
|
||||
let python = if cfg!(windows) { "python" } else { "python3" };
|
||||
let runner = HookRunner::new(crate::PluginHooks {
|
||||
pre_tool_use: vec![format!(
|
||||
"{python} -c \"import sys; sys.stdout.write('quoted plugin hook'); sys.exit(2)\""
|
||||
)],
|
||||
post_tool_use: Vec::new(),
|
||||
post_tool_use_failure: Vec::new(),
|
||||
});
|
||||
let result = runner.run_pre_tool_use("Read", r#"{"path":"README.md"}"#);
|
||||
assert!(result.is_denied(), "{result:?}");
|
||||
assert_eq!(result.messages(), &["quoted plugin hook".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn propagates_plugin_hook_failures() {
|
||||
// given
|
||||
let runner = HookRunner::new(crate::PluginHooks {
|
||||
pre_tool_use: vec![
|
||||
"printf 'broken plugin hook'; exit 1".to_string(),
|
||||
"printf 'later plugin hook'".to_string(),
|
||||
plugin_hook("broken plugin hook", 1),
|
||||
plugin_hook("later plugin hook", 0),
|
||||
],
|
||||
post_tool_use: Vec::new(),
|
||||
post_tool_use_failure: Vec::new(),
|
||||
|
||||
@@ -2502,20 +2502,42 @@ mod tests {
|
||||
|
||||
fn write_lifecycle_plugin(root: &Path, name: &str, version: &str) -> PathBuf {
|
||||
let log_path = root.join("lifecycle.log");
|
||||
let extension = if cfg!(windows) { "cmd" } else { "sh" };
|
||||
let (init_script, shutdown_script) = if cfg!(windows) {
|
||||
(
|
||||
"@echo off\r\necho init>>lifecycle.log\r\n",
|
||||
"@echo off\r\necho shutdown>>lifecycle.log\r\n",
|
||||
)
|
||||
} else {
|
||||
(
|
||||
"#!/bin/sh\nprintf 'init\\n' >> lifecycle.log\n",
|
||||
"#!/bin/sh\nprintf 'shutdown\\n' >> lifecycle.log\n",
|
||||
)
|
||||
};
|
||||
write_file(
|
||||
root.join("lifecycle").join("init.sh").as_path(),
|
||||
"#!/bin/sh\nprintf 'init\\n' >> lifecycle.log\n",
|
||||
root.join("lifecycle")
|
||||
.join(format!("init.{extension}"))
|
||||
.as_path(),
|
||||
init_script,
|
||||
);
|
||||
write_file(
|
||||
root.join("lifecycle").join("shutdown.sh").as_path(),
|
||||
"#!/bin/sh\nprintf 'shutdown\\n' >> lifecycle.log\n",
|
||||
root.join("lifecycle")
|
||||
.join(format!("shutdown.{extension}"))
|
||||
.as_path(),
|
||||
shutdown_script,
|
||||
);
|
||||
write_file(
|
||||
root.join(MANIFEST_RELATIVE_PATH).as_path(),
|
||||
format!(
|
||||
"{{\n \"name\": \"{name}\",\n \"version\": \"{version}\",\n \"description\": \"lifecycle plugin\",\n \"lifecycle\": {{\n \"Init\": [\"./lifecycle/init.sh\"],\n \"Shutdown\": [\"./lifecycle/shutdown.sh\"]\n }}\n}}"
|
||||
)
|
||||
.as_str(),
|
||||
&serde_json::json!({
|
||||
"name": name,
|
||||
"version": version,
|
||||
"description": "lifecycle plugin",
|
||||
"lifecycle": {
|
||||
"Init": [format!("./lifecycle/init.{extension}")],
|
||||
"Shutdown": [format!("./lifecycle/shutdown.{extension}")]
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
);
|
||||
log_path
|
||||
}
|
||||
@@ -2525,25 +2547,28 @@ mod tests {
|
||||
}
|
||||
|
||||
fn write_tool_plugin_with_name(root: &Path, name: &str, version: &str, tool_name: &str) {
|
||||
let script_path = root.join("tools").join("echo-json.sh");
|
||||
let script_path = root.join("tools").join("echo-json.py");
|
||||
write_file(
|
||||
&script_path,
|
||||
"#!/bin/sh\nINPUT=$(cat)\nprintf '{\"plugin\":\"%s\",\"tool\":\"%s\",\"input\":%s}\\n' \"$CLAWD_PLUGIN_ID\" \"$CLAWD_TOOL_NAME\" \"$INPUT\"\n",
|
||||
r#"import json, os, sys
|
||||
print(json.dumps({"plugin": os.environ["CLAWD_PLUGIN_ID"], "tool": os.environ["CLAWD_TOOL_NAME"], "input": json.load(sys.stdin)}))
|
||||
"#,
|
||||
);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let mut permissions = fs::metadata(&script_path).expect("metadata").permissions();
|
||||
permissions.set_mode(0o755);
|
||||
fs::set_permissions(&script_path, permissions).expect("chmod");
|
||||
}
|
||||
write_file(
|
||||
root.join(MANIFEST_RELATIVE_PATH).as_path(),
|
||||
format!(
|
||||
"{{\n \"name\": \"{name}\",\n \"version\": \"{version}\",\n \"description\": \"tool plugin\",\n \"tools\": [\n {{\n \"name\": \"{tool_name}\",\n \"description\": \"Echo JSON input\",\n \"inputSchema\": {{\"type\": \"object\", \"properties\": {{\"message\": {{\"type\": \"string\"}}}}, \"required\": [\"message\"], \"additionalProperties\": false}},\n \"command\": \"./tools/echo-json.sh\",\n \"requiredPermission\": \"workspace-write\"\n }}\n ]\n}}"
|
||||
)
|
||||
.as_str(),
|
||||
&serde_json::json!({
|
||||
"name": name,
|
||||
"version": version,
|
||||
"description": "tool plugin",
|
||||
"tools": [{
|
||||
"name": tool_name,
|
||||
"description": "Echo JSON input",
|
||||
"inputSchema": {"type": "object", "properties": {"message": {"type": "string"}}, "required": ["message"], "additionalProperties": false},
|
||||
"command": if cfg!(windows) { "python" } else { "python3" },
|
||||
"args": ["tools/echo-json.py"],
|
||||
"requiredPermission": "workspace-write"
|
||||
}]
|
||||
}).to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3623,7 +3648,7 @@ mod tests {
|
||||
registry.shutdown().expect("shutdown should succeed");
|
||||
|
||||
let log = fs::read_to_string(&log_path).expect("lifecycle log should exist");
|
||||
assert_eq!(log, "init\nshutdown\n");
|
||||
assert_eq!(log.replace("\r\n", "\n"), "init\nshutdown\n");
|
||||
|
||||
let _ = fs::remove_dir_all(config_home);
|
||||
let _ = fs::remove_dir_all(source_root);
|
||||
@@ -3820,7 +3845,7 @@ mod tests {
|
||||
if registry.initialize().is_ok() && registry.shutdown().is_ok() {
|
||||
// Verify lifecycle.log exists and has expected content
|
||||
if let Ok(log) = fs::read_to_string(&log_path) {
|
||||
if log == "init\nshutdown\n" {
|
||||
if log.replace("\r\n", "\n") == "init\nshutdown\n" {
|
||||
success_count.fetch_add(1, AtomicOrdering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user