-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
152 lines (133 loc) · 6.05 KB
/
test.js
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
import test from "ava";
import { glob } from "glob";
import path from "node:path";
import { readFileSync, promises as fs, createReadStream } from "node:fs";
const exists = path => fs.access(path).then(() => true).catch(() => false);
import { execa } from "execa";
import { Readable, Writable } from "node:stream";
const newWritableStream = () => new Writable({
write(chunk, encoding, callback) {
(this.chunks ?? (this.chunks = [])).push(Buffer.from(chunk));
callback();
},
final(callback) {
this.emit("end");
callback();
}
});
import { text as getStream } from "node:stream/consumers";
import { fileURLToPath } from "node:url";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const xmlPath = path.join(dirname, "test", "usage_example", "in.xml");
const cliPath = path.join(dirname, "cli.js"), cli = async (...options) =>
(await execa("node", [cliPath, xmlPath, ...options])).stdout;
import { withFile } from "tmp-promise";
const xml = readFileSync(xmlPath, "utf8");
import { default as minify, defaultOptions, minifyStream, defaultStreamOptions, minifyPipeline } from "./index.js";
const minifiedXml = minify(xml), minifiedStreamXml = minify(xml, defaultStreamOptions);
glob.sync("test/*/").forEach(dir => {
test(dir.substring("test/".length).replace(/[_\/]/g, " ").trim(), async t => {
const options = await (fs.readFile(path.join(dir, "options.json"), "utf8")
.then(JSON.parse).catch(() => {}));
// minify in.xml with options.json (or default options) and expect out.xml
t.is(minify(await fs.readFile(path.join(dir, "in.xml"), "utf8"), options),
await fs.readFile(path.join(dir, "out.xml"), "utf8"));
const streamOptions = await (fs.readFile(path.join(dir, "streamOptions.json"), "utf8")
.then(JSON.parse).catch(() => options));
// minify in.xml with streamOptions.json (or options.json / default options) as a stream and expect stream.xml
if (await exists(path.join(dir, "stream.xml"))) {
const stream = () => createReadStream(path.join(dir, "in.xml"), "utf8"),
expected = await fs.readFile(path.join(dir, "stream.xml"), "utf8");
t.is(await getStream(stream().pipe(minifyStream(streamOptions))), expected);
const writeStream = newWritableStream();
await minifyPipeline(stream(), writeStream, streamOptions);
t.is(Buffer.concat(writeStream.chunks ?? []).toString("utf8"), expected);
} else {
const expected = { message: /cannot be used with streams/ };
t.throws(() => minifyStream(streamOptions), expected);
await t.throwsAsync(async () => await minifyPipeline(undefined, undefined, streamOptions), expected)
}
});
});
/**
* CLI Tests
*/
const allOptions = Object.keys(defaultOptions);
const buildOptions = flag => allOptions.reduce((options, option) => {
options[option] = option === flag;
return options;
}, {});
const argumentForOption = (option, value) => `--${ value === false ? 'no-' : String() }${
option.replace(/([\p{Lowercase_Letter}\d])(\p{Uppercase_Letter})/gu, "$1-$2")
.toLowerCase().replace("c-data", "cdata").replace("doc-type", "doctype") }`;
const buildArguments = options => Object.entries(options).reduce((args, [option, value]) => {
args.push(argumentForOption(option, value));
typeof value === "string" && args.push(value);
return args;
}, []);
test("test cli help", async t => {
const {exitCode, stdout} = await execa("node", [cliPath], { reject: false });
t.is(exitCode, 2); t.regex(stdout, /\$ minify-xml <input>/);
// test if the help contains all arguments for all options
for (const argument of allOptions.map(option => argumentForOption(option))) {
t.regex(stdout, new RegExp(argument + "\\b"));
}
});
test("test cli unknown flags", async t => {
const {exitCode, stderr} = await execa("node", [cliPath, "--unknown-flag"], { reject: false });
t.not(exitCode, 0); t.regex(stderr, /Unknown flags?\s*--unknown-flag/);
});
test("test cli to stdout", async t => {
t.is(await cli(), minifiedXml);
});
test("test cli stream to stdout", async t => {
t.is(await cli("--stream"), minifiedStreamXml);
});
test("test cli in-place", t => withFile(async ({path: tmpPath}) => {
await fs.copyFile(xmlPath, tmpPath);
await execa("node", [cliPath, tmpPath, "--in-place"]);
t.is(await fs.readFile(tmpPath, "utf8"), minifiedXml);
}));
test("test cli to output", t => withFile(async ({path: tmpPath}) => {
await cli("--output", tmpPath);
t.is(await fs.readFile(tmpPath, "utf8"), minifiedXml);
}));
test("test cli stream to output", t => withFile(async ({path: tmpPath}) => {
await cli("--stream", "--output", tmpPath);
t.is(await fs.readFile(tmpPath, "utf8"), minifiedStreamXml);
}));
test("test cli debug", async t => {
t.true((await cli("--debug=ignore-cdata", "--debug=remove-comments")).includes(
/<!\s*(?:--(?:[^-]|-[^-])*--\s*)>/g.source));
});
for (const option of allOptions) {
test("test cli option " + argumentForOption(option), async t => {
const options = buildOptions(option);
t.is(await cli(...buildArguments(options)),
minify(xml, options));
});
}
test("test stream edge case", async t => {
t.is(await getStream(Readable.from(["<", "t", ">", "<", "/", "t", ">", "<", "t>", "</t>"])
.pipe(minifyStream({ streamMaxMatchLength: 4 }))), "<t></t><t/>");
});
/*
* README.md Tests
*/
test("test README.md lists all options", async t => {
const readme = await fs.readFile(path.join(dirname, "README.md"), "utf8");
// test if the readme contains all options
for (const option of allOptions) {
t.regex(readme, new RegExp("`" + option + "`"));
}
});
/*
* JSDoc Tests
*/
test("test JSDoc lists all options", async t => {
const source = await fs.readFile(path.join(dirname, "index.js"), "utf8");
// test if the JSDoc contains all options
for (const option of allOptions) {
t.regex(source, new RegExp(`@property {.+} ${option} .+`));
}
});