-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathfile_transformer_test.ts
More file actions
213 lines (188 loc) · 5.96 KB
/
file_transformer_test.ts
File metadata and controls
213 lines (188 loc) · 5.96 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
import { expect } from "@std/expect";
import {
FreshFileTransformer,
type ProcessedFile,
} from "./file_transformer.ts";
import { delay } from "../test_utils.ts";
import { stub } from "@std/testing/mock";
function stubDenoReadFile(content: string) {
return stub(Deno, "readFile", () => {
return Promise.resolve(
new TextEncoder().encode(content) as Uint8Array<ArrayBuffer>,
);
});
}
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 = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("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 = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("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 = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("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 = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("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 = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("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 = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("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 = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("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"] },
]);
},
);