in codex-rs/external-agent-migration/src/lib.rs [1982:2129]
fn hook_command_paths_rewrite_to_target_hook_dir() {
let project_dir_env_var = external_agent_project_dir_env_var();
let plugin_root_env_var = format!(
"{}_PLUGIN_ROOT",
SOURCE_EXTERNAL_AGENT_NAME.to_ascii_uppercase()
);
let source_hooks_path = format!(
"{}/{EXTERNAL_AGENT_HOOKS_SUBDIR}",
external_agent_config_dir()
);
assert_eq!(
rewrite_hook_command(
&source_hook_command_with_project_dir("check.py"),
Some(Path::new("/repo/.codex")),
),
migrated_hook_command("check.py")
);
assert_eq!(
rewrite_hook_command(
&format!("\"${project_dir_env_var}\"/{source_hooks_path}/check-style.sh"),
Some(Path::new("/repo/.codex")),
),
shell_single_quote(
Path::new("/repo/.codex")
.join(EXTERNAL_AGENT_MIGRATED_HOOKS_SUBDIR)
.join("check-style.sh")
.to_string_lossy()
.as_ref()
)
);
assert_eq!(
rewrite_hook_command(
&source_hook_command("check.py"),
Some(Path::new("/repo/.codex")),
),
migrated_hook_command("check.py")
);
assert_eq!(
rewrite_hook_command(
&format!("python3 ./{source_hooks_path}/check.py"),
Some(Path::new("/repo/.codex")),
),
migrated_hook_command("check.py")
);
assert_eq!(
rewrite_hook_command(
&format!("python3 '${{{project_dir_env_var}}}/{source_hooks_path}/check.py'"),
Some(Path::new("/repo/.codex")),
),
migrated_quoted_hook_command("check.py")
);
assert_eq!(
rewrite_hook_command(
&format!("python3 \"${{{project_dir_env_var}}}/{source_hooks_path}/check.py\""),
Some(Path::new("/repo/.codex")),
),
migrated_quoted_hook_command("check.py")
);
assert_eq!(
rewrite_hook_command(
&format!("bash -lc \"python3 {source_hooks_path}/check.py\""),
Some(Path::new("/repo/.codex")),
),
format!("bash -lc \"python3 {source_hooks_path}/check.py\"")
);
assert_eq!(
rewrite_hook_command(
&format!(
"HOOK=${{{project_dir_env_var}}}/{source_hooks_path}/check.py python3 \"$HOOK\""
),
Some(Path::new("/repo/.codex")),
),
format!(
"HOOK=${{{project_dir_env_var}}}/{source_hooks_path}/check.py python3 \"$HOOK\""
)
);
assert_eq!(
rewrite_hook_command(
&format!("python3 {source_hooks_path}/${{SCRIPT}}.py"),
Some(Path::new("/repo/.codex")),
),
format!("python3 {source_hooks_path}/${{SCRIPT}}.py")
);
assert_eq!(
rewrite_hook_command(
&format!("python3 {source_hooks_path}/{{lint,fmt}}.sh"),
Some(Path::new("/repo/.codex")),
),
format!("python3 {source_hooks_path}/{{lint,fmt}}.sh")
);
assert_eq!(
rewrite_hook_command(
&format!("python3 {source_hooks_path}/my\\ script.py"),
Some(Path::new("/repo/.codex")),
),
format!("python3 {source_hooks_path}/my\\ script.py")
);
assert_eq!(
rewrite_hook_command(
&format!("python3 .{SOURCE_EXTERNAL_AGENT_NAME}\\hooks\\check.py"),
Some(Path::new("/repo/.codex")),
),
format!("python3 .{}\\hooks\\check.py", SOURCE_EXTERNAL_AGENT_NAME)
);
assert_eq!(
rewrite_hook_command(
&format!(
"python3 \"%{}%\\{}\\hooks\\check.py\"",
project_dir_env_var,
external_agent_config_dir()
),
Some(Path::new("/repo/.codex")),
),
format!(
"python3 \"%{}%\\{}\\hooks\\check.py\"",
project_dir_env_var,
external_agent_config_dir()
)
);
assert_eq!(
rewrite_hook_command(
&format!("python3 '${{{project_dir_env_var}}}/{source_hooks_path}/my script.py'"),
Some(Path::new("/repo/.codex")),
),
migrated_quoted_hook_command("my script.py")
);
assert_eq!(
rewrite_hook_command(
&format!("/repo/{source_hooks_path}/check.py 2>/dev/null || true"),
Some(Path::new("/repo/.codex")),
),
format!(
"{} 2>/dev/null || true",
shell_single_quote(
Path::new("/repo/.codex")
.join(EXTERNAL_AGENT_MIGRATED_HOOKS_SUBDIR)
.join("check.py")
.to_string_lossy()
.as_ref()
)
)
);
let plugin_script_command = format!("${{{plugin_root_env_var}}}/scripts/format.sh");
assert_eq!(
rewrite_hook_command(&plugin_script_command, Some(Path::new("/repo/.codex")),),
plugin_script_command
);
}