From a39ea052deff856ff9d27cdebecbbea1837d5a69 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:40:13 +0100 Subject: 0.4.4: file-tool correctness - read: regular files only, checked offset conversion on 32-bit - write/edit: atomic rename, re-read before apply, preserve mode - edit: CRLF files match and keep their line endings - grep: missing path is an error, skipped files are reported - glob: follows file symlinks --- tests/tools.rs | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) (limited to 'tests/tools.rs') diff --git a/tests/tools.rs b/tests/tools.rs index c30128e..77c1698 100644 --- a/tests/tools.rs +++ b/tests/tools.rs @@ -51,7 +51,7 @@ fn edit_applies_exactly_one_replacement() { ) .expect("unique edit should plan"); - let preview = change.preview(); + let preview = change.preview().unwrap(); assert!(preview.contains("- return 0;"), "{preview}"); assert!(preview.contains("+ return 1;"), "{preview}"); @@ -311,3 +311,111 @@ fn bash_does_not_inherit_the_api_key() { std::env::remove_var("OPENCODE_API_KEY"); assert!(text.contains('0'), "the key was visible to the child: {text}"); } + +#[test] +fn edit_matches_crlf_files_without_visible_carriage_returns() { + let dir = temp_dir("edit-crlf"); + let path = dir.join("win.c"); + fs::write(&path, "int main(void) {\r\n return 0;\r\n}\r\n").unwrap(); + + // The model copies what `read` showed it: plain newlines. + let change = tools::plan( + "edit", + &json!({ + "path": path, + "old": "int main(void) {\n return 0;\n", + "new": "int main(void) {\n return 1;\n" + }), + ) + .expect("CRLF file should still match"); + + change.apply().expect("apply should succeed"); + assert_eq!( + fs::read_to_string(&path).unwrap(), + "int main(void) {\r\n return 1;\r\n}\r\n" + ); +} + +#[test] +fn edit_refuses_when_the_file_changed_while_waiting_for_approval() { + let dir = temp_dir("edit-concurrent"); + let path = dir.join("f.txt"); + fs::write(&path, "alpha\n").unwrap(); + + let change = tools::plan("edit", &json!({ "path": path, "old": "alpha", "new": "beta" })) + .unwrap(); + // Something else (cargo fmt, the user's editor) ran before approval. + fs::write(&path, "alpha\nalpha\n").unwrap(); + + let error = change.apply().err().expect("apply must refuse"); + assert!(error.contains("appears 2 times"), "{error}"); + assert_eq!(fs::read_to_string(&path).unwrap(), "alpha\nalpha\n"); +} + +#[test] +fn write_preserves_the_existing_file_mode() { + use std::os::unix::fs::PermissionsExt; + let dir = temp_dir("write-mode"); + let path = dir.join("script.sh"); + fs::write(&path, "old").unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap(); + + tools::plan("write", &json!({ "path": path, "content": "new" })) + .unwrap() + .apply() + .unwrap(); + + let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777; + assert_eq!(mode, 0o755); + assert_eq!(fs::read_to_string(&path).unwrap(), "new"); +} + +#[test] +fn read_refuses_special_files() { + let dir = temp_dir("read-fifo"); + let path = dir.join("fifo"); + let name = std::ffi::CString::new(path.to_str().unwrap()).unwrap(); + let made = unsafe { libc::mkfifo(name.as_ptr(), 0o600) }; + assert_eq!(made, 0, "could not create a fifo"); + + let error = run("read", &json!({ "path": path })).err().unwrap(); + assert!(error.contains("not a regular file"), "{error}"); +} + +#[test] +fn grep_errors_on_a_missing_path_instead_of_reporting_no_matches() { + let dir = temp_dir("grep-missing"); + let error = run( + "grep", + &json!({ "pattern": "needle", "path": dir.join("does-not-exist") }), + ) + .err() + .expect("missing path must be an error"); + assert!(error.contains("no such file or directory"), "{error}"); +} + +#[test] +fn grep_says_when_it_skipped_files() { + let dir = temp_dir("grep-skip"); + let mut body = String::from("needle\n"); + body.push_str(&"x".repeat(5 * 1024 * 1024)); + fs::write(dir.join("big.txt"), body).unwrap(); + + let text = run("grep", &json!({ "pattern": "needle", "path": dir })).unwrap(); + assert!(text.contains("not searched"), "{text}"); +} + +#[test] +fn glob_follows_file_symlinks_but_not_directories() { + let dir = temp_dir("glob-symlink"); + fs::create_dir_all(dir.join("real-dir")).unwrap(); + fs::write(dir.join("real-dir/inner.rs"), "").unwrap(); + fs::write(dir.join("real.rs"), "").unwrap(); + std::os::unix::fs::symlink(dir.join("real.rs"), dir.join("link.rs")).unwrap(); + std::os::unix::fs::symlink(dir.join("real-dir"), dir.join("link-dir")).unwrap(); + + let text = run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap(); + assert!(text.contains("real.rs"), "{text}"); + assert!(text.contains("link.rs"), "{text}"); + assert!(text.contains("inner.rs"), "{text}"); +} -- cgit v1.2.3