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:
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
|
||||
);
|
||||
}
|
||||
+14
-6
@@ -3,14 +3,22 @@ use predicates::str::contains;
|
||||
|
||||
#[test]
|
||||
fn help_lists_all_subcommands() {
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
.arg("--help").assert().success()
|
||||
.stdout(contains("new")).stdout(contains("run"))
|
||||
.stdout(contains("create")).stdout(contains("add"));
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.arg("--help")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(contains("new"))
|
||||
.stdout(contains("run"))
|
||||
.stdout(contains("create"))
|
||||
.stdout(contains("add"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_requires_a_name() {
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
.arg("new").assert().failure(); // missing <name>
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.arg("new")
|
||||
.assert()
|
||||
.failure(); // missing <name>
|
||||
}
|
||||
+15
-6
@@ -3,20 +3,29 @@ use tempfile::tempdir;
|
||||
|
||||
fn seed_module(dir: &std::path::Path) {
|
||||
std::fs::create_dir_all(dir.join("src/controllers")).unwrap();
|
||||
std::fs::write(dir.join("src/app.module.ts"),
|
||||
"import { Module } from '@green-tea/core';\n\n\
|
||||
@Module({ controllers: [] })\nexport class AppModule {}\n").unwrap();
|
||||
std::fs::write(
|
||||
dir.join("src/app.module.ts"),
|
||||
"import { Module } from '@green-tea/core';\n\n\
|
||||
@Module({ controllers: [] })\nexport class AppModule {}\n",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_controller_writes_and_wires() {
|
||||
let d = tempdir().unwrap();
|
||||
seed_module(d.path());
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["create", "controller", "Users"]).assert().success();
|
||||
.args(["create", "controller", "Users"])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
assert!(d.path().join("src/controllers/users.controller.ts").exists());
|
||||
assert!(d
|
||||
.path()
|
||||
.join("src/controllers/users.controller.ts")
|
||||
.exists());
|
||||
let module = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap();
|
||||
assert!(module.contains("UsersController"));
|
||||
assert!(module.contains("import { UsersController }"));
|
||||
|
||||
+38
-10
@@ -5,9 +5,12 @@ use tempfile::tempdir;
|
||||
#[test]
|
||||
fn new_scaffolds_into_named_dir() {
|
||||
let d = tempdir().unwrap();
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "my-api"]).assert().success();
|
||||
.args(["new", "my-api"])
|
||||
.assert()
|
||||
.success();
|
||||
let root = d.path().join("my-api");
|
||||
assert!(root.join("src/app.module.ts").exists());
|
||||
assert!(root.join("public/index.html").exists());
|
||||
@@ -17,9 +20,12 @@ fn new_scaffolds_into_named_dir() {
|
||||
fn new_refuses_existing_dir() {
|
||||
let d = tempdir().unwrap();
|
||||
std::fs::create_dir(d.path().join("taken")).unwrap();
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "taken"]).assert().failure();
|
||||
.args(["new", "taken"])
|
||||
.assert()
|
||||
.failure();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -29,15 +35,37 @@ fn new_clones_local_template() {
|
||||
let src = d.path().join("src-tmpl");
|
||||
std::fs::create_dir_all(src.join("public")).unwrap();
|
||||
std::fs::write(src.join("public/marker.txt"), "hi").unwrap();
|
||||
Proc::new("git").args(["init", "-q"]).current_dir(&src).status().unwrap();
|
||||
Proc::new("git").args(["add", "."]).current_dir(&src).status().unwrap();
|
||||
Proc::new("git").args(["-c","user.email=t@t","-c","user.name=t",
|
||||
"commit","-qm","init"]).current_dir(&src).status().unwrap();
|
||||
Proc::new("git")
|
||||
.args(["init", "-q"])
|
||||
.current_dir(&src)
|
||||
.status()
|
||||
.unwrap();
|
||||
Proc::new("git")
|
||||
.args(["add", "."])
|
||||
.current_dir(&src)
|
||||
.status()
|
||||
.unwrap();
|
||||
Proc::new("git")
|
||||
.args([
|
||||
"-c",
|
||||
"user.email=t@t",
|
||||
"-c",
|
||||
"user.name=t",
|
||||
"commit",
|
||||
"-qm",
|
||||
"init",
|
||||
])
|
||||
.current_dir(&src)
|
||||
.status()
|
||||
.unwrap();
|
||||
|
||||
let url = format!("file://{}", src.display());
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "cloned", "--template-url", &url]).assert().success();
|
||||
.args(["new", "cloned", "--template-url", &url])
|
||||
.assert()
|
||||
.success();
|
||||
assert!(d.path().join("cloned/public/marker.txt").exists());
|
||||
assert!(!d.path().join("cloned/.git").exists()); // .git stripped
|
||||
}
|
||||
Reference in new issue
Block a user