summaryrefslogtreecommitdiffstats
path: root/dom/system/tests/ioutils/test_ioutils_getfile.html
blob: 077f0b5c1cd5f2a776c78d155a7dce8365e1ad5d (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
<!-- 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>
    "use strict";

    const TEST_PATH = PathUtils.join(PathUtils.tempDir, "test-ioutils-getfile");

    add_task(async function test_getFile() {
      const expectedPath = PathUtils.join(TEST_PATH, "foo", "bar", "baz", "get-file.txt");
      const parentPath = PathUtils.parent(expectedPath);

      ok(!(await IOUtils.exists(parentPath)), "Parent directory should not exist");

      const file = await IOUtils.getFile(TEST_PATH, "foo", "bar", "baz", "get-file.txt");
      const path = file.path;

      is(path, expectedPath, "Should have the correct path");
      ok(await IOUtils.exists(parentPath), "Parent directory should be created");
      ok(!(await IOUtils.exists(path)), "File should not be created");

      await IOUtils.remove(TEST_PATH, { recursive: true });
    });

    add_task(async function test_getFile_exists() {
      const expectedPath = PathUtils.join(TEST_PATH, "foo", "bar", "baz", "get-file-exists.txt");

      await IOUtils.makeDirectory(PathUtils.parent(expectedPath));
      await IOUtils.writeUTF8(expectedPath, "hello world");

      const file = await IOUtils.getFile(TEST_PATH, "foo", "bar", "baz", "get-file-exists.txt");
      is(file.path, expectedPath, "Should have the correct path");
      is(await IOUtils.readUTF8(file.path), "hello world", "Contents should be unchanged");

      await IOUtils.remove(TEST_PATH, { recursive: true });
    });

    add_task(async function test_getDirectory() {
      const expectedPath = PathUtils.join(TEST_PATH, "qux", "quux", "corge");

      ok(!(await IOUtils.exists(PathUtils.parent(expectedPath))), "Parent directory should not exist");

      const file = await IOUtils.getDirectory(TEST_PATH, "qux", "quux", "corge");

      is(file.path, expectedPath, "Should have the correct path");
      ok(await IOUtils.exists(expectedPath), "Directory should be created");

      const info = await IOUtils.stat(expectedPath);
      is(info.type, "directory", "Should create a directory");

      await IOUtils.remove(TEST_PATH, { recursive: true });
    });

    add_task(async function test_getDirectory_exists() {
      const expectedPath = PathUtils.join(TEST_PATH, "qux", "quux", "corge");

      await IOUtils.makeDirectory(expectedPath);

      const file = await IOUtils.getDirectory(TEST_PATH, "qux", "quux", "corge");
      is(file.path, expectedPath, "Should have the correct path");
      ok(await IOUtils.exists(expectedPath), "Directory should still exist");

      const info = await IOUtils.stat(expectedPath);
      is(info.type, "directory", "Should still be a directory");

      await IOUtils.remove(TEST_PATH, { recursive: true });
    });
  </script>
</head>

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

</html>