From 5563622557ac60b6842f6c09c4c76a5ece9f6d09 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:27:16 +0100 Subject: 0.4.0: grep takes a regular expression, fixed=true for literals Invalid patterns explain themselves and point at fixed, rather than failing with a bare regex error the model cannot act on. --- tests/tools.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'tests/tools.rs') diff --git a/tests/tools.rs b/tests/tools.rs index 3df353f..8463d8d 100644 --- a/tests/tools.rs +++ b/tests/tools.rs @@ -201,3 +201,51 @@ fn read_handles_a_missing_trailing_newline() { assert!(text.contains(" 2\ttwo"), "{text}"); assert!(!text.contains("continue with offset"), "{text}"); } + +#[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(); + + let text = tools::run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); + + let text = tools::run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap(); + assert_eq!(text.lines().count(), 2, "{text}"); + + let text = tools::run( + "grep", + &json!({ "pattern": r"FN\s+MAIN", "path": dir, "ignore_case": true }), + ) + .unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); +} + +#[test] +fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() { + let dir = temp_dir("grep-fixed"); + fs::write(dir.join("f.txt"), "cost is a+b\nand ab\n").unwrap(); + + // As a regex, `a+b` matches the "ab" on line 2, not the literal "a+b". + let text = tools::run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); + assert!(text.contains("and ab"), "{text}"); + + let text = tools::run( + "grep", + &json!({ "pattern": "a+b", "path": dir, "fixed": true }), + ) + .unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); + assert!(text.contains("cost is a+b"), "{text}"); + + let error = tools::run("grep", &json!({ "pattern": "(", "path": dir })) + .err() + .expect("invalid regex must fail"); + assert!(error.contains("invalid regex"), "{error}"); + assert!(error.contains("fixed"), "{error}"); +} -- cgit v1.2.3