From c38917b0301be65d7346df4822e016edd02843f8 Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Mon, 5 Dec 2022 10:10:23 -0800 Subject: [PATCH] FSA: Make WPTs assert that overwriting file moves are allowed This codifies the behavior agreed upon here: https://github.com/whatwg/fs/pull/10#issuecomment-1322993643 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 Reviewed-by: Daseul Lee Commit-Queue: Daseul Lee Cr-Commit-Position: refs/heads/main@{#1079294} --- fs/script-tests/FileSystemFileHandle-move.js | 55 +++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/fs/script-tests/FileSystemFileHandle-move.js b/fs/script-tests/FileSystemFileHandle-move.js index 447ff9a78d9838..fba0929d4155df 100644 --- a/fs/script-tests/FileSystemFileHandle-move.js +++ b/fs/script-tests/FileSystemFileHandle-move.js @@ -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); @@ -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}); @@ -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');