-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
198 lines (172 loc) · 4.91 KB
/
index.test.ts
File metadata and controls
198 lines (172 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { FileGenerator, type RenderFunction } from "./index.ts";
import ejs from "ejs";
import { glob } from "glob";
import assert from "node:assert";
import fs from "node:fs";
import test from "node:test";
import { fileURLToPath } from "node:url";
await test("computePath", async (t) => {
const fileGenerator = new FileGenerator();
const sourceDirectory = "/source/directory";
const targetDirectory = "/target/directory";
await t.test("path without extension", () => {
const templateFilePath = `${sourceDirectory}/template/file/path`;
const actual = fileGenerator.computePath(
sourceDirectory,
targetDirectory,
templateFilePath,
{},
);
const expected = `${targetDirectory}/template/file/path`;
assert.strictEqual(actual, expected);
});
await t.test("path with .template extension", () => {
const templateFilePath = `${sourceDirectory}/template/file/path.template`;
const actual = fileGenerator.computePath(
sourceDirectory,
targetDirectory,
templateFilePath,
{},
);
const expected = `${targetDirectory}/template/file/path`;
assert.strictEqual(actual, expected);
});
await t.test("path with arbitrary extension", () => {
const templateFilePath = `${sourceDirectory}/template/file/path.ext`;
const actual = fileGenerator.computePath(
sourceDirectory,
targetDirectory,
templateFilePath,
{},
);
const expected = `${targetDirectory}/template/file/path.ext`;
assert.strictEqual(actual, expected);
});
await t.test("path with defined variables", () => {
const templateFilePath = `${sourceDirectory}/template/__file__/path.__extension__`;
const variables = {
file: "file",
// Extension trimming should be done before variable substitution
extension: "template",
};
const actual = fileGenerator.computePath(
sourceDirectory,
targetDirectory,
templateFilePath,
variables,
);
const expected = `${targetDirectory}/template/file/path.template`;
assert.strictEqual(actual, expected);
});
await t.test("path with undefined variables", () => {
const templateFilePath = `${sourceDirectory}/template/__file__/path`;
const actual = fileGenerator.computePath(
sourceDirectory,
targetDirectory,
templateFilePath,
{},
);
const expected = `${targetDirectory}/template/__file__/path`;
assert.strictEqual(actual, expected);
});
});
await test("generate", async (t) => {
const writeFile = t.mock.fn<typeof fs.promises.writeFile>();
const copyFile = t.mock.fn<typeof fs.promises.copyFile>();
const mkdir = t.mock.fn<typeof fs.promises.mkdir>();
const readFile = t.mock.fn(fs.promises.readFile);
const render = t.mock.fn<RenderFunction>(async (template, data) =>
ejs.render(template, data, { async: true }),
);
const fileGenerator = new FileGenerator({
mkdir,
readFile,
writeFile,
copyFile,
render,
});
const sourceDirectory = fileURLToPath(
new URL("__fixtures__", import.meta.url),
);
const targetDirectory = "/target/directory";
const variables = {
variable: "replaced",
content: "content",
};
const files = await glob(`${sourceDirectory}/**/*`, {
nodir: true,
absolute: true,
});
await fileGenerator.generate(
sourceDirectory,
targetDirectory,
files,
variables,
);
assert.deepStrictEqual(
mkdir.mock.calls.map((call) => call.arguments),
[
["/target/directory", { recursive: true }],
["/target/directory", { recursive: true }],
["/target/directory/nested", { recursive: true }],
],
);
assert.deepStrictEqual(
readFile.mock.calls.map((call) => call.arguments),
[
[
fileURLToPath(new URL("__fixtures__/ejs.template", import.meta.url)),
{
encoding: "utf8",
},
],
],
);
assert.deepStrictEqual(
writeFile.mock.calls.map((call) => call.arguments),
[
[
"/target/directory/ejs",
"content",
{
encoding: "utf8",
},
],
],
);
assert.deepStrictEqual(
copyFile.mock.calls.map((call) => call.arguments),
[
[
fileURLToPath(new URL("__fixtures__/copy_as_is", import.meta.url)),
"/target/directory/copy_as_is",
],
[
fileURLToPath(
new URL("__fixtures__/nested/__variable__", import.meta.url),
),
"/target/directory/nested/replaced",
],
],
);
assert.deepStrictEqual(
render.mock.calls.map((call) => call.arguments),
[
[
"<%= content %>",
{
content: "content",
variable: "replaced",
},
{
sourceDirectory,
targetDirectory,
targetFilePath: "/target/directory/ejs",
templateFilePath: fileURLToPath(
new URL("__fixtures__/ejs.template", import.meta.url),
),
},
],
],
);
});