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

directory_test(async (t, dir) => {
  await promise_rejects_dom(
      t, 'NotFoundError', dir.getFileHandle('non-existing-file'));
}, 'getFileHandle(create=false) rejects for non-existing files');

directory_test(async (t, dir) => {
  const handle = await dir.getFileHandle('non-existing-file', {create: true});
  t.add_cleanup(() => dir.removeEntry('non-existing-file'));

  assert_equals(handle.kind, 'file');
  assert_equals(handle.name, 'non-existing-file');
  assert_equals(await getFileSize(handle), 0);
  assert_equals(await getFileContents(handle), '');
}, 'getFileHandle(create=true) creates an empty file for non-existing files');

directory_test(async (t, dir) => {
  var name = '';
  // test the ascii characters -- start after the non-character ASCII values, exclude DEL
  for (let i = 32; i < 127; i++) {
    // Path separators are disallowed
    let disallow = false;
    for (let j = 0; j < kPathSeparators.length; ++j) {
      if (String.fromCharCode(i) == kPathSeparators[j]) {
        disallow = true;
      }
    }
    if (!disallow) {
      name += String.fromCharCode(i);
    }
  }
  // Add in CR, LF, FF, Tab, Vertical Tab
  for (let i = 9; i < 14; i++) {
    name += String.fromCharCode(i);
  }
  const handle = await dir.getFileHandle(name, {create: true});
  t.add_cleanup(() => dir.removeEntry(name));

  assert_equals(handle.kind, 'file');
  assert_equals(handle.name, name);
  assert_equals(await getFileSize(handle), 0);
  assert_equals(await getFileContents(handle), '');
}, 'getFileHandle(create=true) creates an empty file with all valid ASCII characters in the name');

directory_test(async (t, dir) => {
  var name;
  // A non-ASCII name
  name = 'Funny cat \u{1F639}'
  const handle = await dir.getFileHandle(name, {create: true});
  t.add_cleanup(() => dir.removeEntry(name));

  assert_equals(handle.kind, 'file');
  assert_equals(handle.name, name);
  assert_equals(await getFileSize(handle), 0);
  assert_equals(await getFileContents(handle), '');
}, 'getFileHandle(create=true) creates an empty file with non-ASCII characters in the name');

directory_test(async (t, dir) => {
  const existing_handle = await createFileWithContents(
      t, 'existing-file', '1234567890', /*parent=*/ dir);
  const handle = await dir.getFileHandle('existing-file');

  assert_equals(handle.kind, 'file');
  assert_equals(handle.name, 'existing-file');
  assert_equals(await getFileSize(handle), 10);
  assert_equals(await getFileContents(handle), '1234567890');
}, 'getFileHandle(create=false) returns existing files');

directory_test(async (t, dir) => {
  const existing_handle = await createFileWithContents(
      t, 'file-with-contents', '1234567890', /*parent=*/ dir);
  const handle = await dir.getFileHandle('file-with-contents', {create: true});

  assert_equals(handle.kind, 'file');
  assert_equals(handle.name, 'file-with-contents');
  assert_equals(await getFileSize(handle), 10);
  assert_equals(await getFileContents(handle), '1234567890');
}, 'getFileHandle(create=true) returns existing files without erasing');

directory_test(async (t, dir) => {
  const dir_handle = await dir.getDirectoryHandle('dir-name', {create: true});
  t.add_cleanup(() => dir.removeEntry('dir-name', {recursive: true}));

  await promise_rejects_dom(
      t, 'TypeMismatchError', dir.getFileHandle('dir-name'));
}, 'getFileHandle(create=false) when a directory already exists with the same name');

directory_test(async (t, dir) => {
  const dir_handle = await dir.getDirectoryHandle('dir-name', {create: true});
  t.add_cleanup(() => dir.removeEntry('dir-name', {recursive: true}));

  await promise_rejects_dom(
      t, 'TypeMismatchError', dir.getFileHandle('dir-name', {create: true}));
}, 'getFileHandle(create=true) when a directory already exists with the same name');

directory_test(async (t, dir) => {
  await promise_rejects_js(t, TypeError, dir.getFileHandle('', {create: true}));
  await promise_rejects_js(
      t, TypeError, dir.getFileHandle('', {create: false}));
}, 'getFileHandle() with empty name');

directory_test(async (t, dir) => {
  await promise_rejects_js(t, TypeError, dir.getFileHandle(kCurrentDirectory));
  await promise_rejects_js(
      t, TypeError, dir.getFileHandle(kCurrentDirectory, {create: true}));
}, `getFileHandle() with "${kCurrentDirectory}" name`);

directory_test(async (t, dir) => {
  const subdir = await createDirectory(t, 'subdir-name', /*parent=*/ dir);

  await promise_rejects_js(
      t, TypeError, subdir.getFileHandle(kParentDirectory));
  await promise_rejects_js(
      t, TypeError, subdir.getFileHandle(kParentDirectory, {create: true}));
}, `getFileHandle() with "${kParentDirectory}" name`);

directory_test(async (t, dir) => {
  const subdir_name = 'subdir-name';
  const subdir = await createDirectory(t, subdir_name, /*parent=*/ dir);

  const file_name = 'file-name';
  await createEmptyFile(t, file_name, /*parent=*/ subdir);

  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.getFileHandle(path_with_separator),
        `getFileHandle() must reject names containing "${kPathSeparators[i]}"`);
  }
}, 'getFileHandle(create=false) with a path separator when the file 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.getFileHandle(path_with_separator, {create: true}),
        `getFileHandle(create=true) must reject names containing "${
            kPathSeparators[i]}"`);
  }
}, 'getFileHandle(create=true) with a path separator');