-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathsymlink.test.js
247 lines (237 loc) · 6.31 KB
/
symlink.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
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
const path = require("path");
const fs = require("fs");
const { platform } = require("os");
const resolve = require("../");
const tempPath = path.join(__dirname, "temp");
describe("symlink", () => {
let isAdmin = true;
try {
fs.mkdirSync(tempPath);
fs.symlinkSync(
path.join(__dirname, "..", "lib", "index.js"),
path.join(tempPath, "test"),
"file"
);
fs.symlinkSync(
path.join(__dirname, "..", "lib"),
path.join(tempPath, "test2"),
"dir"
);
} catch (e) {
isAdmin = false;
}
try {
fs.unlinkSync(path.join(tempPath, "test"));
} catch (e) {
isAdmin = false;
}
try {
fs.unlinkSync(path.join(tempPath, "test2"));
} catch (e) {
isAdmin = false;
}
try {
fs.rmdirSync(tempPath);
} catch (e) {
isAdmin = false;
}
if (isAdmin) {
// PR #150: Detect Windows and preserve current working directory.
const isWindows = platform() === "win32";
const oldCWD = (isWindows && process.cwd()) || "";
beforeEach(() => {
// Create some cool symlinks
try {
fs.mkdirSync(tempPath);
fs.symlinkSync(
path.join(__dirname, "..", "lib", "index.js"),
path.join(tempPath, "index.js"),
"file"
);
fs.symlinkSync(
path.join(__dirname, "..", "lib"),
path.join(tempPath, "lib"),
"dir"
);
fs.symlinkSync(
path.join(__dirname, ".."),
path.join(tempPath, "this"),
"dir"
);
fs.symlinkSync(
path.join(tempPath, "this"),
path.join(tempPath, "that"),
"dir"
);
fs.symlinkSync(
path.join("..", "..", "lib", "index.js"),
path.join(tempPath, "node.relative.js"),
"file"
);
fs.symlinkSync(
path.join(".", "node.relative.js"),
path.join(tempPath, "node.relative.sym.js"),
"file"
);
// PR #150: Set the current working directory so that tests will fail if changes get reverted.
if (isWindows) {
process.chdir(path.join(tempPath, "that"));
}
} catch (e) {
// ignore the failure
}
});
afterEach(() => {
// PR #150: Restore the original working directory.
if (isWindows) {
process.chdir(oldCWD);
}
fs.unlinkSync(path.join(tempPath, "index.js"));
fs.unlinkSync(path.join(tempPath, "node.relative.js"));
fs.unlinkSync(path.join(tempPath, "node.relative.sym.js"));
fs.unlinkSync(path.join(tempPath, "lib"));
fs.unlinkSync(path.join(tempPath, "this"));
fs.unlinkSync(path.join(tempPath, "that"));
fs.rmdirSync(tempPath);
});
const resolveWithoutSymlinks = resolve.create({
symlinks: false
});
const resolveSyncWithoutSymlinks = resolve.create.sync({
symlinks: false
});
[
[tempPath, "./index.js", "with a symlink to a file"],
[tempPath, "./node.relative.js", "with a relative symlink to a file"],
[
tempPath,
"./node.relative.sym.js",
"with a relative symlink to a symlink to a file"
],
[tempPath, "./lib/index.js", "with a symlink to a directory 1"],
[tempPath, "./this/lib/index.js", "with a symlink to a directory 2"],
[
tempPath,
"./this/test/temp/index.js",
"with multiple symlinks in the path 1"
],
[
tempPath,
"./this/test/temp/lib/index.js",
"with multiple symlinks in the path 2"
],
[
tempPath,
"./this/test/temp/this/lib/index.js",
"with multiple symlinks in the path 3"
],
[
tempPath,
"./that/lib/index.js",
"with a symlink to a directory 2 (chained)"
],
[
tempPath,
"./that/test/temp/index.js",
"with multiple symlinks in the path 1 (chained)"
],
[
tempPath,
"./that/test/temp/lib/index.js",
"with multiple symlinks in the path 2 (chained)"
],
[
tempPath,
"./that/test/temp/that/lib/index.js",
"with multiple symlinks in the path 3 (chained)"
],
[
path.join(tempPath, "lib"),
"./index.js",
"with symlinked directory as context 1"
],
[
path.join(tempPath, "this"),
"./lib/index.js",
"with symlinked directory as context 2"
],
[
path.join(tempPath, "this"),
"./test/temp/lib/index.js",
"with symlinked directory as context and in path"
],
[
path.join(tempPath, "this", "lib"),
"./index.js",
"with symlinked directory in context path"
],
[
path.join(tempPath, "this", "test"),
"./temp/index.js",
"with symlinked directory in context path and symlinked file"
],
[
path.join(tempPath, "this", "test"),
"./temp/lib/index.js",
"with symlinked directory in context path and symlinked directory"
],
[
path.join(tempPath, "that"),
"./lib/index.js",
"with symlinked directory as context 2 (chained)"
],
[
path.join(tempPath, "that"),
"./test/temp/lib/index.js",
"with symlinked directory as context and in path (chained)"
],
[
path.join(tempPath, "that", "lib"),
"./index.js",
"with symlinked directory in context path (chained)"
],
[
path.join(tempPath, "that", "test"),
"./temp/index.js",
"with symlinked directory in context path and symlinked file (chained)"
],
[
path.join(tempPath, "that", "test"),
"./temp/lib/index.js",
"with symlinked directory in context path and symlinked directory (chained)"
]
].forEach(function (pathToIt) {
it("should resolve symlink to itself " + pathToIt[2], function (done) {
resolve(pathToIt[0], pathToIt[1], function (err, filename) {
if (err) return done(err);
expect(filename).toBeDefined();
expect(typeof filename).toBe("string");
expect(filename).toEqual(
path.join(__dirname, "..", "lib", "index.js")
);
resolveWithoutSymlinks(
pathToIt[0],
pathToIt[1],
function (err, filename) {
if (err) return done(err);
expect(typeof filename).toBe("string");
expect(filename).toEqual(path.resolve(pathToIt[0], pathToIt[1]));
done();
}
);
});
});
it("should resolve symlink to itself sync " + pathToIt[2], () => {
let filename = resolve.sync(pathToIt[0], pathToIt[1]);
expect(filename).toBeDefined();
expect(typeof filename).toBe("string");
expect(filename).toEqual(path.join(__dirname, "..", "lib", "index.js"));
filename = resolveSyncWithoutSymlinks(pathToIt[0], pathToIt[1]);
expect(typeof filename).toBe("string");
expect(filename).toEqual(path.resolve(pathToIt[0], pathToIt[1]));
});
});
} else {
it("cannot test symlinks because we have no permission to create them");
}
});