-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathfiles-test.ts
More file actions
53 lines (49 loc) · 1.68 KB
/
files-test.ts
File metadata and controls
53 lines (49 loc) · 1.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
import assert from "node:assert";
import {stat} from "node:fs/promises";
import {maybeStat, prepareOutput, visitFiles} from "../src/files.js";
describe("prepareOutput(path)", () => {
it("does nothing if passed the current directory", async () => {
assert.strictEqual(await prepareOutput("."), undefined);
});
});
describe("maybeStat(path)", () => {
it("returns stat if the file does not exist", async () => {
assert.deepStrictEqual(await maybeStat("README.md"), await stat("README.md"));
});
it("rethrows unexpected error", async () => {
const expected = new Error();
assert.rejects(
() =>
maybeStat({
toString(): string {
throw expected;
}
} as string),
expected
);
});
it("returns undefined if the file does not exist", async () => {
assert.strictEqual(await maybeStat("does/not/exist.txt"), undefined);
});
});
describe("visitFiles(root)", () => {
it("visits all files in a directory, return the relative path from the root", async () => {
assert.deepStrictEqual(await collect(visitFiles("test/input/build/files")), [
"custom-styles.css",
"file-top.csv",
"files.md",
"another/file-another.csv",
"subsection/additional-styles.css",
"subsection/file-sub.csv",
"subsection/subfiles.md"
]);
});
it("handles circular symlinks, visiting files only once", async () => {
assert.deepStrictEqual(await collect(visitFiles("test/input/circular-files")), ["a/a.txt", "b/b.txt"]);
});
});
async function collect<T>(generator: AsyncGenerator<T>): Promise<T[]> {
const values: T[] = [];
for await (const value of generator) values.push(value);
return values;
}