FileSystemDirectoryHandle-removeEntry.js (9474B)
1 'use strict'; 2 3 directory_test(async (t, root) => { 4 const handle = await createFileWithContents('file-to-remove', '12345', root); 5 await createFileWithContents('file-to-keep', 'abc', root); 6 await root.removeEntry('file-to-remove'); 7 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'); 11 12 directory_test(async (t, root) => { 13 const handle = await createFileWithContents('file-to-remove', '12345', root); 14 await root.removeEntry('file-to-remove'); 15 16 await promise_rejects_dom( 17 t, 'NotFoundError', root.removeEntry('file-to-remove')); 18 }, 'removeEntry() on an already removed file should fail'); 19 20 directory_test(async (t, root) => { 21 const dir = await root.getDirectoryHandle('dir-to-remove', {create: true}); 22 await createFileWithContents('file-to-keep', 'abc', root); 23 await root.removeEntry('dir-to-remove'); 24 25 assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']); 26 }, 'removeEntry() to remove an empty directory'); 27 28 directory_test(async (t, root) => { 29 const dir = await createDirectory('dir-to-remove', root); 30 await createFileWithContents('file-in-dir', 'abc', dir); 31 32 await promise_rejects_dom( 33 t, 'InvalidModificationError', root.removeEntry('dir-to-remove')); 34 assert_array_equals( 35 await getSortedDirectoryEntries(root), ['dir-to-remove/']); 36 assert_array_equals(await getSortedDirectoryEntries(dir), ['file-in-dir']); 37 }, 'removeEntry() on a non-empty directory should fail'); 38 39 directory_test(async (t, root) => { 40 // root 41 // ├──file-to-keep 42 // ├──dir-to-remove 43 // ├── file0 44 // ├── dir1-in-dir 45 // │ └── file1 46 // └── dir2 47 const dir = await root.getDirectoryHandle('dir-to-remove', {create: true}); 48 await createFileWithContents('file-to-keep', 'abc', root); 49 await createEmptyFile('file0', dir); 50 const dir1_in_dir = await createDirectory('dir1-in-dir', dir); 51 await createEmptyFile('file1', dir1_in_dir); 52 await createDirectory('dir2-in-dir', dir); 53 54 await root.removeEntry('dir-to-remove', {recursive: true}); 55 assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']); 56 }, 'removeEntry() on a directory recursively should delete all sub-items'); 57 58 directory_test(async (t, root) => { 59 const dir = await createDirectory('dir', root); 60 await promise_rejects_js(t, TypeError, dir.removeEntry('')); 61 }, 'removeEntry() with empty name should fail'); 62 63 directory_test(async (t, root) => { 64 const dir = await createDirectory('dir', root); 65 await promise_rejects_js(t, TypeError, dir.removeEntry(kCurrentDirectory)); 66 }, `removeEntry() with "${kCurrentDirectory}" name should fail`); 67 68 directory_test(async (t, root) => { 69 const dir = await createDirectory('dir', root); 70 await promise_rejects_js(t, TypeError, dir.removeEntry(kParentDirectory)); 71 }, `removeEntry() with "${kParentDirectory}" name should fail`); 72 73 directory_test(async (t, root) => { 74 const dir_name = 'dir-name'; 75 const dir = await createDirectory(dir_name, root); 76 77 const file_name = 'file-name'; 78 await createEmptyFile(file_name, dir); 79 80 for (let i = 0; i < kPathSeparators.length; ++i) { 81 const path_with_separator = `${dir_name}${kPathSeparators[i]}${file_name}`; 82 await promise_rejects_js( 83 t, TypeError, root.removeEntry(path_with_separator), 84 `removeEntry() must reject names containing "${kPathSeparators[i]}"`); 85 } 86 }, 'removeEntry() with a path separator should fail.'); 87 88 directory_test(async (t, root) => { 89 const handle = 90 await createFileWithContents(t, 'file-to-remove', '12345', root); 91 await createFileWithContents(t, 'file-to-keep', 'abc', root); 92 await root.removeEntry('file-to-remove'); 93 94 assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']); 95 await promise_rejects_dom(t, 'NotFoundError', getFileContents(handle)); 96 }, 'removeEntry() to remove a file'); 97 98 directory_test(async (t, root) => { 99 const handle = 100 await createFileWithContents(t, 'file-to-remove', '12345', root); 101 await root.removeEntry('file-to-remove'); 102 103 await promise_rejects_dom(t, 'NotFoundError', root.removeEntry('file-to-remove')); 104 }, 'removeEntry() on an already removed file should fail'); 105 106 directory_test(async (t, root) => { 107 const dir = await root.getDirectoryHandle('dir-to-remove', {create: true}); 108 await createFileWithContents(t, 'file-to-keep', 'abc', root); 109 await root.removeEntry('dir-to-remove'); 110 111 assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']); 112 }, 'removeEntry() to remove an empty directory'); 113 114 directory_test(async (t, root) => { 115 const dir = await root.getDirectoryHandle('dir-to-remove', {create: true}); 116 t.add_cleanup(() => root.removeEntry('dir-to-remove', {recursive: true})); 117 await createEmptyFile(t, 'file-in-dir', dir); 118 119 await promise_rejects_dom( 120 t, 'InvalidModificationError', root.removeEntry('dir-to-remove')); 121 assert_array_equals( 122 await getSortedDirectoryEntries(root), ['dir-to-remove/']); 123 assert_array_equals(await getSortedDirectoryEntries(dir), ['file-in-dir']); 124 }, 'removeEntry() on a non-empty directory should fail'); 125 126 directory_test(async (t, root) => { 127 // root 128 // ├──file-to-keep 129 // ├──dir-to-remove 130 // ├── file0 131 // ├── dir1-in-dir 132 // │ └── file1 133 // └── dir2 134 const dir = await root.getDirectoryHandle('dir-to-remove', {create: true}); 135 await createFileWithContents(t, 'file-to-keep', 'abc', root); 136 await createEmptyFile(t, 'file0', dir); 137 const dir1_in_dir = await createDirectory(t, 'dir1-in-dir', dir); 138 await createEmptyFile(t, 'file1', dir1_in_dir); 139 await createDirectory(t, 'dir2-in-dir', dir); 140 141 await root.removeEntry('dir-to-remove', {recursive: true}); 142 assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']); 143 }, 'removeEntry() on a directory recursively should delete all sub-items'); 144 145 directory_test(async (t, root) => { 146 const dir = await createDirectory(t, 'dir', root); 147 await promise_rejects_js(t, TypeError, dir.removeEntry('')); 148 }, 'removeEntry() with empty name should fail'); 149 150 directory_test(async (t, root) => { 151 const dir = await createDirectory(t, 'dir', root); 152 await promise_rejects_js(t, TypeError, dir.removeEntry(kCurrentDirectory)); 153 }, `removeEntry() with "${kCurrentDirectory}" name should fail`); 154 155 directory_test(async (t, root) => { 156 const dir = await createDirectory(t, 'dir', root); 157 await promise_rejects_js(t, TypeError, dir.removeEntry(kParentDirectory)); 158 }, `removeEntry() with "${kParentDirectory}" name should fail`); 159 160 directory_test(async (t, root) => { 161 const dir_name = 'dir-name'; 162 const dir = await createDirectory(t, dir_name, root); 163 164 const file_name = 'file-name'; 165 await createEmptyFile(t, file_name, dir); 166 167 for (let i = 0; i < kPathSeparators.length; ++i) { 168 const path_with_separator = `${dir_name}${kPathSeparators[i]}${file_name}`; 169 await promise_rejects_js( 170 t, TypeError, root.removeEntry(path_with_separator), 171 `removeEntry() must reject names containing "${kPathSeparators[i]}"`); 172 } 173 }, 'removeEntry() with a path separator should fail.'); 174 175 directory_test(async (t, root) => { 176 const handle = await createFileWithContents('file-to-remove', '12345', root); 177 await createFileWithContents('file-to-keep', 'abc', root); 178 179 const writable = await cleanup_writable(t, await handle.createWritable()); 180 await promise_rejects_dom( 181 t, 'NoModificationAllowedError', root.removeEntry('file-to-remove')); 182 183 await writable.close(); 184 await root.removeEntry('file-to-remove'); 185 186 assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']); 187 }, 'removeEntry() while the file has an open writable fails'); 188 189 directory_test(async (t, root) => { 190 const dir_name = 'dir-name'; 191 const dir = await createDirectory(dir_name, root); 192 193 const handle = await createFileWithContents('file-to-remove', '12345', dir); 194 await createFileWithContents('file-to-keep', 'abc', dir); 195 196 const writable = await cleanup_writable(t, await handle.createWritable()); 197 await promise_rejects_dom( 198 t, 'NoModificationAllowedError', root.removeEntry(dir_name)); 199 200 await writable.close(); 201 assert_array_equals( 202 await getSortedDirectoryEntries(dir), ['file-to-keep', 'file-to-remove']); 203 204 await dir.removeEntry('file-to-remove'); 205 assert_array_equals(await getSortedDirectoryEntries(dir), ['file-to-keep']); 206 }, 'removeEntry() of a directory while a containing file has an open writable fails'); 207 208 directory_test(async (t, root) => { 209 const handle = await createFileWithContents('file-to-remove', '12345', root); 210 await root.removeEntry('file-to-remove'); 211 212 await promise_rejects_dom( 213 t, 'NotFoundError', 214 cleanup_writable(t, handle.createWritable({keepExistingData: true}))); 215 216 assert_array_equals(await getSortedDirectoryEntries(root), []); 217 }, 'createWritable after removeEntry succeeds but doesnt recreate the file'); 218 219 directory_test(async (t, root) => { 220 // root 221 // ├──file-to-keep 222 // ├──dir-to-keep 223 await createFileWithContents('file-to-keep', 'abc', root); 224 const dir_to_keep = await root.getDirectoryHandle('dir-to-keep', {create: true}); 225 await promise_rejects_dom( 226 t, 'NotFoundError', root.removeEntry('dir-to-remove', {recursive: true})); 227 }, 'removeEntry() on a non-existent directory recursively should throw NotFoundError');