@canonical/summon-core 0.22.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -10
- package/src/discovery/discoverGeneratorTree.test.ts +926 -0
- package/src/discovery/discoverGeneratorTree.ts +14 -3
- package/src/stamp/applyStamp.test.ts +55 -2
- package/src/stamp/applyStamp.ts +4 -4
- package/src/template/ejsEngine.test.ts +30 -0
- package/src/template/generateStamp.test.ts +45 -0
- package/src/template/generateStamp.ts +3 -1
- package/src/template/getCommentStyle.test.ts +112 -0
- package/src/template/prependStamp.test.ts +30 -0
- package/src/template/renderFile.test.ts +27 -0
- package/src/template/renderStringAsync.test.ts +28 -0
- package/src/template/tasks/templateDir.test.ts +126 -0
|
@@ -24,6 +24,7 @@ import type { GeneratorNode, GeneratorOrigin } from "./types.js";
|
|
|
24
24
|
*/
|
|
25
25
|
const mergeIntoTree = (parent: GeneratorNode, child: GeneratorNode): void => {
|
|
26
26
|
const existing = parent.children.get(child.name);
|
|
27
|
+
/* v8 ignore start -- defensive: readdir never returns duplicate names, so existing is always undefined in practice */
|
|
27
28
|
if (existing) {
|
|
28
29
|
// Merge children - existing (local) takes precedence for indexPath
|
|
29
30
|
for (const [name, grandchild] of child.children) {
|
|
@@ -36,6 +37,7 @@ const mergeIntoTree = (parent: GeneratorNode, child: GeneratorNode): void => {
|
|
|
36
37
|
existing.indexPath = child.indexPath;
|
|
37
38
|
}
|
|
38
39
|
} else {
|
|
40
|
+
/* v8 ignore stop */
|
|
39
41
|
parent.children.set(child.name, child);
|
|
40
42
|
}
|
|
41
43
|
};
|
|
@@ -118,7 +120,9 @@ const insertGeneratorAtPath = (
|
|
|
118
120
|
generatorCache.set(pathStr, generator);
|
|
119
121
|
|
|
120
122
|
for (let i = 0; i < segments.length; i++) {
|
|
121
|
-
const segment = segments[i]
|
|
123
|
+
const segment = segments[i];
|
|
124
|
+
/* v8 ignore next — structurally guaranteed by loop bounds and filter(Boolean) */
|
|
125
|
+
if (!segment) throw new Error(`Expected path segment at index ${i}`);
|
|
122
126
|
const isLast = i === segments.length - 1;
|
|
123
127
|
|
|
124
128
|
if (!current.children.has(segment)) {
|
|
@@ -131,7 +135,8 @@ const insertGeneratorAtPath = (
|
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
const child = current.children.get(segment);
|
|
134
|
-
|
|
138
|
+
/* v8 ignore next — child was just set in the block above */
|
|
139
|
+
if (!child) throw new Error(`Expected child node for segment "${segment}"`);
|
|
135
140
|
|
|
136
141
|
if (isLast) {
|
|
137
142
|
// Mark as having a generator (use path as synthetic indexPath)
|
|
@@ -240,6 +245,7 @@ const discoverNodeModulesPackages = async (
|
|
|
240
245
|
* Default is ~/.bun/install/global/node_modules
|
|
241
246
|
*/
|
|
242
247
|
const getBunGlobalNodeModules = (): string => {
|
|
248
|
+
/* v8 ignore next 2 -- HOME is always set in Node.js environments */
|
|
243
249
|
const bunInstallDir =
|
|
244
250
|
process.env.BUN_INSTALL ?? path.join(process.env.HOME ?? "~", ".bun");
|
|
245
251
|
return path.join(bunInstallDir, "install", "global", "node_modules");
|
|
@@ -281,7 +287,12 @@ const getNpmGlobalNodeModules = async (): Promise<string | null> => {
|
|
|
281
287
|
const commonPaths = [
|
|
282
288
|
"/usr/local/lib/node_modules",
|
|
283
289
|
"/usr/lib/node_modules",
|
|
284
|
-
path.join(
|
|
290
|
+
path.join(
|
|
291
|
+
/* v8 ignore next */ process.env.HOME ?? "~",
|
|
292
|
+
".npm-global",
|
|
293
|
+
"lib",
|
|
294
|
+
"node_modules",
|
|
295
|
+
),
|
|
285
296
|
];
|
|
286
297
|
|
|
287
298
|
for (const p of commonPaths) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { pure } from "@canonical/task";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { type Effect, mkdir, pure, task, writeFile } from "@canonical/task";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
3
|
import type StampConfig from "../types/StampConfig.js";
|
|
4
4
|
import applyStamp from "./applyStamp.js";
|
|
5
5
|
import runTaskWithStamp from "./runTaskWithStamp.js";
|
|
@@ -196,4 +196,57 @@ describe("runTaskWithStamp", () => {
|
|
|
196
196
|
});
|
|
197
197
|
expect(result).toBe("done");
|
|
198
198
|
});
|
|
199
|
+
|
|
200
|
+
it("stamps WriteFile effect content when stamp is provided", async () => {
|
|
201
|
+
const captured: Effect[] = [];
|
|
202
|
+
const task = writeFile("/tmp/test-stamp-output.ts", "const x = 1;");
|
|
203
|
+
|
|
204
|
+
await runTaskWithStamp(task, {
|
|
205
|
+
stamp,
|
|
206
|
+
onEffectStart: (effect) => {
|
|
207
|
+
captured.push({ ...effect });
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const writeEffect = captured.find((e) => e._tag === "WriteFile");
|
|
212
|
+
expect(writeEffect).toBeDefined();
|
|
213
|
+
// The content should have been mutated before our onEffectStart was called
|
|
214
|
+
expect((writeEffect as { content: string }).content).toContain(
|
|
215
|
+
"Generated by test-gen v1.0.0",
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("calls user-provided onEffectStart after stamping", async () => {
|
|
220
|
+
const userCallback = vi.fn();
|
|
221
|
+
const task = writeFile("/tmp/test-stamp-cb.ts", "hello");
|
|
222
|
+
|
|
223
|
+
await runTaskWithStamp(task, {
|
|
224
|
+
stamp,
|
|
225
|
+
onEffectStart: userCallback,
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
expect(userCallback).toHaveBeenCalled();
|
|
229
|
+
const callArg = userCallback.mock.calls[0][0];
|
|
230
|
+
expect(callArg._tag).toBe("WriteFile");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("does not modify non-WriteFile effects", async () => {
|
|
234
|
+
const captured: Effect[] = [];
|
|
235
|
+
// mkdir produces a MakeDir effect, not WriteFile
|
|
236
|
+
const mkdirTask = task(mkdir("/tmp/test-stamp-dir"))
|
|
237
|
+
.chain(() => task(writeFile("/tmp/test-stamp-dir/out.ts", "code")))
|
|
238
|
+
.unwrap();
|
|
239
|
+
|
|
240
|
+
await runTaskWithStamp(mkdirTask, {
|
|
241
|
+
stamp,
|
|
242
|
+
onEffectStart: (effect) => {
|
|
243
|
+
captured.push({ ...effect });
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const mkdirEffect = captured.find((e) => e._tag === "MakeDir");
|
|
248
|
+
expect(mkdirEffect).toBeDefined();
|
|
249
|
+
// MakeDir effect should pass through unchanged
|
|
250
|
+
expect(mkdirEffect?._tag).toBe("MakeDir");
|
|
251
|
+
});
|
|
199
252
|
});
|
package/src/stamp/applyStamp.ts
CHANGED
|
@@ -46,6 +46,7 @@ const COMMENT_STYLES: Record<
|
|
|
46
46
|
".md": { blockStart: "<!--", blockEnd: "-->" },
|
|
47
47
|
".mdx": { blockStart: "{/*", blockEnd: "*/}" },
|
|
48
48
|
".sql": { single: "--" },
|
|
49
|
+
".json": {},
|
|
49
50
|
};
|
|
50
51
|
|
|
51
52
|
/**
|
|
@@ -67,11 +68,10 @@ export default function applyStamp(
|
|
|
67
68
|
const stampText = `Generated by ${stamp.generator} v${stamp.version}`;
|
|
68
69
|
let stampLine: string;
|
|
69
70
|
|
|
70
|
-
if (style.
|
|
71
|
-
if (!style.blockStart || !style.blockEnd) return content;
|
|
72
|
-
stampLine = `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
73
|
-
} else if (style.single) {
|
|
71
|
+
if (style.single && !style.preferBlock) {
|
|
74
72
|
stampLine = `${style.single} ${stampText}`;
|
|
73
|
+
} else if (style.blockStart && style.blockEnd) {
|
|
74
|
+
stampLine = `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
75
75
|
} else {
|
|
76
76
|
return content;
|
|
77
77
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import ejsEngine from "./ejsEngine.js";
|
|
3
|
+
|
|
4
|
+
describe("ejsEngine", () => {
|
|
5
|
+
describe("render (sync)", () => {
|
|
6
|
+
it("renders a template string synchronously", () => {
|
|
7
|
+
expect(ejsEngine.render("Hello, <%= name %>!", { name: "World" })).toBe(
|
|
8
|
+
"Hello, World!",
|
|
9
|
+
);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe("renderAsync", () => {
|
|
14
|
+
it("renders a template string asynchronously", async () => {
|
|
15
|
+
const result = await ejsEngine.renderAsync("Hello, <%= name %>!", {
|
|
16
|
+
name: "Async",
|
|
17
|
+
});
|
|
18
|
+
expect(result).toBe("Hello, Async!");
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe("renderFile", () => {
|
|
23
|
+
it("renders a template file", async () => {
|
|
24
|
+
// Use a known fixture or just verify it throws for non-existent files
|
|
25
|
+
await expect(
|
|
26
|
+
ejsEngine.renderFile("/nonexistent-path.ejs", {}),
|
|
27
|
+
).rejects.toThrow();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type StampConfig from "../types/StampConfig.js";
|
|
3
|
+
import generateStamp from "./generateStamp.js";
|
|
4
|
+
|
|
5
|
+
const stamp: StampConfig = { generator: "test-gen", version: "1.0.0" };
|
|
6
|
+
|
|
7
|
+
describe("generateStamp", () => {
|
|
8
|
+
it("generates single-line stamp for .ts files", () => {
|
|
9
|
+
expect(generateStamp("file.ts", stamp)).toBe(
|
|
10
|
+
"// Generated by test-gen v1.0.0",
|
|
11
|
+
);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("generates block stamp for .css files (preferBlock)", () => {
|
|
15
|
+
expect(generateStamp("style.css", stamp)).toBe(
|
|
16
|
+
"/* Generated by test-gen v1.0.0 */",
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("generates block stamp for .html files (block only, no single)", () => {
|
|
21
|
+
expect(generateStamp("page.html", stamp)).toBe(
|
|
22
|
+
"<!-- Generated by test-gen v1.0.0 -->",
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("generates JSX stamp for .mdx files", () => {
|
|
27
|
+
expect(generateStamp("doc.mdx", stamp)).toBe(
|
|
28
|
+
"{/* Generated by test-gen v1.0.0 */}",
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("generates hash stamp for .py files", () => {
|
|
33
|
+
expect(generateStamp("main.py", stamp)).toBe(
|
|
34
|
+
"# Generated by test-gen v1.0.0",
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("returns null for .json (no single or block comment)", () => {
|
|
39
|
+
expect(generateStamp("data.json", stamp)).toBeNull();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("returns null for unknown extensions", () => {
|
|
43
|
+
expect(generateStamp("file.dat", stamp)).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -19,9 +19,11 @@ export default function generateStamp(
|
|
|
19
19
|
return `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/* v8 ignore next -- unreachable false branch: if we reach here, style.single is always truthy */
|
|
22
23
|
if (style.single) {
|
|
23
24
|
return `${style.single} ${stampText}`;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
/* v8 ignore next -- structurally unreachable: line 14 guards !style.single && !style.blockStart */
|
|
28
|
+
throw new Error(`No comment syntax for stamp in ${filePath}`);
|
|
27
29
|
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import getCommentStyle from "./getCommentStyle.js";
|
|
3
|
+
|
|
4
|
+
describe("getCommentStyle", () => {
|
|
5
|
+
it.each([
|
|
6
|
+
[".ts", "//", "/*", "*/"],
|
|
7
|
+
[".tsx", "//", "/*", "*/"],
|
|
8
|
+
[".js", "//", "/*", "*/"],
|
|
9
|
+
[".jsx", "//", "/*", "*/"],
|
|
10
|
+
[".mjs", "//", "/*", "*/"],
|
|
11
|
+
[".cjs", "//", "/*", "*/"],
|
|
12
|
+
[".scss", "//", "/*", "*/"],
|
|
13
|
+
[".sass", "//", undefined, undefined],
|
|
14
|
+
[".less", "//", "/*", "*/"],
|
|
15
|
+
[".go", "//", "/*", "*/"],
|
|
16
|
+
[".rs", "//", "/*", "*/"],
|
|
17
|
+
[".java", "//", "/*", "*/"],
|
|
18
|
+
[".kt", "//", "/*", "*/"],
|
|
19
|
+
[".swift", "//", "/*", "*/"],
|
|
20
|
+
[".c", "//", "/*", "*/"],
|
|
21
|
+
[".cpp", "//", "/*", "*/"],
|
|
22
|
+
[".h", "//", "/*", "*/"],
|
|
23
|
+
[".hpp", "//", "/*", "*/"],
|
|
24
|
+
[".php", "//", "/*", "*/"],
|
|
25
|
+
])("returns single-line '//' style for %s", (ext, single, blockStart, blockEnd) => {
|
|
26
|
+
const style = getCommentStyle(`file${ext}`);
|
|
27
|
+
expect(style).not.toBeNull();
|
|
28
|
+
expect(style?.single).toBe(single);
|
|
29
|
+
expect(style?.blockStart).toBe(blockStart);
|
|
30
|
+
expect(style?.blockEnd).toBe(blockEnd);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it.each([
|
|
34
|
+
[".css", "/*", "*/", true],
|
|
35
|
+
])("returns block style with preferBlock for %s", (ext, start, end, pref) => {
|
|
36
|
+
const style = getCommentStyle(`file${ext}`);
|
|
37
|
+
expect(style).not.toBeNull();
|
|
38
|
+
expect(style?.blockStart).toBe(start);
|
|
39
|
+
expect(style?.blockEnd).toBe(end);
|
|
40
|
+
expect(style?.preferBlock).toBe(pref);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it.each([
|
|
44
|
+
[".html", "<!--", "-->"],
|
|
45
|
+
[".htm", "<!--", "-->"],
|
|
46
|
+
[".xml", "<!--", "-->"],
|
|
47
|
+
[".svg", "<!--", "-->"],
|
|
48
|
+
[".vue", "<!--", "-->"],
|
|
49
|
+
[".svelte", "<!--", "-->"],
|
|
50
|
+
[".md", "<!--", "-->"],
|
|
51
|
+
])("returns HTML comment style for %s", (ext, start, end) => {
|
|
52
|
+
const style = getCommentStyle(`file${ext}`);
|
|
53
|
+
expect(style).not.toBeNull();
|
|
54
|
+
expect(style?.blockStart).toBe(start);
|
|
55
|
+
expect(style?.blockEnd).toBe(end);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("returns JSX comment style for .mdx", () => {
|
|
59
|
+
const style = getCommentStyle("file.mdx");
|
|
60
|
+
expect(style).not.toBeNull();
|
|
61
|
+
expect(style?.blockStart).toBe("{/*");
|
|
62
|
+
expect(style?.blockEnd).toBe("*/}");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it.each([
|
|
66
|
+
[".yaml", "#"],
|
|
67
|
+
[".yml", "#"],
|
|
68
|
+
[".toml", "#"],
|
|
69
|
+
[".sh", "#"],
|
|
70
|
+
[".bash", "#"],
|
|
71
|
+
[".zsh", "#"],
|
|
72
|
+
[".fish", "#"],
|
|
73
|
+
[".py", "#"],
|
|
74
|
+
[".rb", "#"],
|
|
75
|
+
[".pl", "#"],
|
|
76
|
+
])("returns hash comment style for %s", (ext, single) => {
|
|
77
|
+
const style = getCommentStyle(`file${ext}`);
|
|
78
|
+
expect(style).not.toBeNull();
|
|
79
|
+
expect(style?.single).toBe(single);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("returns '--' comment style for .sql", () => {
|
|
83
|
+
const style = getCommentStyle("file.sql");
|
|
84
|
+
expect(style).not.toBeNull();
|
|
85
|
+
expect(style?.single).toBe("--");
|
|
86
|
+
expect(style?.blockStart).toBe("/*");
|
|
87
|
+
expect(style?.blockEnd).toBe("*/");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("returns empty object (no comment fields) for .json", () => {
|
|
91
|
+
const style = getCommentStyle("file.json");
|
|
92
|
+
expect(style).not.toBeNull();
|
|
93
|
+
expect(style?.single).toBeUndefined();
|
|
94
|
+
expect(style?.blockStart).toBeUndefined();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("returns null for unknown extensions", () => {
|
|
98
|
+
expect(getCommentStyle("file.dat")).toBeNull();
|
|
99
|
+
expect(getCommentStyle("file.bin")).toBeNull();
|
|
100
|
+
expect(getCommentStyle("file.xyz")).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("returns null for extensionless files", () => {
|
|
104
|
+
expect(getCommentStyle("Makefile")).toBeNull();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("handles uppercase extensions via lowercase normalization", () => {
|
|
108
|
+
const style = getCommentStyle("file.TS");
|
|
109
|
+
expect(style).not.toBeNull();
|
|
110
|
+
expect(style?.single).toBe("//");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import prependStamp from "./prependStamp.js";
|
|
3
|
+
|
|
4
|
+
describe("prependStamp", () => {
|
|
5
|
+
it("prepends stamp to normal content", () => {
|
|
6
|
+
expect(prependStamp("content", "// stamp")).toBe("// stamp\ncontent");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("places stamp after shebang line", () => {
|
|
10
|
+
const content = "#!/bin/bash\necho hello";
|
|
11
|
+
expect(prependStamp(content, "# stamp")).toBe(
|
|
12
|
+
"#!/bin/bash\n# stamp\necho hello",
|
|
13
|
+
);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("handles shebang-only content (no newline) — prepends normally", () => {
|
|
17
|
+
const content = "#!/bin/bash";
|
|
18
|
+
expect(prependStamp(content, "# stamp")).toBe("# stamp\n#!/bin/bash");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("handles empty content", () => {
|
|
22
|
+
expect(prependStamp("", "// stamp")).toBe("// stamp\n");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("handles multiline content", () => {
|
|
26
|
+
expect(prependStamp("line1\nline2\nline3", "// stamp")).toBe(
|
|
27
|
+
"// stamp\nline1\nline2\nline3",
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import renderFile from "./renderFile.js";
|
|
3
|
+
import type TemplatingEngine from "./TemplatingEngine.js";
|
|
4
|
+
|
|
5
|
+
describe("renderFile", () => {
|
|
6
|
+
it("delegates to the engine's renderFile method", async () => {
|
|
7
|
+
const mockEngine: TemplatingEngine = {
|
|
8
|
+
render: () => "",
|
|
9
|
+
renderAsync: async () => "",
|
|
10
|
+
renderFile: async (templatePath, vars) =>
|
|
11
|
+
`rendered ${templatePath} with ${JSON.stringify(vars)}`,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const result = await renderFile(
|
|
15
|
+
"/tpl/foo.ejs",
|
|
16
|
+
{ name: "bar" },
|
|
17
|
+
mockEngine,
|
|
18
|
+
);
|
|
19
|
+
expect(result).toBe('rendered /tpl/foo.ejs with {"name":"bar"}');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("uses default ejs engine when no engine is provided", async () => {
|
|
23
|
+
// We don't have a real template file, but we can verify it calls ejsEngine.
|
|
24
|
+
// Since ejs.renderFile will fail without a real file, we expect it to throw.
|
|
25
|
+
await expect(renderFile("/nonexistent.ejs", {})).rejects.toThrow();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import renderStringAsync from "./renderStringAsync.js";
|
|
3
|
+
import type TemplatingEngine from "./TemplatingEngine.js";
|
|
4
|
+
|
|
5
|
+
describe("renderStringAsync", () => {
|
|
6
|
+
it("delegates to the engine's renderAsync method", async () => {
|
|
7
|
+
const mockEngine: TemplatingEngine = {
|
|
8
|
+
render: () => "",
|
|
9
|
+
renderAsync: async (template, vars) =>
|
|
10
|
+
`async: ${template} ${JSON.stringify(vars)}`,
|
|
11
|
+
renderFile: async () => "",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const result = await renderStringAsync(
|
|
15
|
+
"Hello <%= name %>",
|
|
16
|
+
{ name: "World" },
|
|
17
|
+
mockEngine,
|
|
18
|
+
);
|
|
19
|
+
expect(result).toBe('async: Hello <%= name %> {"name":"World"}');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("uses default ejs engine when no engine is provided", async () => {
|
|
23
|
+
const result = await renderStringAsync("Hello, <%= name %>!", {
|
|
24
|
+
name: "World",
|
|
25
|
+
});
|
|
26
|
+
expect(result).toBe("Hello, World!");
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { dryRunWith, type Effect, filterEffects } from "@canonical/task";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import templateDir from "./templateDir.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Helper: build a mocks Map for dryRunWith.
|
|
7
|
+
* Glob returns the given files; ReadFile returns a simple template body.
|
|
8
|
+
*/
|
|
9
|
+
const buildMocks = (files: string[]) =>
|
|
10
|
+
new Map<string, (e: Effect) => unknown>([
|
|
11
|
+
["Glob", () => files],
|
|
12
|
+
["ReadFile", (e) => `content of ${(e as { path: string }).path}`],
|
|
13
|
+
["WriteFile", () => undefined],
|
|
14
|
+
["MakeDir", () => undefined],
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
describe("templateDir", () => {
|
|
18
|
+
it("processes files from glob and creates write effects", () => {
|
|
19
|
+
const t = templateDir({
|
|
20
|
+
source: "/templates",
|
|
21
|
+
dest: "/output",
|
|
22
|
+
vars: { name: "Button" },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const { effects } = dryRunWith(t, buildMocks(["component.tsx.ejs"]));
|
|
26
|
+
|
|
27
|
+
expect(effects.some((e) => e._tag === "Glob")).toBe(true);
|
|
28
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
29
|
+
expect(writes).toHaveLength(1);
|
|
30
|
+
expect((writes[0] as { path: string }).path).toBe("/output/component.tsx");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("strips .ejs extension from output files", () => {
|
|
34
|
+
const t = templateDir({
|
|
35
|
+
source: "/tpl",
|
|
36
|
+
dest: "/out",
|
|
37
|
+
vars: {},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const { effects } = dryRunWith(t, buildMocks(["file.ts.ejs"]));
|
|
41
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
42
|
+
expect((writes[0] as { path: string }).path).toBe("/out/file.ts");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("applies rename mappings", () => {
|
|
46
|
+
const t = templateDir({
|
|
47
|
+
source: "/tpl",
|
|
48
|
+
dest: "/out",
|
|
49
|
+
vars: {},
|
|
50
|
+
rename: { "file.ts": "renamed.ts" },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const { effects } = dryRunWith(t, buildMocks(["file.ts"]));
|
|
54
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
55
|
+
expect((writes[0] as { path: string }).path).toBe("/out/renamed.ts");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("renders template variables in destination filenames", () => {
|
|
59
|
+
const t = templateDir({
|
|
60
|
+
source: "/tpl",
|
|
61
|
+
dest: "/out",
|
|
62
|
+
vars: { name: "Button" },
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const { effects } = dryRunWith(t, buildMocks(["<%= name %>.tsx"]));
|
|
66
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
67
|
+
expect((writes[0] as { path: string }).path).toBe("/out/Button.tsx");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("filters files matching ignore patterns", () => {
|
|
71
|
+
const t = templateDir({
|
|
72
|
+
source: "/tpl",
|
|
73
|
+
dest: "/out",
|
|
74
|
+
vars: {},
|
|
75
|
+
ignore: ["*.md"],
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const { effects } = dryRunWith(
|
|
79
|
+
t,
|
|
80
|
+
buildMocks(["component.ts", "README.md"]),
|
|
81
|
+
);
|
|
82
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
83
|
+
expect(writes).toHaveLength(1);
|
|
84
|
+
expect((writes[0] as { path: string }).path).toBe("/out/component.ts");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("filters files matching glob-star ignore patterns", () => {
|
|
88
|
+
const t = templateDir({
|
|
89
|
+
source: "/tpl",
|
|
90
|
+
dest: "/out",
|
|
91
|
+
vars: {},
|
|
92
|
+
ignore: ["**/*.test.ts"],
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const { effects } = dryRunWith(
|
|
96
|
+
t,
|
|
97
|
+
buildMocks(["component.ts", "tests/component.test.ts"]),
|
|
98
|
+
);
|
|
99
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
100
|
+
expect(writes).toHaveLength(1);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("handles empty glob result", () => {
|
|
104
|
+
const t = templateDir({
|
|
105
|
+
source: "/empty",
|
|
106
|
+
dest: "/out",
|
|
107
|
+
vars: {},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const { effects } = dryRunWith(t, buildMocks([]));
|
|
111
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
112
|
+
expect(writes).toHaveLength(0);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("handles multiple files", () => {
|
|
116
|
+
const t = templateDir({
|
|
117
|
+
source: "/tpl",
|
|
118
|
+
dest: "/out",
|
|
119
|
+
vars: {},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const { effects } = dryRunWith(t, buildMocks(["a.ts", "b.ts", "c.ts"]));
|
|
123
|
+
const writes = filterEffects(effects, "WriteFile");
|
|
124
|
+
expect(writes).toHaveLength(3);
|
|
125
|
+
});
|
|
126
|
+
});
|