summaryrefslogtreecommitdiffstats
path: root/dom/system/tests/ioutils/test_ioutils_copy_move.html
blob: ba55eb84632e40ded921568c99a854d913b4d1b6 (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
<!-- 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");
    const { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");

    add_task(async function test_move_relative_path() {
      const tmpFileName = PathUtils.join(PathUtils.tempDir, "test_ioutils_move_relative_path.tmp");
      const dest = "relative_to_cwd.tmp";
      await createFile(tmpFileName, "source");

      info("Test moving a file to a relative destination");
      await Assert.rejects(
        IOUtils.move(tmpFileName, dest),
        /Could not parse path/,
        "IOUtils::move only works with absolute paths"
      );
      ok(
        await fileHasTextContents(tmpFileName, "source"),
        "IOUtils::move doesn't change source file when move fails"
      );

      await cleanup(tmpFileName);
    });

    add_task(async function test_move_rename() {
      // Set up.
      const tmpFileName = PathUtils.join(PathUtils.tempDir, "test_ioutils_move_src.tmp");
      const destFileName = PathUtils.join(PathUtils.tempDir, "test_ioutils_move_dest.tmp");
      await createFile(tmpFileName, "dest");
      // Test.
      info("Test move to new file in same directory");
      await IOUtils.move(tmpFileName, destFileName);
      info(`Moved ${tmpFileName} to ${destFileName}`);
      ok(
        !await fileExists(tmpFileName)
        && await fileHasTextContents(destFileName, "dest"),
        "IOUtils::move can move source to dest in same directory"
      )

      // Set up.
      info("Test move to existing file with no overwrite");
      await createFile(tmpFileName, "source");
      // Test.
      await Assert.rejects(
        IOUtils.move(tmpFileName, destFileName, { noOverwrite: true }),
        /Could not move source file\(.*\) to destination\(.*\) because the destination already exists and overwrites are not allowed/,
        "IOUtils::move will refuse to move a file if overwrites are disabled"
      );
      ok(
        await fileExists(tmpFileName)
        && await fileHasTextContents(destFileName, "dest"),
        "Failed IOUtils::move doesn't move the source file"
      );

      // Test.
      info("Test move to existing file with overwrite");
      await IOUtils.move(tmpFileName, destFileName, { noOverwrite: false });
      ok(!await fileExists(tmpFileName), "IOUtils::move moved source");
      ok(
        await fileHasTextContents(destFileName, "source"),
        "IOUtils::move overwrote the destination with the source"
      );

      // Clean up.
      await cleanup(tmpFileName, destFileName);
    });

    add_task(async function test_move_to_dir() {
      // Set up.
      info("Test move and rename to non-existing directory");
      const tmpFileName = PathUtils.join(PathUtils.tempDir, "test_move_to_dir.tmp");
      const destDir = PathUtils.join(PathUtils.tempDir, "test_move_to_dir.tmp.d");
      const dest = PathUtils.join(destDir, "dest.tmp");
      await createFile(tmpFileName);
      // Test.
      ok(!await IOUtils.exists(destDir), "Expected path not to exist");
      await IOUtils.move(tmpFileName, dest);
      ok(
        !await fileExists(tmpFileName) && await fileExists(dest),
        "IOUtils::move creates non-existing parents if needed"
      );

      // Set up.
      info("Test move and rename to existing directory.")
      await createFile(tmpFileName);
      // Test.
      ok(await dirExists(destDir), `Expected ${destDir} to be a directory`);
      await IOUtils.move(tmpFileName, dest);
      ok(
        !await fileExists(tmpFileName)
        && await fileExists(dest),
        "IOUtils::move can move/rename a file into an existing dir"
      );

      // Set up.
      info("Test move to existing directory without specifying leaf name.")
      await createFile(tmpFileName);
      // Test.
      await IOUtils.move(tmpFileName, destDir);
      ok(await dirExists(destDir), `Expected ${destDir} to be a directory`);
      ok(
        !await fileExists(tmpFileName)
        && await fileExists(PathUtils.join(destDir, PathUtils.filename(tmpFileName))),
        "IOUtils::move can move a file into an existing dir"
      );

      // Clean up.
      await cleanup(destDir);
    });

    add_task(async function test_move_dir() {
      // Set up.
      info("Test rename an empty directory");
      const srcDir = PathUtils.join(PathUtils.tempDir, "test_move_dir.tmp.d");
      const destDir = PathUtils.join(PathUtils.tempDir, "test_move_dir_dest.tmp.d");
      await createDir(srcDir);
      // Test.
      await IOUtils.move(srcDir, destDir);
      ok(
        !await IOUtils.exists(srcDir) && await dirExists(destDir),
        "IOUtils::move can rename directories"
      );

      // Set up.
      info("Test move directory and its content into another directory");
      await createDir(srcDir);
      await createFile(PathUtils.join(srcDir, "file.tmp"), "foo");
      // Test.
      await IOUtils.move(srcDir, destDir);
      const destFile = PathUtils.join(destDir, PathUtils.filename(srcDir), "file.tmp");
      ok(
        !await IOUtils.exists(srcDir)
        && await dirExists(destDir)
        && await dirExists(PathUtils.join(destDir, PathUtils.filename(srcDir)))
        && await fileHasTextContents(destFile, "foo"),
        "IOUtils::move can move a directory and its contents into another one"
      )

      // Clean up.
      await cleanup(srcDir, destDir);
    });

    add_task(async function test_move_failures() {
      // Set up.
      info("Test attempt to rename a non-existent source file");
      const notExistsSrc = PathUtils.join(PathUtils.tempDir, "not_exists_src.tmp");
      const notExistsDest = PathUtils.join(PathUtils.tempDir, "not_exists_dest.tmp");
      // Test.
      await Assert.rejects(
        IOUtils.move(notExistsSrc, notExistsDest),
        /Could not move source file\(.*\) because it does not exist/,
        "IOUtils::move throws if source file does not exist"
      );
      ok(
        !await fileExists(notExistsSrc) && !await fileExists(notExistsDest),
        "IOUtils::move fails if source file does not exist"
      );

      // Set up.
      info("Test attempt to move a directory to a file");
      const destFile = PathUtils.join(PathUtils.tempDir, "test_move_failures_file_dest.tmp");
      const srcDir = PathUtils.join(PathUtils.tempDir, "test_move_failure_src.tmp.d");
      await createFile(destFile);
      await createDir(srcDir);
      // Test.
      await Assert.rejects(
        IOUtils.move(srcDir, destFile),
        /Could not move the source directory\(.*\) to the destination\(.*\) because the destination is not a directory/,
        "IOUtils::move throws if try to move dir into an existing file"
      );

      // Clean up.
      await cleanup(destFile, srcDir);
    });

    add_task(async function test_copy() {
      // Set up.
      const tmpFileName = PathUtils.join(PathUtils.tempDir, "test_ioutils_orig.tmp");
      const destFileName = PathUtils.join(PathUtils.tempDir, "test_ioutils_copy.tmp");
      await createFile(tmpFileName, "original");
      // Test.
      info("Test copy to new file in same directory");
      await IOUtils.copy(tmpFileName, destFileName);
      ok(
        await fileExists(tmpFileName)
        && await fileHasTextContents(destFileName, "original"),
        "IOUtils::copy can copy source to dest in same directory"
      );

      // Set up.
      info("Test copy to existing file with no overwrite");
      await createFile(tmpFileName, "new contents");
      // Test.
      await Assert.rejects(
        IOUtils.copy(tmpFileName, destFileName, { noOverwrite: true }),
        /Could not copy source file\(.*\) to destination\(.*\) because the destination already exists and overwrites are not allowed/,
        "IOUtils::copy will refuse to copy to existing destination if overwrites are disabled"
      );
      ok(
        await fileExists(tmpFileName)
        && await fileHasTextContents(destFileName, "original"),
        "Failed IOUtils::move doesn't move the source file"
      );

      // Test.
      info("Test copy to existing file with overwrite");
      await IOUtils.copy(tmpFileName, destFileName, { noOverwrite: false });
      ok(await fileExists(tmpFileName), "IOUtils::copy retains source");
      ok(
        await fileHasTextContents(destFileName, "new contents"),
        "IOUtils::copy overwrote the destination with the source"
      );

      // Clean up.
      await cleanup(tmpFileName, destFileName);
    });

    add_task(async function test_copy_file_to_dir() {
      // Set up.
      info("Test copy file to non-existing directory");
      const tmpFileName = PathUtils.join(PathUtils.tempDir, "test_copy_file_to_dir.tmp");
      const destDir = PathUtils.join(PathUtils.tempDir, "test_copy_file_to_dir.tmp.d");
      const dest = PathUtils.join(destDir, "dest.tmp");
      await createFile(tmpFileName);
      // Test.
      ok(!await IOUtils.exists(destDir), "Expected path not to exist");
      await IOUtils.copy(tmpFileName, dest);
      ok(
        await fileExists(tmpFileName) && await fileExists(dest),
        "IOUtils::copy creates non-existing parents if needed"
      );

      // Set up.
      info("Test copy file to existing directory")
      await createFile(tmpFileName);
      // Test.
      ok(await dirExists(destDir), `Expected ${destDir} to be a directory`);
      await IOUtils.copy(tmpFileName, dest);
      ok(
        await fileExists(tmpFileName)
        && await fileExists(dest),
        "IOUtils::copy can copy a file into an existing dir"
      );

      // Set up.
      info("Test copy file to existing directory without specifying leaf name")
      await createFile(tmpFileName);
      // Test.
      await IOUtils.copy(tmpFileName, destDir);
      ok(await dirExists(destDir), `Expected ${destDir} to be a directory`);
      ok(
        await fileExists(tmpFileName)
        && await fileExists(PathUtils.join(destDir, PathUtils.filename(tmpFileName))),
        "IOUtils::copy can copy a file into an existing dir"
      );

      // Clean up.
      await cleanup(tmpFileName, destDir);
    });

    add_task(async function test_copy_dir_recursive() {
      // Set up.
      info("Test rename an empty directory");
      const srcDir = PathUtils.join(PathUtils.tempDir, "test_copy_dir.tmp.d");
      const destDir = PathUtils.join(PathUtils.tempDir, "test_copy_dir_dest.tmp.d");
      await createDir(srcDir);
      // Test.
      await IOUtils.copy(srcDir, destDir, { recursive: true });
      ok(
        await dirExists(srcDir) && await dirExists(destDir),
        "IOUtils::copy can recursively copy entire directories"
      );

      // Set up.
      info("Test copy directory and its content into another directory");
      await createDir(srcDir);
      await createFile(PathUtils.join(srcDir, "file.tmp"), "foo");
      // Test.
      await IOUtils.copy(srcDir, destDir, { recursive: true });
      const destFile = PathUtils.join(destDir, PathUtils.filename(srcDir), "file.tmp");
      ok(
        await dirExists(srcDir)
        && await dirExists(destDir)
        && await dirExists(PathUtils.join(destDir, PathUtils.filename(srcDir)))
        && await fileHasTextContents(destFile, "foo"),
        "IOUtils::copy can move a directory and its contents into another one"
      )

      // Clean up.
      await cleanup(srcDir, destDir);
    });

    add_task(async function test_copy_failures() {
      // Set up.
      info("Test attempt to copy a non-existent source file");
      const notExistsSrc = PathUtils.join(PathUtils.tempDir, "test_copy_not_exists_src.tmp");
      const notExistsDest = PathUtils.join(PathUtils.tempDir, "test_copy_not_exists_dest.tmp");
      // Test.
      await Assert.rejects(
        IOUtils.copy(notExistsSrc, notExistsDest),
        /Could not copy source file\(.*\) because it does not exist/,
        "IOUtils::copy throws if source file does not exist"
      );
      ok(
        !await fileExists(notExistsSrc) && !await fileExists(notExistsDest),
        "IOUtils::copy failure due to missing source file does not affect destination"
      );

      // Set up.
      info("Test attempt to copy a directory to a file");
      const destFile = PathUtils.join(PathUtils.tempDir, "test_copy_failures_file_dest.tmp");
      const srcDir = PathUtils.join(PathUtils.tempDir, "test_copy_failure_src.tmp.d");
      await createFile(destFile);
      await createDir(srcDir);
      // Test.
      await Assert.rejects(
        IOUtils.copy(srcDir, destFile, { recursive: true }),
        /Could not copy the source directory\(.*\) to the destination\(.*\) because the destination is not a directory/,
        "IOUtils::copy throws if try to move dir into an existing file"
      );
      ok(await fileHasTextContents(destFile, ""), "IOUtils::copy failure does not affect destination");

      // Set up.
      info("Test copy directory without recursive option");
      await createDir(srcDir);
      // Test.
      await Assert.rejects(
        IOUtils.copy(srcDir, notExistsDest, { recursive: false }),
        /Refused to copy source directory\(.*\) to the destination\(.*\)/,
        "IOUtils::copy throws if try to copy a directory with { recursive: false }"
      );
      console.log(`${notExistsDest} exists?`, await IOUtils.exists(notExistsDest))
      ok(!await IOUtils.exists(notExistsDest), "IOUtils::copy failure does not affect destination");

      // Clean up.
      await cleanup(destFile, srcDir);
    });
  </script>
</head>

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

</html>