-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathfile_transformer_test.ts
More file actions
247 lines (218 loc) · 6.68 KB
/
file_transformer_test.ts
File metadata and controls
247 lines (218 loc) · 6.68 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { expect } from "@std/expect";
import type { FsAdapter } from "../fs.ts";
import { FileTransformer, type ProcessedFile } from "./file_transformer.ts";
import { delay } from "@fresh/internal/test-utils";
function testTransformer(files: Record<string, string>, root = "/") {
const mockFs: FsAdapter = {
cwd: () => root,
isDirectory: () => Promise.resolve(false),
mkdirp: () => Promise.resolve(),
walk: async function* foo() {
},
readFile: (file) => {
if (file instanceof URL) throw new Error("Not supported");
// deno-lint-ignore no-explicit-any
const content = (files as any)[file];
const buf = new TextEncoder().encode(content);
return Promise.resolve(buf);
},
readTextFile: (file) => {
if (file instanceof URL) throw new Error("Not supported");
// deno-lint-ignore no-explicit-any
const content = (files as any)[file];
return Promise.resolve(content);
},
};
return new FileTransformer(mockFs, root);
}
function consumeResult(result: ProcessedFile[]) {
const out: {
path: string;
content: string;
map: string | null;
inputFiles: string[];
}[] = [];
for (let i = 0; i < result.length; i++) {
const file = result[i];
out.push({
path: file.path,
content: typeof file.content === "string"
? file.content
: new TextDecoder().decode(file.content),
map: file.map !== null
? typeof file.map === "string"
? file.map
: new TextDecoder().decode(file.map)
: null,
inputFiles: file.inputFiles,
});
}
return out.sort((a, b) => a.path.localeCompare(b.path));
}
Deno.test("FileTransformer - transform sync", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
transformer.onTransform({ pluginName: "foo", filter: /.*/ }, (args) => {
return {
content: args.text + "bar",
};
});
const result = await transformer.process("foo.txt", "development", "");
const files = await consumeResult(result!);
expect(files).toEqual([
{ content: "foobar", map: null, path: "foo.txt", inputFiles: ["foo.txt"] },
]);
});
Deno.test("FileTransformer - transform async", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
transformer.onTransform({ pluginName: "foo", filter: /.*/ }, async (args) => {
await delay(1);
return {
content: args.text + "bar",
};
});
const result = await transformer.process("foo.txt", "development", "");
const files = await consumeResult(result!);
expect(files).toEqual([
{ content: "foobar", map: null, path: "foo.txt", inputFiles: ["foo.txt"] },
]);
});
Deno.test("FileTransformer - transform return Uint8Array", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
transformer.onTransform({ pluginName: "foo", filter: /.*/ }, () => {
return {
content: new TextEncoder().encode("foobar"),
};
});
const result = await transformer.process("foo.txt", "development", "");
const files = await consumeResult(result!);
expect(files).toEqual([
{ content: "foobar", map: null, path: "foo.txt", inputFiles: ["foo.txt"] },
]);
});
Deno.test("FileTransformer - pass transformed content", async () => {
const transformer = testTransformer({
"input.txt": "input",
});
transformer.onTransform({ pluginName: "A", filter: /.*/ }, (args) => {
return {
content: args.text + " -> A",
};
});
transformer.onTransform({ pluginName: "B", filter: /.*/ }, (args) => {
return {
content: args.text + " -> B",
};
});
const result = await transformer.process("input.txt", "development", "");
const files = await consumeResult(result!);
expect(files).toEqual([
{
content: "input -> A -> B",
map: null,
path: "input.txt",
inputFiles: ["input.txt"],
},
]);
});
Deno.test(
"FileTransformer - pass transformed content with multiple",
async () => {
const transformer = testTransformer({
"input.txt": "input",
});
transformer.onTransform({ pluginName: "A", filter: /.*/ }, (args) => {
return [{
path: args.path,
content: args.text + " -> A",
}];
});
transformer.onTransform({ pluginName: "B", filter: /.*/ }, (args) => {
return {
content: args.text + " -> B",
};
});
const result = await transformer.process("input.txt", "development", "");
const files = await consumeResult(result!);
expect(files).toEqual([
{
content: "input -> A -> B",
map: null,
path: "input.txt",
inputFiles: ["input.txt"],
},
]);
},
);
Deno.test("FileTransformer - return multiple results", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
const received: string[] = [];
transformer.onTransform({ pluginName: "A", filter: /foo\.txt$/ }, () => {
return [{
path: "a.txt",
content: "A",
}, {
path: "b.txt",
content: "B",
}];
});
transformer.onTransform({ pluginName: "B", filter: /.*/ }, (args) => {
received.push(args.path);
});
const result = await transformer.process("foo.txt", "development", "");
const files = await consumeResult(result!);
expect(files).toEqual([
{ content: "A", map: null, path: "a.txt", inputFiles: ["foo.txt"] },
{ content: "B", map: null, path: "b.txt", inputFiles: ["foo.txt"] },
]);
expect(received).toEqual(["foo.txt", "b.txt", "a.txt"]);
});
Deno.test(
"FileTransformer - track input files through temporary results",
async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
transformer.onTransform({ pluginName: "A", filter: /foo\.txt$/ }, () => {
return [{
path: "a.txt",
content: "A",
}, {
path: "b.txt",
content: "B",
}];
});
transformer.onTransform(
{ pluginName: "B", filter: /[ab]\.txt$/ },
(args) => {
return {
path: "c" + args.path,
content: args.text + "C",
};
},
);
const result = await transformer.process("foo.txt", "development", "");
const files = await consumeResult(result!);
expect(files).toEqual([
{ content: "AC", map: null, path: "ca.txt", inputFiles: ["foo.txt"] },
{ content: "BC", map: null, path: "cb.txt", inputFiles: ["foo.txt"] },
]);
},
);
Deno.test("FileTransformer - pass root to args", async () => {
const transformer = testTransformer({ "foo.txt": "foo" }, "/<root>/");
let root = "";
transformer.onTransform({ pluginName: "A", filter: /.*/ }, (args) => {
root = args.root;
return undefined;
});
await transformer.process("foo.txt", "development", "");
expect(root).toEqual("/<root>/");
});