Bug 1733097 [wpt PR 30909] - FSA: Make removal of files compatible with file locking...
[gecko.git] / testing / web-platform / tests / file-system-access / script-tests / FileSystemDirectoryHandle-removeEntry.js
blob01e2ebe10889b0de539b54d983124865ab9276bc
2 directory_test(async (t, root) => {
3   const handle =
4       await createFileWithContents(t, 'file-to-remove', '12345', root);
5   await createFileWithContents(t, 'file-to-keep', 'abc', root);
6   await root.removeEntry('file-to-remove');
8   assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
9   await promise_rejects_dom(t, 'NotFoundError', getFileContents(handle));
10 }, 'removeEntry() to remove a file');
12 directory_test(async (t, root) => {
13   const handle =
14       await createFileWithContents(t, 'file-to-remove', '12345', root);
15   await root.removeEntry('file-to-remove');
17   await promise_rejects_dom(t, 'NotFoundError', root.removeEntry('file-to-remove'));
18 }, 'removeEntry() on an already removed file should fail');
20 directory_test(async (t, root) => {
21   const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
22   await createFileWithContents(t, 'file-to-keep', 'abc', root);
23   await root.removeEntry('dir-to-remove');
25   assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
26   await promise_rejects_dom(t, 'NotFoundError', getSortedDirectoryEntries(dir));
27 }, 'removeEntry() to remove an empty directory');
29 directory_test(async (t, root) => {
30   const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
31   t.add_cleanup(() => root.removeEntry('dir-to-remove', {recursive: true}));
32   await createEmptyFile(t, 'file-in-dir', dir);
34   await promise_rejects_dom(
35       t, 'InvalidModificationError', root.removeEntry('dir-to-remove'));
36   assert_array_equals(
37       await getSortedDirectoryEntries(root), ['dir-to-remove/']);
38   assert_array_equals(await getSortedDirectoryEntries(dir), ['file-in-dir']);
39 }, 'removeEntry() on a non-empty directory should fail');
41 directory_test(async (t, root) => {
42   // root
43   // ├──file-to-keep
44   // ├──dir-to-remove
45   //    ├── file0
46   //    ├── dir1-in-dir
47   //    │   └── file1
48   //    └── dir2
49   const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
50   await createFileWithContents(t, 'file-to-keep', 'abc', root);
51   await createEmptyFile(t, 'file0', dir);
52   const dir1_in_dir = await createDirectory(t, 'dir1-in-dir', dir);
53   await createEmptyFile(t, 'file1', dir1_in_dir);
54   await createDirectory(t, 'dir2-in-dir', dir);
56   await root.removeEntry('dir-to-remove', {recursive: true});
57   assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
58 }, 'removeEntry() on a directory recursively should delete all sub-items');
60 directory_test(async (t, root) => {
61   const dir = await createDirectory(t, 'dir', root);
62   await promise_rejects_js(t, TypeError, dir.removeEntry(''));
63 }, 'removeEntry() with empty name should fail');
65 directory_test(async (t, root) => {
66   const dir = await createDirectory(t, 'dir', root);
67   await promise_rejects_js(t, TypeError, dir.removeEntry(kCurrentDirectory));
68 }, `removeEntry() with "${kCurrentDirectory}" name should fail`);
70 directory_test(async (t, root) => {
71   const dir = await createDirectory(t, 'dir', root);
72   await promise_rejects_js(t, TypeError, dir.removeEntry(kParentDirectory));
73 }, `removeEntry() with "${kParentDirectory}" name should fail`);
75 directory_test(async (t, root) => {
76   const dir_name = 'dir-name';
77   const dir = await createDirectory(t, dir_name, root);
79   const file_name = 'file-name';
80   await createEmptyFile(t, file_name, dir);
82   for (let i = 0; i < kPathSeparators.length; ++i) {
83     const path_with_separator = `${dir_name}${kPathSeparators[i]}${file_name}`;
84     await promise_rejects_js(
85         t, TypeError, root.removeEntry(path_with_separator),
86         `removeEntry() must reject names containing "${kPathSeparators[i]}"`);
87   }
88 }, 'removeEntry() with a path separator should fail.');
90 directory_test(async (t, root) => {
91   const handle =
92       await createFileWithContents(t, 'file-to-remove', '12345', root);
93   await createFileWithContents(t, 'file-to-keep', 'abc', root);
95   const writable = await handle.createWritable();
96   await root.removeEntry('file-to-remove');
97   await promise_rejects_dom(t, 'NotFoundError', getFileContents(handle));
99   await writable.close();
100   assert_array_equals(
101       await getSortedDirectoryEntries(root),
102       ['file-to-keep', 'file-to-remove']);
103 }, 'removeEntry() while the file has an open writable succeeds');