Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeFS] Add kind enum to replace isFile/isDirectory. #24543

Merged
merged 1 commit into from Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 24 additions & 28 deletions native-file-system/resources/messaging-serialize-helpers.js
Expand Up @@ -15,16 +15,15 @@ async function serialize_handles(handle_array) {

// Serializes either a FileSystemFileHandle or FileSystemDirectoryHandle.
async function serialize_handle(handle) {
let serialized;
if (handle.isDirectory) {
serialized = await serialize_file_system_directory_handle(handle);
} else if (handle.isFile) {
serialized = await serialize_file_system_file_handle(handle);
} else {
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${handle}`;
switch (handle.kind) {
case 'directory':
return await serialize_file_system_directory_handle(handle);
case 'file':
return await serialize_file_system_file_handle(handle);
default:
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${handle}`;
}
return serialized;
}

// Creates a dictionary for a FileSystemHandle base, which contains
Expand All @@ -38,8 +37,7 @@ async function serialize_file_system_handle(handle) {
await handle.queryPermission({ writable: true })

return {
is_file: handle.isFile,
is_directory: handle.isDirectory,
kind: handle.kind,
name: handle.name,
read_permission,
write_permission
Expand All @@ -50,8 +48,7 @@ async function serialize_file_system_handle(handle) {
// Also, reads the contents of the file to include with the returned
// dictionary. Example output:
// {
// is_file: true,
// is_directory: false,
// kind: "file",
// name: "example-file-name"
// read_permission: "granted",
// write_permission: "granted",
Expand All @@ -69,8 +66,7 @@ async function serialize_file_system_file_handle(file_handle) {
// Create a dictionary with each property value in FileSystemDirectoryHandle.
// Example output:
// {
// is_file: false,
// is_directory: true,
// kind: "directory",
// name: "example-directory-name"
// read_permission: "granted",
// write_permission: "granted",
Expand All @@ -83,7 +79,7 @@ async function serialize_file_system_directory_handle(directory_handle) {
const serialized_directories = [];
for await (const child_handle of directory_handle.getEntries()) {
const serialized_child_handle = await serialize_handle(child_handle);
if (child_handle.isDirectory) {
if (child_handle.kind === "directory") {
serialized_directories.push(serialized_child_handle);
} else {
serialized_files.push(serialized_child_handle);
Expand Down Expand Up @@ -138,24 +134,24 @@ function assert_equals_serialized_handles(left_array, right_array) {
// Verifies each property of a serialized FileSystemFileHandle or
// FileSystemDirectoryHandle.
function assert_equals_serialized_handle(left, right) {
if (left.is_directory) {
assert_equals_serialized_file_system_directory_handle(left, right);
} else if (left.is_file) {
assert_equals_serialized_file_system_file_handle(left, right);
} else {
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${left}`;
switch (left.kind) {
case 'directory':
assert_equals_serialized_file_system_directory_handle(left, right);
break;
case 'file':
assert_equals_serialized_file_system_file_handle(left, right);
break;
default:
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${left}`;
}
}

// Compares the output of serialize_file_system_handle() for
// two FileSystemHandles.
function assert_equals_serialized_file_system_handle(left, right) {
assert_equals(left.is_file, right.is_file,
'Each FileSystemHandle instance must use the expected "isFile".');

assert_equals(left.is_directory, right.is_directory,
'Each FileSystemHandle instance must use the expected "isDirectory".');
assert_equals(left.kind, right.kind,
'Each FileSystemHandle instance must use the expected "kind".');

assert_equals(left.name, right.name,
'Each FileSystemHandle instance must use the expected "name" ' +
Expand Down
3 changes: 2 additions & 1 deletion native-file-system/resources/native-fs-test-helpers.js
Expand Up @@ -33,7 +33,8 @@ function directory_test(func, description) {
// To be resilient against tests not cleaning up properly, cleanup before
// every test.
for await (let entry of directory.getEntries()) {
await directory.removeEntry(entry.name, {recursive: entry.isDirectory});
await directory.removeEntry(
entry.name, {recursive: entry.kind === 'directory'});
}
await func(t, directory);
}, description);
Expand Down
2 changes: 1 addition & 1 deletion native-file-system/resources/sandboxed-fs-test-helpers.js
Expand Up @@ -10,7 +10,7 @@ async function cleanupSandboxedFileSystem() {
const dir =
await self.getOriginPrivateDirectory();
for await (let entry of dir.getEntries())
await dir.removeEntry(entry.name, {recursive: entry.isDirectory});
await dir.removeEntry(entry.name, {recursive: entry.kind === 'directory'});
}

function directory_test(func, description) {
Expand Down
2 changes: 1 addition & 1 deletion native-file-system/resources/test-helpers.js
Expand Up @@ -34,7 +34,7 @@ async function getDirectoryEntryCount(handle) {
async function getSortedDirectoryEntries(handle) {
let result = [];
for await (let entry of handle.getEntries()) {
if (entry.isDirectory)
if (entry.kind === 'directory')
result.push(entry.name + '/');
else
result.push(entry.name);
Expand Down
Expand Up @@ -8,8 +8,7 @@ directory_test(async (t, root) => {
await root.getDirectoryHandle('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.kind, 'directory');
assert_equals(handle.name, 'non-existing-dir');
assert_equals(await getDirectoryEntryCount(handle), 0);
assert_array_equals(
Expand All @@ -25,8 +24,7 @@ directory_test(async (t, root) => {
const handle =
await root.getDirectoryHandle('dir-with-contents', {create: false});

assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.kind, 'directory');
assert_equals(handle.name, 'dir-with-contents');
assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']);
}, 'getDirectoryHandle(create=false) returns existing directories');
Expand All @@ -41,8 +39,7 @@ directory_test(async (t, root) => {
const handle =
await root.getDirectoryHandle('dir-with-contents', {create: true});

assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.kind, 'directory');
assert_equals(handle.name, 'dir-with-contents');
assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']);
}, 'getDirectoryHandle(create=true) returns existing directories without erasing');
Expand Down
Expand Up @@ -7,8 +7,7 @@ directory_test(async (t, dir) => {
const handle = await dir.getFileHandle('non-existing-file', {create: true});
t.add_cleanup(() => dir.removeEntry('non-existing-file'));

assert_true(handle.isFile);
assert_false(handle.isDirectory);
assert_equals(handle.kind, 'file');
assert_equals(handle.name, 'non-existing-file');
assert_equals(await getFileSize(handle), 0);
assert_equals(await getFileContents(handle), '');
Expand All @@ -19,8 +18,7 @@ directory_test(async (t, dir) => {
t, 'existing-file', '1234567890', /*parent=*/ dir);
const handle = await dir.getFileHandle('existing-file');

assert_true(handle.isFile);
assert_false(handle.isDirectory);
assert_equals(handle.kind, 'file');
assert_equals(handle.name, 'existing-file');
assert_equals(await getFileSize(handle), 10);
assert_equals(await getFileContents(handle), '1234567890');
Expand All @@ -31,8 +29,7 @@ directory_test(async (t, dir) => {
t, 'file-with-contents', '1234567890', /*parent=*/ dir);
const handle = await dir.getFileHandle('file-with-contents', {create: true});

assert_true(handle.isFile);
assert_false(handle.isDirectory);
assert_equals(handle.kind, 'file');
assert_equals(handle.name, 'file-with-contents');
assert_equals(await getFileSize(handle), 10);
assert_equals(await getFileContents(handle), '1234567890');
Expand Down
3 changes: 1 addition & 2 deletions native-file-system/showDirectoryPicker-manual.https.html
Expand Up @@ -22,8 +22,7 @@
const dir = await self.showDirectoryPicker();
assert_true(dir instanceof FileSystemHandle);
assert_true(dir instanceof FileSystemDirectoryHandle);
assert_false(dir.isFile);
assert_true(dir.isDirectory);
assert_equals(dir.kind, "directory");
assert_equals(dir.name, 'data');
assert_array_equals(await getSortedDirectoryEntries(dir), ['testfile.txt']);

Expand Down
3 changes: 1 addition & 2 deletions native-file-system/showOpenFilePicker-manual.https.html
Expand Up @@ -29,8 +29,7 @@
assert_equals(files.length, 1);
assert_true(files[0] instanceof FileSystemHandle);
assert_true(files[0] instanceof FileSystemFileHandle);
assert_true(files[0].isFile);
assert_false(files[0].isDirectory);
assert_equals(files[0].kind, "file");
assert_equals(files[0].name, 'testfile.txt');
assert_equals(await (await files[0].getFile()).text(), 'Hello World!\n');

Expand Down
3 changes: 1 addition & 2 deletions native-file-system/showSaveFilePicker-manual.https.html
Expand Up @@ -19,8 +19,7 @@
});
assert_true(file instanceof FileSystemHandle);
assert_true(file instanceof FileSystemFileHandle);
assert_true(file.isFile);
assert_false(file.isDirectory);
assert_equals(file.kind, "file");
assert_equals(file.name, 'testfile.txt');
assert_equals(await (await file.getFile()).text(), '',
'showSaveFilePicker should clear contents of file');
Expand Down