diff options
Diffstat (limited to 'tests/tools.rs')
| -rw-r--r-- | tests/tools.rs | 110 |
1 files changed, 109 insertions, 1 deletions
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() { | |||
| 51 | ) | 51 | ) |
| 52 | .expect("unique edit should plan"); | 52 | .expect("unique edit should plan"); |
| 53 | 53 | ||
| 54 | let preview = change.preview(); | 54 | let preview = change.preview().unwrap(); |
| 55 | assert!(preview.contains("- return 0;"), "{preview}"); | 55 | assert!(preview.contains("- return 0;"), "{preview}"); |
| 56 | assert!(preview.contains("+ return 1;"), "{preview}"); | 56 | assert!(preview.contains("+ return 1;"), "{preview}"); |
| 57 | 57 | ||
| @@ -311,3 +311,111 @@ fn bash_does_not_inherit_the_api_key() { | |||
| 311 | std::env::remove_var("OPENCODE_API_KEY"); | 311 | std::env::remove_var("OPENCODE_API_KEY"); |
| 312 | assert!(text.contains('0'), "the key was visible to the child: {text}"); | 312 | assert!(text.contains('0'), "the key was visible to the child: {text}"); |
| 313 | } | 313 | } |
| 314 | |||
| 315 | #[test] | ||
| 316 | fn edit_matches_crlf_files_without_visible_carriage_returns() { | ||
| 317 | let dir = temp_dir("edit-crlf"); | ||
| 318 | let path = dir.join("win.c"); | ||
| 319 | fs::write(&path, "int main(void) {\r\n return 0;\r\n}\r\n").unwrap(); | ||
| 320 | |||
| 321 | // The model copies what `read` showed it: plain newlines. | ||
| 322 | let change = tools::plan( | ||
| 323 | "edit", | ||
| 324 | &json!({ | ||
| 325 | "path": path, | ||
| 326 | "old": "int main(void) {\n return 0;\n", | ||
| 327 | "new": "int main(void) {\n return 1;\n" | ||
| 328 | }), | ||
| 329 | ) | ||
| 330 | .expect("CRLF file should still match"); | ||
| 331 | |||
| 332 | change.apply().expect("apply should succeed"); | ||
| 333 | assert_eq!( | ||
| 334 | fs::read_to_string(&path).unwrap(), | ||
| 335 | "int main(void) {\r\n return 1;\r\n}\r\n" | ||
| 336 | ); | ||
| 337 | } | ||
| 338 | |||
| 339 | #[test] | ||
| 340 | fn edit_refuses_when_the_file_changed_while_waiting_for_approval() { | ||
| 341 | let dir = temp_dir("edit-concurrent"); | ||
| 342 | let path = dir.join("f.txt"); | ||
| 343 | fs::write(&path, "alpha\n").unwrap(); | ||
| 344 | |||
| 345 | let change = tools::plan("edit", &json!({ "path": path, "old": "alpha", "new": "beta" })) | ||
| 346 | .unwrap(); | ||
| 347 | // Something else (cargo fmt, the user's editor) ran before approval. | ||
| 348 | fs::write(&path, "alpha\nalpha\n").unwrap(); | ||
| 349 | |||
| 350 | let error = change.apply().err().expect("apply must refuse"); | ||
| 351 | assert!(error.contains("appears 2 times"), "{error}"); | ||
| 352 | assert_eq!(fs::read_to_string(&path).unwrap(), "alpha\nalpha\n"); | ||
| 353 | } | ||
| 354 | |||
| 355 | #[test] | ||
| 356 | fn write_preserves_the_existing_file_mode() { | ||
| 357 | use std::os::unix::fs::PermissionsExt; | ||
| 358 | let dir = temp_dir("write-mode"); | ||
| 359 | let path = dir.join("script.sh"); | ||
| 360 | fs::write(&path, "old").unwrap(); | ||
| 361 | fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap(); | ||
| 362 | |||
| 363 | tools::plan("write", &json!({ "path": path, "content": "new" })) | ||
| 364 | .unwrap() | ||
| 365 | .apply() | ||
| 366 | .unwrap(); | ||
| 367 | |||
| 368 | let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777; | ||
| 369 | assert_eq!(mode, 0o755); | ||
| 370 | assert_eq!(fs::read_to_string(&path).unwrap(), "new"); | ||
| 371 | } | ||
| 372 | |||
| 373 | #[test] | ||
| 374 | fn read_refuses_special_files() { | ||
| 375 | let dir = temp_dir("read-fifo"); | ||
| 376 | let path = dir.join("fifo"); | ||
| 377 | let name = std::ffi::CString::new(path.to_str().unwrap()).unwrap(); | ||
| 378 | let made = unsafe { libc::mkfifo(name.as_ptr(), 0o600) }; | ||
| 379 | assert_eq!(made, 0, "could not create a fifo"); | ||
| 380 | |||
| 381 | let error = run("read", &json!({ "path": path })).err().unwrap(); | ||
| 382 | assert!(error.contains("not a regular file"), "{error}"); | ||
| 383 | } | ||
| 384 | |||
| 385 | #[test] | ||
| 386 | fn grep_errors_on_a_missing_path_instead_of_reporting_no_matches() { | ||
| 387 | let dir = temp_dir("grep-missing"); | ||
| 388 | let error = run( | ||
| 389 | "grep", | ||
| 390 | &json!({ "pattern": "needle", "path": dir.join("does-not-exist") }), | ||
| 391 | ) | ||
| 392 | .err() | ||
| 393 | .expect("missing path must be an error"); | ||
| 394 | assert!(error.contains("no such file or directory"), "{error}"); | ||
| 395 | } | ||
| 396 | |||
| 397 | #[test] | ||
| 398 | fn grep_says_when_it_skipped_files() { | ||
| 399 | let dir = temp_dir("grep-skip"); | ||
| 400 | let mut body = String::from("needle\n"); | ||
| 401 | body.push_str(&"x".repeat(5 * 1024 * 1024)); | ||
| 402 | fs::write(dir.join("big.txt"), body).unwrap(); | ||
| 403 | |||
| 404 | let text = run("grep", &json!({ "pattern": "needle", "path": dir })).unwrap(); | ||
| 405 | assert!(text.contains("not searched"), "{text}"); | ||
| 406 | } | ||
| 407 | |||
| 408 | #[test] | ||
| 409 | fn glob_follows_file_symlinks_but_not_directories() { | ||
| 410 | let dir = temp_dir("glob-symlink"); | ||
| 411 | fs::create_dir_all(dir.join("real-dir")).unwrap(); | ||
| 412 | fs::write(dir.join("real-dir/inner.rs"), "").unwrap(); | ||
| 413 | fs::write(dir.join("real.rs"), "").unwrap(); | ||
| 414 | std::os::unix::fs::symlink(dir.join("real.rs"), dir.join("link.rs")).unwrap(); | ||
| 415 | std::os::unix::fs::symlink(dir.join("real-dir"), dir.join("link-dir")).unwrap(); | ||
| 416 | |||
| 417 | let text = run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap(); | ||
| 418 | assert!(text.contains("real.rs"), "{text}"); | ||
| 419 | assert!(text.contains("link.rs"), "{text}"); | ||
| 420 | assert!(text.contains("inner.rs"), "{text}"); | ||
| 421 | } | ||
