summaryrefslogtreecommitdiffstats
path: root/dom/system/tests/ioutils/test_ioutils_dir_iteration.html
blob: 93e2c60039e91084cf5d3644b1aa73783176e61c (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
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8">
  <title>Test the IOUtils file I/O API</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
  <script src="file_ioutils_test_fixtures.js"></script>
  <script>
    "use strict";

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

    add_task(async function iterate_dir_failure() {
      let notExists = PathUtils.join(PathUtils.tempDir, 'does_not_exist_dir.tmp.d');

      await Assert.rejects(
        IOUtils.getChildren(notExists),
        /Could not get children of file\(.*\) because it does not exist/,
        "IOUtils::getChildren rejects if the file does not exist"
      );
      ok(!await fileExists(notExists), `Expected ${notExists} not to exist`);

      info('Try to get the children of a regular file');

      let tmpFileName = PathUtils.join(PathUtils.tempDir, 'iterator_file.tmp');
      await createFile(tmpFileName)
      await Assert.rejects(IOUtils.getChildren(tmpFileName),
        /Could not get children of file\(.*\) because it is not a directory/,
        "IOUtils::getChildren rejects if the file is not a dir"
      );

      await cleanup(tmpFileName);
    });

    add_task(async function iterate_dir() {
      info('Try to get the children of a multi-level directory hierarchy');

      let root = PathUtils.join(PathUtils.tempDir, 'iterator.tmp.d');
      let child1 = PathUtils.join(root, 'child1.tmp');
      let child2 = PathUtils.join(root, 'child2.tmp');
      let grandchild = PathUtils.join(child1, 'grandchild.tmp');

      await createDir(grandchild); // Ancestors will be created.
      await createDir(child2);

      let entries = await IOUtils.getChildren(root);

      is(entries.length, 2, `Expected 2 entries below the path at ${root}`);
      ok(!entries.includes(grandchild), "IOUtils::getChildren does not enter subdirectories");

      await cleanup(root);
    });

    add_task(async function iterate_empty_dir() {
      info('Try to get the children of an empty directory');

      let emptyDir = PathUtils.join(PathUtils.tempDir, 'iterator_empty_dir.tmp.d');
      await createDir(emptyDir);

      is(
        (await IOUtils.getChildren(emptyDir)).length,
        0,
        "IOUtils::getChildren return an empty array when called on an empty dir"
      );

      await cleanup(emptyDir);
    });

    add_task(async function iterate_ignore_missing_dir() {
      info("Try to get the children of a missing file with ignoreAbsent");

      const notExists = PathUtils.join(PathUtils.tempDir, "does_not_exist_dir.tmp.d");

      is(
        (await IOUtils.getChildren(notExists, { ignoreAbsent: true })).length,
        0,
        "IOUtils::getChildren returns an empty array when called with ignoreAbsent on a missing file"
      );
      ok(!await fileExists(notExists), `Expected ${notExists} not to exist`);
    });
  </script>
</head>

<body>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test"></pre>
</body>

</html>