FileSystemWritableFileStream.js (4235B)
1 'use strict'; 2 3 directory_test(async (t, root) => { 4 const handle = await createEmptyFile('trunc_shrink', root); 5 const stream = await handle.createWritable(); 6 7 await stream.write('1234567890'); 8 await stream.truncate(5); 9 await stream.close(); 10 11 assert_equals(await getFileContents(handle), '12345'); 12 assert_equals(await getFileSize(handle), 5); 13 }, 'truncate() to shrink a file'); 14 15 directory_test(async (t, root) => { 16 const handle = await createEmptyFile('trunc_grow', root); 17 const stream = await handle.createWritable(); 18 19 await stream.write('abc'); 20 await stream.truncate(5); 21 await stream.close(); 22 23 assert_equals(await getFileContents(handle), 'abc\0\0'); 24 assert_equals(await getFileSize(handle), 5); 25 }, 'truncate() to grow a file'); 26 27 directory_test(async (t, root) => { 28 const dir = await createDirectory('parent_dir', root); 29 const file_name = 'create_writable_fails_when_dir_removed.txt'; 30 const handle = await createEmptyFile(file_name, dir); 31 32 await root.removeEntry('parent_dir', {recursive: true}); 33 await promise_rejects_dom(t, 'NotFoundError', cleanup_writable(t, handle.createWritable())); 34 }, 'createWritable() fails when parent directory is removed'); 35 36 directory_test(async (t, root) => { 37 const handle = 38 await createFileWithContents('atomic_file_is_copied.txt', 'fooks', root); 39 const stream = await handle.createWritable({keepExistingData: true}); 40 41 await stream.write('bar'); 42 await stream.close(); 43 assert_equals(await getFileContents(handle), 'barks'); 44 assert_equals(await getFileSize(handle), 5); 45 }, 'createWritable({keepExistingData: true}): atomic writable file stream initialized with source contents'); 46 47 directory_test(async (t, root) => { 48 const handle = await createFileWithContents( 49 'atomic_file_is_not_copied.txt', 'very long string', root); 50 const stream = await handle.createWritable({keepExistingData: false}); 51 52 await stream.write('bar'); 53 assert_equals(await getFileContents(handle), 'very long string'); 54 await stream.close(); 55 assert_equals(await getFileContents(handle), 'bar'); 56 assert_equals(await getFileSize(handle), 3); 57 }, 'createWritable({keepExistingData: false}): atomic writable file stream initialized with empty file'); 58 59 directory_test(async (t, root) => { 60 const dir = await createDirectory(t, 'parent_dir', root); 61 const file_name = 'create_writable_twice.txt'; 62 const handle1 = await createEmptyFile(t, file_name, dir); 63 const handle2 = await dir.getFileHandle(file_name, {create: false}); 64 65 const stream1 = await handle1.createWritable(); 66 const stream2 = await handle2.createWritable(); 67 }, 'createWritable() can be called on two handles representing the same file'); 68 69 directory_test(async (t, root) => { 70 const handle = await createFileWithContents( 71 'trunc_smaller_offset.txt', '1234567890', root); 72 const stream = await handle.createWritable({keepExistingData: true}); 73 74 await stream.truncate(5); 75 await stream.write('abc'); 76 await stream.close(); 77 78 assert_equals(await getFileContents(handle), 'abc45'); 79 assert_equals(await getFileSize(handle), 5); 80 }, 'cursor position: truncate size > offset'); 81 82 directory_test(async (t, root) => { 83 const handle = await createFileWithContents( 84 'trunc_bigger_offset.txt', '1234567890', root); 85 const stream = await handle.createWritable({keepExistingData: true}); 86 87 await stream.seek(6); 88 await stream.truncate(5); 89 await stream.write('abc'); 90 await stream.close(); 91 92 assert_equals(await getFileContents(handle), '12345abc'); 93 assert_equals(await getFileSize(handle), 8); 94 }, 'cursor position: truncate size < offset'); 95 96 directory_test(async (t, root) => { 97 const handle = await createEmptyFile('contents', root); 98 const stream = await handle.createWritable(); 99 assert_false(stream.locked); 100 101 stream.write('abc'); 102 assert_false(stream.locked); 103 stream.write('def'); 104 assert_false(stream.locked); 105 stream.truncate(9); 106 assert_false(stream.locked); 107 stream.seek(0); 108 assert_false(stream.locked); 109 stream.write('xyz'); 110 assert_false(stream.locked); 111 await stream.close(); 112 113 assert_equals(await getFileContents(handle), 'xyzdef\0\0\0'); 114 assert_equals(await getFileSize(handle), 9); 115 }, 'commands are queued, stream is unlocked after each operation');