-
Notifications
You must be signed in to change notification settings - Fork 54
/
FileSystemDirectoryHandle-getDirectory.js
111 lines (92 loc) · 4.55 KB
/
FileSystemDirectoryHandle-getDirectory.js
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
directory_test(async (t, root) => {
await promise_rejects_dom(
t, 'NotFoundError', root.getDirectory('non-existing-dir'));
}, 'getDirectory(create=false) rejects for non-existing directories');
directory_test(async (t, root) => {
const handle = await root.getDirectory('non-existing-dir', {create: true});
t.add_cleanup(() => root.removeEntry('non-existing-dir', {recursive: true}));
assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.name, 'non-existing-dir');
assert_equals(await getDirectoryEntryCount(handle), 0);
assert_array_equals(
await getSortedDirectoryEntries(root), ['non-existing-dir/']);
}, 'getDirectory(create=true) creates an empty directory');
directory_test(async (t, root) => {
const existing_handle =
await root.getDirectory('dir-with-contents', {create: true});
t.add_cleanup(() => root.removeEntry('dir-with-contents', {recursive: true}));
const file_handle = await createEmptyFile(t, 'test-file', existing_handle);
const handle = await root.getDirectory('dir-with-contents', {create: false});
assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.name, 'dir-with-contents');
assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']);
}, 'getDirectory(create=false) returns existing directories');
directory_test(async (t, root) => {
const existing_handle =
await root.getDirectory('dir-with-contents', {create: true});
t.add_cleanup(() => root.removeEntry('dir-with-contents', {recursive: true}));
const file_handle =
await existing_handle.getFile('test-file', {create: true});
const handle = await root.getDirectory('dir-with-contents', {create: true});
assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.name, 'dir-with-contents');
assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']);
}, 'getDirectory(create=true) returns existing directories without erasing');
directory_test(async (t, root) => {
await createEmptyFile(t, 'file-name', root);
await promise_rejects_dom(t, 'TypeMismatchError', root.getDirectory('file-name'));
await promise_rejects_dom(
t, 'TypeMismatchError', root.getDirectory('file-name', {create: false}));
await promise_rejects_dom(
t, 'TypeMismatchError', root.getDirectory('file-name', {create: true}));
}, 'getDirectory() when a file already exists with the same name');
directory_test(async (t, dir) => {
await promise_rejects_js(
t, TypeError, dir.getDirectory('', {create: true}));
await promise_rejects_js(
t, TypeError, dir.getDirectory('', {create: false}));
}, 'getDirectory() with empty name');
directory_test(async (t, dir) => {
await promise_rejects_js(
t, TypeError, dir.getDirectory(kCurrentDirectory));
await promise_rejects_js(
t, TypeError, dir.getDirectory(kCurrentDirectory, {create: true}));
}, `getDirectory() with "${kCurrentDirectory}" name`);
directory_test(async (t, dir) => {
const subdir = await createDirectory(t, 'subdir-name', /*parent=*/ dir);
await promise_rejects_js(
t, TypeError, subdir.getDirectory(kParentDirectory));
await promise_rejects_js(
t, TypeError,
subdir.getDirectory(kParentDirectory, {create: true}));
}, `getDirectory() with "${kParentDirectory}" name`);
directory_test(async (t, dir) => {
const first_subdir_name = 'first-subdir-name';
const first_subdir =
await createDirectory(t, first_subdir_name, /*parent=*/ dir);
const second_subdir_name = 'second-subdir-name';
const second_subdir =
await createDirectory(t, second_subdir_name, /*parent=*/ first_subdir);
for (let i = 0; i < kPathSeparators.length; ++i) {
const path_with_separator =
`${first_subdir_name}${kPathSeparators[i]}${second_subdir_name}`;
await promise_rejects_js(
t, TypeError, dir.getDirectory(path_with_separator),
`getDirectory() must reject names containing "${kPathSeparators[i]}"`);
}
}, 'getDirectory(create=false) with a path separator when the directory exists');
directory_test(async (t, dir) => {
const subdir_name = 'subdir-name';
const subdir = await createDirectory(t, subdir_name, /*parent=*/ dir);
for (let i = 0; i < kPathSeparators.length; ++i) {
const path_with_separator = `${subdir_name}${kPathSeparators[i]}file_name`;
await promise_rejects_js(
t, TypeError,
dir.getDirectory(path_with_separator, {create: true}),
`getDirectory(true) must reject names containing "${
kPathSeparators[i]}"`);
}
}, 'getDirectory(create=true) with a path separator');