summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fs/script-tests/FileSystemDirectoryHandle-removeEntry.js
blob: 07d26e901160e57f584f93647734e37be0bab912 (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
'use strict';

directory_test(async (t, root) => {
  const handle =
      await createFileWithContents(t, 'file-to-remove', '12345', root);
  await createFileWithContents(t, 'file-to-keep', 'abc', root);
  await root.removeEntry('file-to-remove');

  assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
  await promise_rejects_dom(t, 'NotFoundError', getFileContents(handle));
}, 'removeEntry() to remove a file');

directory_test(async (t, root) => {
  const handle =
      await createFileWithContents(t, 'file-to-remove', '12345', root);
  await root.removeEntry('file-to-remove');

  await promise_rejects_dom(
      t, 'NotFoundError', root.removeEntry('file-to-remove'));
}, 'removeEntry() on an already removed file should fail');

directory_test(async (t, root) => {
  const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
  await createFileWithContents(t, 'file-to-keep', 'abc', root);
  await root.removeEntry('dir-to-remove');

  assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
}, 'removeEntry() to remove an empty directory');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'dir-to-remove', root);
  await createFileWithContents(t, 'file-in-dir', 'abc', dir);

  await promise_rejects_dom(
      t, 'InvalidModificationError', root.removeEntry('dir-to-remove'));
  assert_array_equals(
      await getSortedDirectoryEntries(root), ['dir-to-remove/']);
  assert_array_equals(await getSortedDirectoryEntries(dir), ['file-in-dir']);
}, 'removeEntry() on a non-empty directory should fail');

directory_test(async (t, root) => {
  // root
  // ├──file-to-keep
  // ├──dir-to-remove
  //    ├── file0
  //    ├── dir1-in-dir
  //    │   └── file1
  //    └── dir2
  const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
  await createFileWithContents(t, 'file-to-keep', 'abc', root);
  await createEmptyFile(t, 'file0', dir);
  const dir1_in_dir = await createDirectory(t, 'dir1-in-dir', dir);
  await createEmptyFile(t, 'file1', dir1_in_dir);
  await createDirectory(t, 'dir2-in-dir', dir);

  await root.removeEntry('dir-to-remove', {recursive: true});
  assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
}, 'removeEntry() on a directory recursively should delete all sub-items');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'dir', root);
  await promise_rejects_js(t, TypeError, dir.removeEntry(''));
}, 'removeEntry() with empty name should fail');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'dir', root);
  await promise_rejects_js(t, TypeError, dir.removeEntry(kCurrentDirectory));
}, `removeEntry() with "${kCurrentDirectory}" name should fail`);

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'dir', root);
  await promise_rejects_js(t, TypeError, dir.removeEntry(kParentDirectory));
}, `removeEntry() with "${kParentDirectory}" name should fail`);

directory_test(async (t, root) => {
  const dir_name = 'dir-name';
  const dir = await createDirectory(t, dir_name, root);

  const file_name = 'file-name';
  await createEmptyFile(t, file_name, dir);

  for (let i = 0; i < kPathSeparators.length; ++i) {
    const path_with_separator = `${dir_name}${kPathSeparators[i]}${file_name}`;
    await promise_rejects_js(
        t, TypeError, root.removeEntry(path_with_separator),
        `removeEntry() must reject names containing "${kPathSeparators[i]}"`);
  }
}, 'removeEntry() with a path separator should fail.');

directory_test(async (t, root) => {
  const handle =
      await createFileWithContents(t, 'file-to-remove', '12345', root);
  await createFileWithContents(t, 'file-to-keep', 'abc', root);
  await root.removeEntry('file-to-remove');

  assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
  await promise_rejects_dom(t, 'NotFoundError', getFileContents(handle));
}, 'removeEntry() to remove a file');

directory_test(async (t, root) => {
  const handle =
      await createFileWithContents(t, 'file-to-remove', '12345', root);
  await root.removeEntry('file-to-remove');

  await promise_rejects_dom(t, 'NotFoundError', root.removeEntry('file-to-remove'));
}, 'removeEntry() on an already removed file should fail');

directory_test(async (t, root) => {
  const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
  await createFileWithContents(t, 'file-to-keep', 'abc', root);
  await root.removeEntry('dir-to-remove');

  assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
}, 'removeEntry() to remove an empty directory');

directory_test(async (t, root) => {
  const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
  t.add_cleanup(() => root.removeEntry('dir-to-remove', {recursive: true}));
  await createEmptyFile(t, 'file-in-dir', dir);

  await promise_rejects_dom(
      t, 'InvalidModificationError', root.removeEntry('dir-to-remove'));
  assert_array_equals(
      await getSortedDirectoryEntries(root), ['dir-to-remove/']);
  assert_array_equals(await getSortedDirectoryEntries(dir), ['file-in-dir']);
}, 'removeEntry() on a non-empty directory should fail');

directory_test(async (t, root) => {
  // root
  // ├──file-to-keep
  // ├──dir-to-remove
  //    ├── file0
  //    ├── dir1-in-dir
  //    │   └── file1
  //    └── dir2
  const dir = await root.getDirectoryHandle('dir-to-remove', {create: true});
  await createFileWithContents(t, 'file-to-keep', 'abc', root);
  await createEmptyFile(t, 'file0', dir);
  const dir1_in_dir = await createDirectory(t, 'dir1-in-dir', dir);
  await createEmptyFile(t, 'file1', dir1_in_dir);
  await createDirectory(t, 'dir2-in-dir', dir);

  await root.removeEntry('dir-to-remove', {recursive: true});
  assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
}, 'removeEntry() on a directory recursively should delete all sub-items');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'dir', root);
  await promise_rejects_js(t, TypeError, dir.removeEntry(''));
}, 'removeEntry() with empty name should fail');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'dir', root);
  await promise_rejects_js(t, TypeError, dir.removeEntry(kCurrentDirectory));
}, `removeEntry() with "${kCurrentDirectory}" name should fail`);

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'dir', root);
  await promise_rejects_js(t, TypeError, dir.removeEntry(kParentDirectory));
}, `removeEntry() with "${kParentDirectory}" name should fail`);

directory_test(async (t, root) => {
  const dir_name = 'dir-name';
  const dir = await createDirectory(t, dir_name, root);

  const file_name = 'file-name';
  await createEmptyFile(t, file_name, dir);

  for (let i = 0; i < kPathSeparators.length; ++i) {
    const path_with_separator = `${dir_name}${kPathSeparators[i]}${file_name}`;
    await promise_rejects_js(
        t, TypeError, root.removeEntry(path_with_separator),
        `removeEntry() must reject names containing "${kPathSeparators[i]}"`);
  }
}, 'removeEntry() with a path separator should fail.');

directory_test(async (t, root) => {
  const handle =
      await createFileWithContents(t, 'file-to-remove', '12345', root);
  await createFileWithContents(t, 'file-to-keep', 'abc', root);

  const writable = await cleanup_writable(t, await handle.createWritable());
  await promise_rejects_dom(
      t, 'NoModificationAllowedError', root.removeEntry('file-to-remove'));

  await writable.close();
  await root.removeEntry('file-to-remove');

  assert_array_equals(await getSortedDirectoryEntries(root), ['file-to-keep']);
}, 'removeEntry() while the file has an open writable fails');

directory_test(async (t, root) => {
  const dir_name = 'dir-name';
  const dir = await createDirectory(t, dir_name, root);

  const handle =
      await createFileWithContents(t, 'file-to-remove', '12345', dir);
  await createFileWithContents(t, 'file-to-keep', 'abc', dir);

  const writable = await cleanup_writable(t, await handle.createWritable());
  await promise_rejects_dom(
      t, 'NoModificationAllowedError', root.removeEntry(dir_name));

  await writable.close();
  assert_array_equals(
      await getSortedDirectoryEntries(dir), ['file-to-keep', 'file-to-remove']);

  await dir.removeEntry('file-to-remove');
  assert_array_equals(await getSortedDirectoryEntries(dir), ['file-to-keep']);
}, 'removeEntry() of a directory while a containing file has an open writable fails');

directory_test(async (t, root) => {
  const handle =
      await createFileWithContents(t, 'file-to-remove', '12345', root);
  await root.removeEntry('file-to-remove');

  await promise_rejects_dom(t, 'NotFoundError', cleanup_writable(t, handle.createWritable({keepExistingData: true})));

  assert_array_equals(
      await getSortedDirectoryEntries(root),
      []);
}, 'createWritable after removeEntry succeeds but doesnt recreate the file');