-
Notifications
You must be signed in to change notification settings - Fork 678
/
fs.test.ts
173 lines (156 loc) · 5.93 KB
/
fs.test.ts
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
import * as fs from "fs";
import { createServer } from "net";
import { Project, tempdirProject } from "@typestrong/fs-fixture-builder";
import { AssertionError, deepStrictEqual as equal } from "assert";
import { basename, dirname, resolve, normalize } from "path";
import { getCommonDirectory, glob } from "../../lib/utils/fs";
describe("fs.ts", () => {
let fix: Project;
beforeEach(() => {
fix = tempdirProject();
});
afterEach(() => {
fix.rm();
});
describe("getCommonDirectory", () => {
it("Returns the empty string if no files are provided", () => {
equal(getCommonDirectory([]), "");
});
it("Returns the dirname if only one file is provided", () => {
equal(getCommonDirectory(["a/b/c.ts"]), "a/b");
});
it("Handles duplicates paths appropriately", () => {
const p = "a/b/c";
equal(getCommonDirectory([p, p]), p);
});
it("Gets the path common to all files", () => {
equal(
getCommonDirectory(["/a/b/c", "/a/b/c/d/e", "/a/b/d"]),
"/a/b"
);
});
});
describe("glob", () => {
it("handles root match", () => {
fix.write();
const result = glob(fix.cwd, fix.cwd, { includeDirectories: true });
equal(result.map(normalize), [fix.cwd].map(normalize));
});
it("Handles basic globbing", () => {
fix.addFile("test.ts");
fix.addFile("test2.ts");
fix.addFile("a.ts");
fix.addFile("b.js");
fix.write();
equal(
glob(`${fix.cwd}/*.ts`, fix.cwd).map((f) => basename(f)),
["a.ts", "test.ts", "test2.ts"]
);
equal(
glob(`**/test*.ts`, fix.cwd).map((f) => basename(f)),
["test.ts", "test2.ts"]
);
});
describe("when 'followSymlinks' option is true", () => {
it("should navigate symlinked directories", () => {
const target = dirname(fix.dir("a").addFile("test.ts").path);
fix.write();
fs.symlinkSync(target, resolve(fix.cwd, "b"), "junction");
equal(
glob(`${fix.cwd}/b/*.ts`, fix.cwd, {
followSymlinks: true,
}).map((f) => basename(f)),
["test.ts"]
);
});
it("should navigate recursive symlinked directories only once", () => {
fix.addFile("test.ts");
fix.write();
fs.symlinkSync(
fix.cwd,
resolve(fix.cwd, "recursive"),
"junction"
);
equal(
glob(`${fix.cwd}/**/*.ts`, fix.cwd, {
followSymlinks: true,
}).map((f) => basename(f)),
["test.ts", "test.ts"]
);
});
it("should handle symlinked files", function () {
const { path } = fix.addFile("test.ts");
fix.write();
try {
fs.symlinkSync(
path,
resolve(dirname(path), "test-2.ts"),
"file"
);
} catch (err) {
// on windows, you need elevated permissions to create a file symlink.
// maybe we have them! maybe we don't!
if (
(err as NodeJS.ErrnoException).code === "EPERM" &&
process.platform === "win32"
) {
return this.skip();
}
}
equal(
glob(`${fix.cwd}/**/*.ts`, fix.cwd, {
followSymlinks: true,
}).map((f) => basename(f)),
["test-2.ts", "test.ts"]
);
});
});
describe("when node_modules is present in the pattern", function () {
it("should traverse node_modules", function () {
fix.dir("node_modules").addFile("test.ts").path;
fix.write();
equal(
glob(`${fix.cwd}/node_modules/test.ts`, fix.cwd).map((f) =>
basename(f)
),
["test.ts"]
);
});
});
describe("when node_modules is not present in the pattern", function () {
it("should not traverse node_modules", function () {
fix.dir("node_modules").addFile("test.ts").path;
fix.write();
equal(
glob(`${fix.cwd}/**/test.ts`, fix.cwd).map((f) =>
basename(f)
),
[]
);
});
});
it("should ignore anything that is not a file, symbolic link, or directory", function (done) {
// Use unix socket for example, because that's easiest to create.
// Skip on Windows because it doesn't support unix sockets
if (process.platform === "win32") {
return this.skip();
}
fix.write();
const sockServer = createServer()
.unref()
.listen(resolve(fix.cwd, "socket.sock"))
.once("listening", () => {
let err: AssertionError | null = null;
try {
equal(glob(`${fix.cwd}/*.sock`, fix.cwd), []);
} catch (e) {
err = e as AssertionError;
} finally {
sockServer.close(() => {
done(err);
});
}
});
});
});
});