fix(add): merge imports into existing brace + capability idempotency; fmt
- wire::add_import now merges a new symbol into an already-present
`import { ... } from '<from>';` brace instead of appending a second,
colliding import line for the same module (a duplicate ESM binding
is a hard SyntaxError/TS2300, not a harmless dupe as previously
assumed). Falls back to appending a new line when the module isn't
imported yet, or if the brace-merge edit fails the parses_clean
guard.
- cmd_add::run gains a capability-level idempotency guard, keyed on
each capability's unique route-path literal, so `matcha add <cap>`
run twice no longer appends a duplicate handler + route.
- cargo fmt --all across the crate; drop the stale #[allow(dead_code)]
on wire::parses_clean now that it's demonstrably live.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 parent
65ecba05d1
commit
2f9b656692
10 files changed
+329
-79
No files matched your search
+51
-11
@@ -5,13 +5,19 @@ use tempfile::tempdir;
|
||||
fn add_sse_appends_handler() {
|
||||
let d = tempdir().unwrap();
|
||||
std::fs::create_dir_all(d.path().join("src/controllers")).unwrap();
|
||||
std::fs::write(d.path().join("src/controllers/home.controller.ts"),
|
||||
"import { Route, Get } from '@green-tea/core';\n\n\
|
||||
@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n").unwrap();
|
||||
std::fs::write(
|
||||
d.path().join("src/controllers/home.controller.ts"),
|
||||
"import { Route, Get } from '@green-tea/core';\n\n\
|
||||
@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["add", "sse"]).assert().success();
|
||||
.args(["add", "sse"])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
|
||||
assert!(out.contains("@Sse("));
|
||||
@@ -22,9 +28,34 @@ fn add_sse_appends_handler() {
|
||||
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()
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["add", "sse"]).assert().failure();
|
||||
.args(["add", "sse"])
|
||||
.assert()
|
||||
.failure();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_sse_twice_is_idempotent() {
|
||||
let d = tempdir().unwrap();
|
||||
std::fs::create_dir_all(d.path().join("src/controllers")).unwrap();
|
||||
std::fs::write(d.path().join("src/controllers/home.controller.ts"),
|
||||
"import { Route, Get } from '@green-tea/core';\n\n@Route('/')\nexport class HomeController {\n @Get('/')\n home() {}\n}\n").unwrap();
|
||||
let run = || {
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["add", "sse"])
|
||||
.assert()
|
||||
.success()
|
||||
};
|
||||
run();
|
||||
run();
|
||||
let out = std::fs::read_to_string(d.path().join("src/controllers/home.controller.ts")).unwrap();
|
||||
// exactly one Sse handler, exactly one @green-tea/core import line
|
||||
assert_eq!(out.matches("@Sse(").count(), 1);
|
||||
assert_eq!(out.matches("from '@green-tea/core'").count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -36,10 +67,19 @@ fn add_errors_when_multiple_controllers_and_writes_nothing() {
|
||||
for n in ["a", "b"] {
|
||||
std::fs::write(cdir.join(format!("{n}.controller.ts")), original).unwrap();
|
||||
}
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["add", "sse"]).assert().failure();
|
||||
.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);
|
||||
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
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user