Skip to content

Commit

Permalink
FSA: Make WPTs assert that overwriting file moves are allowed
Browse files Browse the repository at this point in the history
This codifies the behavior agreed upon here:
whatwg/fs#10 (comment)

See go/fsa-overwriting-moves for more context

Bug: 1366652, 1381621
Change-Id: I24d8ff94ebd9f6b6a8f2ffe9a0e30623193e7eab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4076968
Auto-Submit: Austin Sullivan <asully@chromium.org>
Reviewed-by: Daseul Lee <dslee@chromium.org>
Commit-Queue: Daseul Lee <dslee@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1079294}
  • Loading branch information
a-sully authored and BruceDai committed Dec 13, 2022
1 parent 4e54bbd commit c38917b
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions fs/script-tests/FileSystemFileHandle-move.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,21 @@ directory_test(async (t, root) => {
await promise_rejects_dom(
t, 'NoModificationAllowedError', handle.move('file-after'));

// Can't move handle to overwrite an existing file.
await stream.close();
await promise_rejects_dom(
t, 'InvalidModificationError', handle.move('file-after'));
assert_array_equals(
await getSortedDirectoryEntries(root), ['file-after', 'file-before']);
}, 'move(name) while the destination file has an open writable fails');

directory_test(async (t, root) => {
const handle = await createFileWithContents(t, 'file-before', 'abc', root);
const handle_dest =
await createFileWithContents(t, 'file-after', '123', root);

await handle.move('file-after');
assert_array_equals(await getSortedDirectoryEntries(root), ['file-after']);
assert_equals(await getFileContents(handle), 'abc');
assert_equals(await getFileContents(handle_dest), 'abc');
}, 'move(name) can overwrite an existing file');

directory_test(async (t, root) => {
const handle = await createFileWithContents(t, 'file-before', 'foo', root);
Expand Down Expand Up @@ -290,13 +297,23 @@ directory_test(async (t, root) => {
// Assert the file is still in the source directory.
assert_array_equals(await getSortedDirectoryEntries(dir_src), ['file']);

// Can't move handle to overwrite an existing file.
await stream.close();
await promise_rejects_dom(
t, 'InvalidModificationError', file.move(dir_dest));
assert_array_equals(await getSortedDirectoryEntries(dir_src), ['file']);
assert_array_equals(await getSortedDirectoryEntries(dir_dest), ['file']);
}, 'move(dir) while the destination file has an open writable fails');

directory_test(async (t, root) => {
const dir_src = await root.getDirectoryHandle('dir-src', {create: true});
const dir_dest = await root.getDirectoryHandle('dir-dest', {create: true});
const file = await createFileWithContents(t, 'file', 'abc', dir_src);
const file_dest = await createFileWithContents(t, 'file', '123', dir_dest);

await file.move(dir_dest);
assert_array_equals(await getSortedDirectoryEntries(dir_src), []);
assert_array_equals(await getSortedDirectoryEntries(dir_dest), ['file']);
assert_equals(await getFileContents(file), 'abc');
assert_equals(await getFileContents(file_dest), 'abc');
}, 'move(dir) can overwrite an existing file');

directory_test(async (t, root) => {
const dir_src = await root.getDirectoryHandle('dir-src', {create: true});
const dir_dest = await root.getDirectoryHandle('dir-dest', {create: true});
Expand All @@ -314,12 +331,22 @@ directory_test(async (t, root) => {
// Assert the file is still in the source directory.
assert_array_equals(await getSortedDirectoryEntries(dir_src), ['file-src']);

// Can't move handle to overwrite an existing file.
await stream.close();
await promise_rejects_dom(
t, 'InvalidModificationError', file.move(dir_dest, 'file-dest'));
// Assert the file is still in the source directory.
assert_array_equals(await getSortedDirectoryEntries(dir_src), ['file-src']);
assert_equals(await getFileContents(file), 'abc');
assert_equals(await getFileSize(file), 3);
assert_array_equals(await getSortedDirectoryEntries(dir_dest), ['file-dest']);
}, 'move(dir, name) while the destination file has an open writable fails');

directory_test(async (t, root) => {
const dir_src = await root.getDirectoryHandle('dir-src', {create: true});
const dir_dest = await root.getDirectoryHandle('dir-dest', {create: true});
const file = await createFileWithContents(t, 'file-src', 'abc', dir_src);
const file_dest =
await createFileWithContents(t, 'file-dest', '123', dir_dest);

await file.move(dir_dest, 'file-dest');

// Assert the file has been moved to the destination directory and renamed.
assert_array_equals(await getSortedDirectoryEntries(dir_src), []);
assert_array_equals(await getSortedDirectoryEntries(dir_dest), ['file-dest']);
assert_equals(await getFileContents(file), 'abc');
assert_equals(await getFileContents(file_dest), 'abc');
}, 'move(dir, name) can overwrite an existing file');

0 comments on commit c38917b

Please sign in to comment.