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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PathUtils tests</title>
</head>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/WorkerHandler.js"></script>
<link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script>
"use strict";
const { Assert } = ChromeUtils.importESModule(
"resource://testing-common/Assert.sys.mjs"
);
const UNRECOGNIZED_PATH = /Could not initialize path: NS_ERROR_FILE_UNRECOGNIZED_PATH/;
const SPLIT_RELATIVE_ABSOLUTE = /PathUtils.splitRelative requires a relative path/;
const SPLIT_RELATIVE_EMPTY = /PathUtils.splitRelative: Empty directory components \(""\) not allowed by options/;
const SPLIT_RELATIVE_PARENT = /PathUtils.splitRelative: Parent directory components \("\.\."\) not allowed by options/;
const SPLIT_RELATIVE_CURRENT = /PathUtils.splitRelative: Current directory components \("\."\) not allowed by options/;
const EMPTY_PATH = /PathUtils does not support empty paths/;
const JOIN = /Could not append to path/;
add_task(function test_filename() {
Assert.throws(
() => PathUtils.filename(""),
EMPTY_PATH,
"PathUtils.filename() does not support empty paths"
);
Assert.throws(
() => PathUtils.filename("foo.txt"),
UNRECOGNIZED_PATH,
"PathUtils.filename() does not support relative paths"
);
if (Services.appinfo.OS === "WINNT") {
is(
PathUtils.filename("C:"),
"C:",
"PathUtils.filename() with a drive path"
);
is(
PathUtils.filename("C:\\"),
"C:",
"PathUtils.filename() with a drive path"
);
is(
PathUtils.filename("C:\\Windows"),
"Windows",
"PathUtils.filename() with a path with 2 components"
);
is(
PathUtils.filename("C:\\Windows\\"),
"Windows",
"PathUtils.filename() with a path with 2 components and a trailing slash"
);
is(
PathUtils.filename("C:\\Windows\\System32"),
"System32",
"PathUtils.filename() with a path with 3 components"
);
is(
PathUtils.filename("\\\\server"),
"\\\\server",
"PathUtils.filename() with a UNC server path"
);
is(
PathUtils.filename("C:\\file.dat"),
"file.dat",
"PathUtils.filename() with a file path"
);
} else {
is(
PathUtils.filename("/"),
"/",
"PathUtils.filename() with a root path"
);
is(
PathUtils.filename("/usr/"),
"usr",
"PathUtils.filename() with a non-root path"
);
is(
PathUtils.filename("/usr/lib/libfoo.so"),
"libfoo.so",
"PathUtils.filename() with a path with 3 components"
);
}
});
add_task(function test_parent() {
Assert.throws(
() => PathUtils.parent("."),
UNRECOGNIZED_PATH,
"PathUtils.parent() does not support relative paths"
);
Assert.throws(
() => PathUtils.parent(""),
EMPTY_PATH,
"PathUtils.parent() does not support empty paths"
);
if (Services.appinfo.OS === "WINNT") {
is(
PathUtils.parent("C:"),
null,
"PathUtils.parent() with a drive path"
);
is(
PathUtils.parent("\\\\server"),
null,
"PathUtils.parent() with a UNC server path"
);
is(
PathUtils.parent("\\\\server\\foo"),
"\\\\server",
"PathUtils.parent() with a UNC server path and child component"
);
Assert.throws(
() => PathUtils.parent("C:", -1),
/^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/,
"PathUtils.parent() with a negative depth throws"
);
Assert.throws(
() => PathUtils.parent("C:", 0),
/^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/,
"PathUtils.parent() with a zero depth throws"
);
{
const path = "C:\\Users\\User\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\foo.default";
const expected = [
"C:\\Users\\User\\AppData\\Local\\Mozilla\\Firefox\\Profiles",
"C:\\Users\\User\\AppData\\Local\\Mozilla\\Firefox",
"C:\\Users\\User\\AppData\\Local\\Mozilla",
"C:\\Users\\User\\AppData\\Local",
"C:\\Users\\User\\AppData",
"C:\\Users\\User",
"C:\\Users",
"C:",
null,
];
for (const [i, parent] of expected.entries()) {
is(PathUtils.parent(path, i + 1), parent, `PathUtils.parent() with depth=${i + 1}`)
}
}
} else {
is(
PathUtils.parent("/"),
null,
"PathUtils.parent() with a root path"
);
is(
PathUtils.parent("/var"),
"/",
"PathUtils.parent() with a 2 component path"
);
is(
PathUtils.parent("/var/run"),
"/var",
"PathUtils.parent() with a 3 component path"
);
Assert.throws(
() => PathUtils.parent("/", -1),
/^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/,
"PathUtils.parent() with a negative depth throws"
);
Assert.throws(
() => PathUtils.parent("/", 0),
/^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/,
"PathUtils.parent() with a zero depth throws"
);
{
const path = "/home/user/.mozilla/firefox/foo.default";
const expected = [
"/home/user/.mozilla/firefox",
"/home/user/.mozilla",
"/home/user",
"/home",
"/",
null,
];
for (const [i, parent] of expected.entries()) {
is(
PathUtils.parent(path, i + 1),
parent,
`PathUtils.parent() with depth=${i + 1}`
);
}
}
}
});
add_task(function test_join() {
Assert.throws(
() => PathUtils.join(),
EMPTY_PATH,
"PathUtils.join() does not support empty paths"
);
Assert.throws(
() => PathUtils.join(""),
EMPTY_PATH,
"PathUtils.join() does not support empty paths"
);
Assert.throws(
() => PathUtils.join("foo", "bar"),
UNRECOGNIZED_PATH,
"PathUtils.join() does not support relative paths"
);
Assert.throws(
() => PathUtils.join("."),
UNRECOGNIZED_PATH,
"PathUtils.join() does not support relative paths"
);
if (Services.appinfo.OS === "WINNT") {
is(
PathUtils.join("C:"),
"C:",
"PathUtils.join() with a single path"
);
is(
PathUtils.join("C:\\Windows", "System32"),
"C:\\Windows\\System32",
"PathUtils.join() with a 2 component path and an additional component"
);
is(
PathUtils.join("C:", "Users", "Example"),
"C:\\Users\\Example",
"PathUtils.join() with a root path and two additional components"
);
is(
PathUtils.join("\\\\server", "Files", "Example.dat"),
"\\\\server\\Files\\Example.dat",
"PathUtils.join() with a server path"
);
} else {
is(
PathUtils.join("/"),
"/",
"PathUtils.join() with a root path"
);
is(
PathUtils.join("/usr", "lib"),
"/usr/lib",
"PathUtils.join() with a 2 component path and an additional component"
);
is(
PathUtils.join("/", "home", "example"),
"/home/example",
"PathUtils.join() with a root path and two additional components"
);
}
});
add_task(function test_join_relative() {
if (Services.appinfo.OS === "WINNT") {
is(
PathUtils.joinRelative("C:", ""),
"C:",
"PathUtils.joinRelative() with an empty relative path"
);
is(
PathUtils.joinRelative("C:", "foo\\bar\\baz"),
"C:\\foo\\bar\\baz",
"PathUtils.joinRelative() with a relative path containing path separators"
);
} else {
is(
PathUtils.joinRelative("/", ""),
"/",
"PathUtils.joinRelative() with an empty relative path"
);
is(
PathUtils.joinRelative("/", "foo/bar/baz"),
"/foo/bar/baz",
"PathUtils.joinRelative() with a relative path containing path separators"
);
}
});
add_task(async function test_normalize() {
Assert.throws(
() => PathUtils.normalize(""),
EMPTY_PATH,
"PathUtils.normalize() does not support empty paths"
);
Assert.throws(
() => PathUtils.normalize("."),
UNRECOGNIZED_PATH,
"PathUtils.normalize() does not support relative paths"
);
if (Services.appinfo.OS === "WINNT") {
is(
PathUtils.normalize("C:\\\\Windows\\\\..\\\\\\.\\Users\\..\\Windows"),
"C:\\Windows",
"PathUtils.normalize() with a non-normalized path"
);
} else {
// nsLocalFileUnix::Normalize() calls realpath, which resolves symlinks
// and requires the file to exist.
//
// On Darwin, the temp directory is located in `/private/var`, which is a
// symlink to `/var`, so we need to pre-normalize our temporary directory
// or expected paths won't match.
const tmpDir = PathUtils.join(
PathUtils.normalize(PathUtils.tempDir),
"pathutils_test"
);
await IOUtils.makeDirectory(tmpDir, { ignoreExisting: true });
info(`created tmpDir ${tmpDir}`);
SimpleTest.registerCleanupFunction(async () => {
await IOUtils.remove(tmpDir, {
recursive: true,
});
});
await IOUtils.makeDirectory(PathUtils.join(tmpDir, "foo", "bar"), {
createAncestors: true,
});
is(
PathUtils.normalize("/"),
"/",
"PathUtils.normalize() with a normalized path"
);
is(
PathUtils.normalize(
PathUtils.join(
tmpDir,
"foo",
".",
"bar",
".",
)
),
PathUtils.join(tmpDir, "foo", "bar"),
"PathUtils.normalize() with a non-normalized path"
);
}
});
add_task(function test_split() {
Assert.throws(
() => PathUtils.split("foo"),
UNRECOGNIZED_PATH,
"PathUtils.split() does not support relative paths"
);
Assert.throws(
() => PathUtils.split(""),
EMPTY_PATH,
"PathUtils.split() does not support empty paths"
);
if (Services.appinfo.OS === "WINNT") {
Assert.deepEqual(
PathUtils.split("C:\\Users\\Example"),
["C:", "Users", "Example"],
"PathUtils.split() on an absolute path"
);
Assert.deepEqual(
PathUtils.split("C:\\Users\\Example\\"),
["C:", "Users", "Example"],
"PathUtils.split() on an absolute path with a trailing slash"
);
Assert.deepEqual(
PathUtils.split("\\\\server\\Files\\Example.dat"),
["\\\\server", "Files", "Example.dat"],
"PathUtils.split() with a server as the root"
);
} else {
Assert.deepEqual(
PathUtils.split("/home/foo"),
["/", "home", "foo"],
"PathUtils.split() on absolute path"
);
Assert.deepEqual(
PathUtils.split("/home/foo/"),
["/", "home", "foo"],
"PathUtils.split() on absolute path with trailing slash"
);
}
});
add_task(function test_splitRelative() {
Assert.throws(
() => PathUtils.splitRelative(""),
EMPTY_PATH,
"PathUtils.splitRelative() should throw with empty path"
);
if (Services.appinfo.OS === "WINNT") {
Assert.throws(
() => PathUtils.splitRelative("C:\\"),
SPLIT_RELATIVE_ABSOLUTE,
"PathUtils.splitRelative() should throw with a drive path"
);
Assert.throws(
() => PathUtils.splitRelative("\\\\server\\share\\"),
SPLIT_RELATIVE_ABSOLUTE,
"PathUtils.splitRelative() should throw with a UNC path"
);
Assert.throws(
() => PathUtils.splitRelative("foo\\\\bar"),
SPLIT_RELATIVE_EMPTY,
"PathUtils.splitRelative() should throw with empty component by default"
);
Assert.deepEqual(
PathUtils.splitRelative("foo\\\\bar", { allowEmpty: true }),
["foo", "", "bar"],
"PathUtils.splitRelative() with an empty component is allowed with option"
);
Assert.deepEqual(
PathUtils.splitRelative("foo"),
["foo"],
"PathUtils.splitRelative() on a relative path with one component"
);
Assert.deepEqual(
PathUtils.splitRelative("foo\\"),
["foo"],
"PathUtils.splitRelative() on a relative path with one component and a trailing slash"
);
Assert.deepEqual(
PathUtils.splitRelative("foo\\bar"),
["foo", "bar"],
"PathUtils.splitRelative() on a relative path with two components"
);
} else {
Assert.throws(
() => PathUtils.splitRelative("/foo/bar"),
SPLIT_RELATIVE_ABSOLUTE,
"PathUtils.splitRelative() should throw with an absolute path"
);
Assert.throws(
() => PathUtils.splitRelative("foo//bar"),
SPLIT_RELATIVE_EMPTY,
"PathUtils.splitRelative() should throw with empty component by default"
);
Assert.deepEqual(
PathUtils.splitRelative("foo//bar", { allowEmpty: true }),
["foo", "", "bar"],
"PathUtils.splitRelative() with an empty component is allowed with option"
);
Assert.deepEqual(
PathUtils.splitRelative("foo"),
["foo"],
"PathUtils.splitRelative() on a relative path with one component"
);
Assert.deepEqual(
PathUtils.splitRelative("foo/"),
["foo"],
"PathUtils.splitRelative() on a relative path with one component and a trailing slash"
);
Assert.deepEqual(
PathUtils.splitRelative("foo/bar"),
["foo", "bar"],
"PathUtils.splitRelative() on a relative path with two components",
);
}
Assert.throws(
() => PathUtils.splitRelative("."),
SPLIT_RELATIVE_CURRENT,
"PathUtils.splitRelative() with a current dir component is disallowed by default"
);
Assert.throws(
() => PathUtils.splitRelative(".."),
SPLIT_RELATIVE_PARENT,
"PathUtils.splitRelative() with a parent dir component is disallowed by default"
);
Assert.deepEqual(
PathUtils.splitRelative(".", { allowCurrentDir: true }),
["."],
"PathUtils.splitRelative() with a current dir component is allowed with option"
);
Assert.deepEqual(
PathUtils.splitRelative("..", { allowParentDir: true }),
[".."],
"PathUtils.splitRelative() with a parent dir component is allowed with option"
);
});
add_task(function test_toFileURI() {
Assert.throws(
() => PathUtils.toFileURI("."),
UNRECOGNIZED_PATH,
"PathUtils.toFileURI() does not support relative paths"
);
Assert.throws(
() => PathUtils.toFileURI(""),
EMPTY_PATH,
"PathUtils.toFileURI() does not support empty paths"
);
if (Services.appinfo.OS === "WINNT") {
is(
PathUtils.toFileURI("C:\\"),
"file:///C:",
"PathUtils.toFileURI() with a root path"
);
is(
PathUtils.toFileURI("C:\\Windows\\"),
"file:///C:/Windows",
"PathUtils.toFileURI() with a non-root directory path"
);
is(
PathUtils.toFileURI("C:\\Windows\\system32\\notepad.exe"),
"file:///C:/Windows/system32/notepad.exe",
"PathUtils.toFileURI() with a file path"
);
} else {
is(
PathUtils.toFileURI("/"),
"file:///",
"PathUtils.toFileURI() with a root path"
);
is(
PathUtils.toFileURI("/bin"),
"file:///bin",
"PathUtils.toFileURI() with a non-root directory path"
);
is(
PathUtils.toFileURI("/bin/ls"),
"file:///bin/ls",
"PathUtils.toFileURI() with a file path"
);
}
});
add_task(async function test_isAbsolute() {
if (Services.appinfo.OS === "WINNT") {
ok(PathUtils.isAbsolute("C:"), "Drive paths are absolute paths on Windows");
ok(PathUtils.isAbsolute("C:\\Windows"), "Paths from the root are absolute paths on Windows");
ok(!PathUtils.isAbsolute("foo"), "Paths containing a single item are not absolute paths on Windows");
ok(!PathUtils.isAbsolute(".\\foo"), "Paths relative to the current working directory are not absolute paths on Windows");
ok(!PathUtils.isAbsolute("..\\foo"), "Paths relative to the parent directory are not absolute paths on Windows");
} else {
ok(PathUtils.isAbsolute("/"), "Root paths are absolute paths");
ok(PathUtils.isAbsolute("/home"), "Paths with a root stem are absolute paths");
ok(!PathUtils.isAbsolute("foo"), "Paths containing a single non-root item are not absolute paths");
ok(!PathUtils.isAbsolute("./foo"), "Paths relative to the current working directory are not absolute paths");
ok(!PathUtils.isAbsolute("../foo"), "Paths relative to the parent directory are not absolute paths");
}
});
add_task(async function test_getDirectories() {
// See: nsAppDirectoryServiceDefs.h
const tests = [
["profileDir", "ProfD"],
["localProfileDir", "ProfLD"],
["tempDir", "TmpD"],
];
for (const [attrName, dirConstant] of tests) {
const expected = Services.dirsvc.get(dirConstant, Ci.nsIFile).path;
const attrValue = PathUtils[attrName];
is(attrValue, expected, `PathUtils.${attrName} == Services.dirsvc.get("${dirConstant}", Ci.nsIFile).path`);
}
});
</script>
<body>
</body>
</html>
|