11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci// The following tests validate base functionality for the fs.promises 61cb0ef41Sopenharmony_ci// FileHandle.writeFile method. 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst fs = require('fs'); 91cb0ef41Sopenharmony_ciconst { open, writeFile } = fs.promises; 101cb0ef41Sopenharmony_ciconst path = require('path'); 111cb0ef41Sopenharmony_ciconst { Readable } = require('stream'); 121cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 131cb0ef41Sopenharmony_ciconst assert = require('assert'); 141cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_citmpdir.refresh(); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciasync function validateWriteFile() { 191cb0ef41Sopenharmony_ci const filePathForHandle = path.resolve(tmpDir, 'tmp-write-file2.txt'); 201cb0ef41Sopenharmony_ci const fileHandle = await open(filePathForHandle, 'w+'); 211cb0ef41Sopenharmony_ci try { 221cb0ef41Sopenharmony_ci const buffer = Buffer.from('Hello world'.repeat(100), 'utf8'); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci await fileHandle.writeFile(buffer); 251cb0ef41Sopenharmony_ci const readFileData = fs.readFileSync(filePathForHandle); 261cb0ef41Sopenharmony_ci assert.deepStrictEqual(buffer, readFileData); 271cb0ef41Sopenharmony_ci } finally { 281cb0ef41Sopenharmony_ci await fileHandle.close(); 291cb0ef41Sopenharmony_ci } 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci// Signal aborted while writing file 331cb0ef41Sopenharmony_ciasync function doWriteAndCancel() { 341cb0ef41Sopenharmony_ci const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt'); 351cb0ef41Sopenharmony_ci const fileHandle = await open(filePathForHandle, 'w+'); 361cb0ef41Sopenharmony_ci try { 371cb0ef41Sopenharmony_ci const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8'); 381cb0ef41Sopenharmony_ci const controller = new AbortController(); 391cb0ef41Sopenharmony_ci const { signal } = controller; 401cb0ef41Sopenharmony_ci process.nextTick(() => controller.abort()); 411cb0ef41Sopenharmony_ci await assert.rejects(writeFile(fileHandle, buffer, { signal }), { 421cb0ef41Sopenharmony_ci name: 'AbortError' 431cb0ef41Sopenharmony_ci }); 441cb0ef41Sopenharmony_ci } finally { 451cb0ef41Sopenharmony_ci await fileHandle.close(); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciconst dest = path.resolve(tmpDir, 'tmp.txt'); 501cb0ef41Sopenharmony_ciconst otherDest = path.resolve(tmpDir, 'tmp-2.txt'); 511cb0ef41Sopenharmony_ciconst stream = Readable.from(['a', 'b', 'c']); 521cb0ef41Sopenharmony_ciconst stream2 = Readable.from(['ümlaut', ' ', 'sechzig']); 531cb0ef41Sopenharmony_ciconst iterable = { 541cb0ef41Sopenharmony_ci expected: 'abc', 551cb0ef41Sopenharmony_ci *[Symbol.iterator]() { 561cb0ef41Sopenharmony_ci yield 'a'; 571cb0ef41Sopenharmony_ci yield 'b'; 581cb0ef41Sopenharmony_ci yield 'c'; 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci}; 611cb0ef41Sopenharmony_cifunction iterableWith(value) { 621cb0ef41Sopenharmony_ci return { 631cb0ef41Sopenharmony_ci *[Symbol.iterator]() { 641cb0ef41Sopenharmony_ci yield value; 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci }; 671cb0ef41Sopenharmony_ci} 681cb0ef41Sopenharmony_ciconst bufferIterable = { 691cb0ef41Sopenharmony_ci expected: 'abc', 701cb0ef41Sopenharmony_ci *[Symbol.iterator]() { 711cb0ef41Sopenharmony_ci yield Buffer.from('a'); 721cb0ef41Sopenharmony_ci yield Buffer.from('b'); 731cb0ef41Sopenharmony_ci yield Buffer.from('c'); 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci}; 761cb0ef41Sopenharmony_ciconst asyncIterable = { 771cb0ef41Sopenharmony_ci expected: 'abc', 781cb0ef41Sopenharmony_ci async* [Symbol.asyncIterator]() { 791cb0ef41Sopenharmony_ci yield 'a'; 801cb0ef41Sopenharmony_ci yield 'b'; 811cb0ef41Sopenharmony_ci yield 'c'; 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci}; 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ciasync function doWriteStream() { 861cb0ef41Sopenharmony_ci const fileHandle = await open(dest, 'w+'); 871cb0ef41Sopenharmony_ci try { 881cb0ef41Sopenharmony_ci await fileHandle.writeFile(stream); 891cb0ef41Sopenharmony_ci const expected = 'abc'; 901cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 911cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, expected); 921cb0ef41Sopenharmony_ci } finally { 931cb0ef41Sopenharmony_ci await fileHandle.close(); 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci} 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ciasync function doWriteStreamWithCancel() { 981cb0ef41Sopenharmony_ci const controller = new AbortController(); 991cb0ef41Sopenharmony_ci const { signal } = controller; 1001cb0ef41Sopenharmony_ci process.nextTick(() => controller.abort()); 1011cb0ef41Sopenharmony_ci const fileHandle = await open(otherDest, 'w+'); 1021cb0ef41Sopenharmony_ci try { 1031cb0ef41Sopenharmony_ci await assert.rejects( 1041cb0ef41Sopenharmony_ci fileHandle.writeFile(stream, { signal }), 1051cb0ef41Sopenharmony_ci { name: 'AbortError' } 1061cb0ef41Sopenharmony_ci ); 1071cb0ef41Sopenharmony_ci } finally { 1081cb0ef41Sopenharmony_ci await fileHandle.close(); 1091cb0ef41Sopenharmony_ci } 1101cb0ef41Sopenharmony_ci} 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ciasync function doWriteIterable() { 1131cb0ef41Sopenharmony_ci const fileHandle = await open(dest, 'w+'); 1141cb0ef41Sopenharmony_ci try { 1151cb0ef41Sopenharmony_ci await fileHandle.writeFile(iterable); 1161cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 1171cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, iterable.expected); 1181cb0ef41Sopenharmony_ci } finally { 1191cb0ef41Sopenharmony_ci await fileHandle.close(); 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci} 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ciasync function doWriteInvalidIterable() { 1241cb0ef41Sopenharmony_ci const fileHandle = await open(dest, 'w+'); 1251cb0ef41Sopenharmony_ci try { 1261cb0ef41Sopenharmony_ci await Promise.all( 1271cb0ef41Sopenharmony_ci [42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) => 1281cb0ef41Sopenharmony_ci assert.rejects( 1291cb0ef41Sopenharmony_ci fileHandle.writeFile(iterableWith(value)), 1301cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' } 1311cb0ef41Sopenharmony_ci ) 1321cb0ef41Sopenharmony_ci ) 1331cb0ef41Sopenharmony_ci ); 1341cb0ef41Sopenharmony_ci } finally { 1351cb0ef41Sopenharmony_ci await fileHandle.close(); 1361cb0ef41Sopenharmony_ci } 1371cb0ef41Sopenharmony_ci} 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ciasync function doWriteIterableWithEncoding() { 1401cb0ef41Sopenharmony_ci const fileHandle = await open(dest, 'w+'); 1411cb0ef41Sopenharmony_ci try { 1421cb0ef41Sopenharmony_ci await fileHandle.writeFile(stream2, 'latin1'); 1431cb0ef41Sopenharmony_ci const expected = 'ümlaut sechzig'; 1441cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'latin1'); 1451cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, expected); 1461cb0ef41Sopenharmony_ci } finally { 1471cb0ef41Sopenharmony_ci await fileHandle.close(); 1481cb0ef41Sopenharmony_ci } 1491cb0ef41Sopenharmony_ci} 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ciasync function doWriteBufferIterable() { 1521cb0ef41Sopenharmony_ci const fileHandle = await open(dest, 'w+'); 1531cb0ef41Sopenharmony_ci try { 1541cb0ef41Sopenharmony_ci await fileHandle.writeFile(bufferIterable); 1551cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 1561cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, bufferIterable.expected); 1571cb0ef41Sopenharmony_ci } finally { 1581cb0ef41Sopenharmony_ci await fileHandle.close(); 1591cb0ef41Sopenharmony_ci } 1601cb0ef41Sopenharmony_ci} 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ciasync function doWriteAsyncIterable() { 1631cb0ef41Sopenharmony_ci const fileHandle = await open(dest, 'w+'); 1641cb0ef41Sopenharmony_ci try { 1651cb0ef41Sopenharmony_ci await fileHandle.writeFile(asyncIterable); 1661cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 1671cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, asyncIterable.expected); 1681cb0ef41Sopenharmony_ci } finally { 1691cb0ef41Sopenharmony_ci await fileHandle.close(); 1701cb0ef41Sopenharmony_ci } 1711cb0ef41Sopenharmony_ci} 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ciasync function doWriteInvalidValues() { 1741cb0ef41Sopenharmony_ci const fileHandle = await open(dest, 'w+'); 1751cb0ef41Sopenharmony_ci try { 1761cb0ef41Sopenharmony_ci await Promise.all( 1771cb0ef41Sopenharmony_ci [42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) => 1781cb0ef41Sopenharmony_ci assert.rejects( 1791cb0ef41Sopenharmony_ci fileHandle.writeFile(value), 1801cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_ARG_TYPE' } 1811cb0ef41Sopenharmony_ci ) 1821cb0ef41Sopenharmony_ci ) 1831cb0ef41Sopenharmony_ci ); 1841cb0ef41Sopenharmony_ci } finally { 1851cb0ef41Sopenharmony_ci await fileHandle.close(); 1861cb0ef41Sopenharmony_ci } 1871cb0ef41Sopenharmony_ci} 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci(async () => { 1901cb0ef41Sopenharmony_ci await validateWriteFile(); 1911cb0ef41Sopenharmony_ci await doWriteAndCancel(); 1921cb0ef41Sopenharmony_ci await doWriteStream(); 1931cb0ef41Sopenharmony_ci await doWriteStreamWithCancel(); 1941cb0ef41Sopenharmony_ci await doWriteIterable(); 1951cb0ef41Sopenharmony_ci await doWriteInvalidIterable(); 1961cb0ef41Sopenharmony_ci await doWriteIterableWithEncoding(); 1971cb0ef41Sopenharmony_ci await doWriteBufferIterable(); 1981cb0ef41Sopenharmony_ci await doWriteAsyncIterable(); 1991cb0ef41Sopenharmony_ci await doWriteInvalidValues(); 2001cb0ef41Sopenharmony_ci})().then(common.mustCall()); 201