From 87d7e185d7c6967cd20154a30696d5b16e9f4132 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 11 Jul 2026 13:38:09 -0500 Subject: [PATCH] test(add): cover insert_method revert/not-found paths and only_controller errors Signed-off-by: Diego Resendez --- src/wire.rs | 13 +++++++++++++ tests/add.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/wire.rs b/src/wire.rs index e8b88ee..38709c7 100644 --- a/src/wire.rs +++ b/src/wire.rs @@ -304,4 +304,17 @@ export class AppModule {} // ...and must NOT touch the decoy array assert!(out.contains("controllers: [Existing] }")); } + + #[test] + fn insert_method_reverts_on_parse_break() { + // a method body that makes the class re-parse fail must yield None (revert guard) + let src = "export class HomeController {\n home() {}\n}\n"; + assert!(insert_method(src, "HomeController", " zen() { ]; class Evil {\n").is_none()); + } + + #[test] + fn insert_method_none_when_class_absent() { + let src = "export class HomeController {\n home() {}\n}\n"; + assert!(insert_method(src, "NotHere", " zen() {}\n").is_none()); + } } diff --git a/tests/add.rs b/tests/add.rs index 988836a..f093b20 100644 --- a/tests/add.rs +++ b/tests/add.rs @@ -17,3 +17,29 @@ fn add_sse_appends_handler() { assert!(out.contains("@Sse(")); assert!(out.contains("import { Route, Get, Sse }") || out.contains("Sse }")); } + +#[test] +fn add_errors_when_no_controller() { + let d = tempdir().unwrap(); + std::fs::create_dir_all(d.path().join("src/controllers")).unwrap(); + Command::cargo_bin("matcha").unwrap() + .current_dir(d.path()) + .args(["add", "sse"]).assert().failure(); +} + +#[test] +fn add_errors_when_multiple_controllers_and_writes_nothing() { + let d = tempdir().unwrap(); + let cdir = d.path().join("src/controllers"); + std::fs::create_dir_all(&cdir).unwrap(); + let original = "export class X {}\n"; + for n in ["a", "b"] { + std::fs::write(cdir.join(format!("{n}.controller.ts")), original).unwrap(); + } + Command::cargo_bin("matcha").unwrap() + .current_dir(d.path()) + .args(["add", "sse"]).assert().failure(); + // no file was modified + assert_eq!(std::fs::read_to_string(cdir.join("a.controller.ts")).unwrap(), original); + assert_eq!(std::fs::read_to_string(cdir.join("b.controller.ts")).unwrap(), original); +}