From e803aa488c3a95193d3e3ae358b640c299f8f121 Mon Sep 17 00:00:00 2001 From: salmonik Date: Mon, 23 Nov 2026 20:36:22 +0100 Subject: License --- tests/tools.rs | 68 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 26 deletions(-) (limited to 'tests/tools.rs') diff --git a/tests/tools.rs b/tests/tools.rs index 0321bf8..a5099e8 100644 --- a/tests/tools.rs +++ b/tests/tools.rs @@ -1,3 +1,10 @@ +/* + * This file is part of agent686 + * Copyright (C) 2026 Salmonik + * SPDX-License-Identifier: GPL-3.0-or-later + * https://git.2137697.xyz/agent686 + */ + use std::fs; use std::path::PathBuf; use std::time::Duration; @@ -24,9 +31,12 @@ fn edit_refuses_ambiguous_and_missing_text() { let path = dir.join("a.txt"); fs::write(&path, "alpha\nbeta\nalpha\n").unwrap(); - let error = tools::plan("edit", &json!({ "path": path, "old": "alpha", "new": "gamma" })) - .err() - .expect("ambiguous edit must fail"); + let error = tools::plan( + "edit", + &json!({ "path": path, "old": "alpha", "new": "gamma" }), + ) + .err() + .expect("ambiguous edit must fail"); assert!(error.contains("appears 2 times"), "{error}"); assert!(error.contains("lines 1, 3"), "{error}"); @@ -128,15 +138,23 @@ fn bash_reports_exit_code_and_times_out() { assert!(text.contains("hi"), "{text}"); assert!(text.contains("[exit 3]"), "{text}"); - let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150), &AtomicBool::new(false)).unwrap(); + let text = shell::bash( + &json!({ "cmd": "sleep 5" }), + Duration::from_millis(150), + &AtomicBool::new(false), + ) + .unwrap(); assert!(text.contains("timed out after"), "{text}"); } #[test] fn bash_rejects_a_missing_cwd() { - let error = run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" })) - .err() - .expect("missing cwd must fail"); + let error = run( + "bash", + &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" }), + ) + .err() + .expect("missing cwd must fail"); assert!(error.contains("cwd is not a directory"), "{error}"); } @@ -210,11 +228,7 @@ fn read_handles_a_missing_trailing_newline() { #[test] fn grep_uses_regular_expressions_by_default() { let dir = temp_dir("grep-regex"); - fs::write( - dir.join("f.rs"), - "fn main() {\n fn other() {\n}\n", - ) - .unwrap(); + fs::write(dir.join("f.rs"), "fn main() {\n fn other() {\n}\n").unwrap(); let text = run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 1, "{text}"); @@ -283,7 +297,11 @@ fn bash_timeout_kills_the_whole_process_group() { assert!(text.contains("timed out"), "{text}"); std::thread::sleep(Duration::from_millis(300)); - let pid: i32 = fs::read_to_string(&pidfile).unwrap().trim().parse().unwrap(); + let pid: i32 = fs::read_to_string(&pidfile) + .unwrap() + .trim() + .parse() + .unwrap(); assert!(!running(pid), "grandchild {pid} survived the timeout"); } @@ -309,7 +327,10 @@ fn bash_does_not_inherit_the_api_key() { ) .unwrap(); std::env::remove_var("OPENCODE_API_KEY"); - assert!(text.contains('0'), "the key was visible to the child: {text}"); + assert!( + text.contains('0'), + "the key was visible to the child: {text}" + ); } #[test] @@ -342,8 +363,11 @@ fn edit_refuses_when_the_file_changed_while_waiting_for_approval() { 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(); + 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(); @@ -470,11 +494,7 @@ fn bash_stderr_alone_is_labelled() { #[test] fn bash_honours_a_per_call_timeout() { let started = std::time::Instant::now(); - let text = run( - "bash", - &json!({ "cmd": "sleep 5", "timeout_seconds": 1 }), - ) - .unwrap(); + let text = run("bash", &json!({ "cmd": "sleep 5", "timeout_seconds": 1 })).unwrap(); assert!(text.contains("timed out after 1.0s"), "{text}"); assert!(started.elapsed() < std::time::Duration::from_secs(4)); } @@ -507,11 +527,7 @@ fn validate_requires_content_and_new_but_allows_empty() { fn a_pipeline_that_exceeds_the_file_limit_is_reported() { // 70 MiB through a pipeline: the grandchild (head) is the writer, so only the // file-length check can detect the limit. - let text = run( - "bash", - &json!({ "cmd": "yes | head -c 70000000" }), - ) - .unwrap(); + let text = run("bash", &json!({ "cmd": "yes | head -c 70000000" })).unwrap(); assert!( text.contains("output exceeded 64 MiB"), "should report the file limit: {text}" -- cgit v1.2.3