diff --git a/codex-rs/exec/tests/suite/mod.rs b/codex-rs/exec/tests/suite/mod.rs index 6f86856327..5a86e94206 100644 --- a/codex-rs/exec/tests/suite/mod.rs +++ b/codex-rs/exec/tests/suite/mod.rs @@ -6,6 +6,7 @@ mod auth_env; mod ephemeral; mod mcp_required_exit; mod originator; +mod output_last_message; mod output_schema; mod prompt_stdin; mod resume; diff --git a/codex-rs/exec/tests/suite/output_last_message.rs b/codex-rs/exec/tests/suite/output_last_message.rs new file mode 100644 index 0000000000..29fddd9935 --- /dev/null +++ b/codex-rs/exec/tests/suite/output_last_message.rs @@ -0,0 +1,35 @@ +#![cfg(unix)] + +use core_test_support::responses; +use core_test_support::test_codex_exec::test_codex_exec; +use predicates::str::contains; +use pretty_assertions::assert_eq; + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn output_last_message_does_not_follow_symlink() -> anyhow::Result<()> { + let test = test_codex_exec(); + let target_path = test.cwd_path().join("target.md"); + let output_path = test.cwd_path().join("output.md"); + std::fs::write(&target_path, "original")?; + std::os::unix::fs::symlink(&target_path, &output_path)?; + + let server = responses::start_mock_server().await; + let body = responses::sse(vec![ + responses::ev_response_created("resp1"), + responses::ev_assistant_message("m1", "replacement"), + responses::ev_completed("resp1"), + ]); + let _response_mock = responses::mount_sse_once(&server, body).await; + + test.cmd_with_server(&server) + .arg("--skip-git-repo-check") + .arg("--output-last-message") + .arg(&output_path) + .arg("write a response") + .assert() + .success() + .stderr(contains("Failed to write last message file")); + + assert_eq!(std::fs::read_to_string(&target_path)?, "original"); + Ok(()) +} diff --git a/codex-rs/utils/path-utils/src/lib.rs b/codex-rs/utils/path-utils/src/lib.rs index 5b346084bb..0d6af71936 100644 --- a/codex-rs/utils/path-utils/src/lib.rs +++ b/codex-rs/utils/path-utils/src/lib.rs @@ -144,7 +144,10 @@ pub fn write_atomically(write_path: &Path, contents: &str) -> io::Result<()> { Ok(()) } -/// Opens a file for writing without following symlinks in any path component. +/// Creates or truncates a file for writing without following symlinks in any path component. +/// +/// Relative paths are resolved from the process's current directory. The returned file uses mode +/// `0o666`, subject to the process umask. #[cfg(unix)] pub fn open_file_for_write_no_follow(path: &Path) -> io::Result { open_file_no_follow(