summaryrefslogtreecommitdiffstats
path: root/dom/system/tests/test_pathutils.html
blob: 555942e13d3405c91dfe82ff35acb2951f47ceb5 (plain)
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
<!-- 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>
<link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script>
  "use strict";

  const { AppConstants } = ChromeUtils.import(
    "resource://gre/modules/AppConstants.jsm"
  );
  const { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm");
  const { Services } = ChromeUtils.import(
    "resource://gre/modules/Services.jsm"
  );

  const UNRECOGNIZED_PATH = /Could not initialize path: NS_ERROR_FILE_UNRECOGNIZED_PATH/;
  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"
      );
    } 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"
      );
    }
  });

  add_task(function test_join() {
    is(
      PathUtils.join(),
      "",
      "PathUtils.join() with an empty sequence"
    );
    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(await PathUtils.getTempDir()),
        "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",
            ".",
            "..",
            "foo",
            ".",
            "bar",
            "..",
            "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_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_getDirectories() {
    const profile = await PathUtils.getProfileDir();
    is(
      profile,
      Services.dirsvc.get("ProfD", Ci.nsIFile).path,
      "PathUtils.getProfileDir() should match dirsvc"
    );

    const localProfile = await PathUtils.getLocalProfileDir();
    is(
      localProfile,
      Services.dirsvc.get("ProfLD", Ci.nsIFile).path,
      "PathUtils.getLocalProfileDir() should match dirsvc"
    );

    // See: nsAppDirectoryServiceDefs.h
    const tempDir = await PathUtils.getTempDir();
    if (AppConstants.MOZ_SANDBOX) {
      is(
        tempDir,
        Services.dirsvc.get("ContentTmpD", Ci.nsIFile).path,
        "PathUtils.getTempDir() should match dirsvc"
      );
    } else {
      is(
        tempDir,
        Services.dirsvc.get("TmpD", Ci.nsIFile).path,
        "PathUtils.getTempDir() should match dirsvc"
      );
    }
  });

  add_task(async function test_createUniquePath() {
    let path = PathUtils.join(await PathUtils.getProfileDir(), ".test");

    let firstPath = PathUtils.createUniquePath(path);
    let secondPath = PathUtils.createUniquePath(path);
    SimpleTest.registerCleanupFunction(async () => {
      await IOUtils.remove(firstPath);
      await IOUtils.remove(secondPath);
    });

    isnot(firstPath, secondPath, "Create unique paths returns different paths");
    is(PathUtils.filename(firstPath), ".test", "PathUtils.createUniquePath() matches filename for first path");
    is(PathUtils.filename(secondPath), "-1.test", "PathUtils.createUniquePath() has unique filename for second path");
  });
</script>

<body>
</body>

</html>