11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst fs = require('fs'); 51cb0ef41Sopenharmony_ciconst fsPromises = fs.promises; 61cb0ef41Sopenharmony_ciconst path = require('path'); 71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path; 101cb0ef41Sopenharmony_ciconst { Readable } = require('stream'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_citmpdir.refresh(); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst dest = path.resolve(tmpDir, 'tmp.txt'); 151cb0ef41Sopenharmony_ciconst otherDest = path.resolve(tmpDir, 'tmp-2.txt'); 161cb0ef41Sopenharmony_ciconst buffer = Buffer.from('abc'.repeat(1000)); 171cb0ef41Sopenharmony_ciconst buffer2 = Buffer.from('xyz'.repeat(1000)); 181cb0ef41Sopenharmony_ciconst stream = Readable.from(['a', 'b', 'c']); 191cb0ef41Sopenharmony_ciconst stream2 = Readable.from(['ümlaut', ' ', 'sechzig']); 201cb0ef41Sopenharmony_ciconst iterable = { 211cb0ef41Sopenharmony_ci expected: 'abc', 221cb0ef41Sopenharmony_ci *[Symbol.iterator]() { 231cb0ef41Sopenharmony_ci yield 'a'; 241cb0ef41Sopenharmony_ci yield 'b'; 251cb0ef41Sopenharmony_ci yield 'c'; 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci}; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciconst veryLargeBuffer = { 301cb0ef41Sopenharmony_ci expected: 'dogs running'.repeat(512 * 1024), 311cb0ef41Sopenharmony_ci *[Symbol.iterator]() { 321cb0ef41Sopenharmony_ci yield Buffer.from('dogs running'.repeat(512 * 1024), 'utf8'); 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci}; 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cifunction iterableWith(value) { 371cb0ef41Sopenharmony_ci return { 381cb0ef41Sopenharmony_ci *[Symbol.iterator]() { 391cb0ef41Sopenharmony_ci yield value; 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci }; 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ciconst bufferIterable = { 441cb0ef41Sopenharmony_ci expected: 'abc', 451cb0ef41Sopenharmony_ci *[Symbol.iterator]() { 461cb0ef41Sopenharmony_ci yield Buffer.from('a'); 471cb0ef41Sopenharmony_ci yield Buffer.from('b'); 481cb0ef41Sopenharmony_ci yield Buffer.from('c'); 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci}; 511cb0ef41Sopenharmony_ciconst asyncIterable = { 521cb0ef41Sopenharmony_ci expected: 'abc', 531cb0ef41Sopenharmony_ci async* [Symbol.asyncIterator]() { 541cb0ef41Sopenharmony_ci yield 'a'; 551cb0ef41Sopenharmony_ci yield 'b'; 561cb0ef41Sopenharmony_ci yield 'c'; 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci}; 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciasync function doWrite() { 611cb0ef41Sopenharmony_ci await fsPromises.writeFile(dest, buffer); 621cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest); 631cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, buffer); 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciasync function doWriteStream() { 671cb0ef41Sopenharmony_ci await fsPromises.writeFile(dest, stream); 681cb0ef41Sopenharmony_ci const expected = 'abc'; 691cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 701cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, expected); 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciasync function doWriteStreamWithCancel() { 741cb0ef41Sopenharmony_ci const controller = new AbortController(); 751cb0ef41Sopenharmony_ci const { signal } = controller; 761cb0ef41Sopenharmony_ci process.nextTick(() => controller.abort()); 771cb0ef41Sopenharmony_ci await assert.rejects( 781cb0ef41Sopenharmony_ci fsPromises.writeFile(otherDest, stream, { signal }), 791cb0ef41Sopenharmony_ci { name: 'AbortError' } 801cb0ef41Sopenharmony_ci ); 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ciasync function doWriteIterable() { 841cb0ef41Sopenharmony_ci await fsPromises.writeFile(dest, iterable); 851cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 861cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, iterable.expected); 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ciasync function doWriteInvalidIterable() { 901cb0ef41Sopenharmony_ci await Promise.all( 911cb0ef41Sopenharmony_ci [42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) => 921cb0ef41Sopenharmony_ci assert.rejects(fsPromises.writeFile(dest, iterableWith(value)), { 931cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 941cb0ef41Sopenharmony_ci }) 951cb0ef41Sopenharmony_ci ) 961cb0ef41Sopenharmony_ci ); 971cb0ef41Sopenharmony_ci} 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ciasync function doWriteIterableWithEncoding() { 1001cb0ef41Sopenharmony_ci await fsPromises.writeFile(dest, stream2, 'latin1'); 1011cb0ef41Sopenharmony_ci const expected = 'ümlaut sechzig'; 1021cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'latin1'); 1031cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, expected); 1041cb0ef41Sopenharmony_ci} 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ciasync function doWriteBufferIterable() { 1071cb0ef41Sopenharmony_ci await fsPromises.writeFile(dest, bufferIterable); 1081cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 1091cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, bufferIterable.expected); 1101cb0ef41Sopenharmony_ci} 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ciasync function doWriteAsyncIterable() { 1131cb0ef41Sopenharmony_ci await fsPromises.writeFile(dest, asyncIterable); 1141cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 1151cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, asyncIterable.expected); 1161cb0ef41Sopenharmony_ci} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ciasync function doWriteAsyncLargeIterable() { 1191cb0ef41Sopenharmony_ci await fsPromises.writeFile(dest, veryLargeBuffer); 1201cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest, 'utf-8'); 1211cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, veryLargeBuffer.expected); 1221cb0ef41Sopenharmony_ci} 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ciasync function doWriteInvalidValues() { 1251cb0ef41Sopenharmony_ci await Promise.all( 1261cb0ef41Sopenharmony_ci [42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) => 1271cb0ef41Sopenharmony_ci assert.rejects(fsPromises.writeFile(dest, value), { 1281cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1291cb0ef41Sopenharmony_ci }) 1301cb0ef41Sopenharmony_ci ) 1311cb0ef41Sopenharmony_ci ); 1321cb0ef41Sopenharmony_ci} 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ciasync function doWriteWithCancel() { 1351cb0ef41Sopenharmony_ci const controller = new AbortController(); 1361cb0ef41Sopenharmony_ci const { signal } = controller; 1371cb0ef41Sopenharmony_ci process.nextTick(() => controller.abort()); 1381cb0ef41Sopenharmony_ci await assert.rejects( 1391cb0ef41Sopenharmony_ci fsPromises.writeFile(otherDest, buffer, { signal }), 1401cb0ef41Sopenharmony_ci { name: 'AbortError' } 1411cb0ef41Sopenharmony_ci ); 1421cb0ef41Sopenharmony_ci} 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ciasync function doAppend() { 1451cb0ef41Sopenharmony_ci await fsPromises.appendFile(dest, buffer2, { flag: null }); 1461cb0ef41Sopenharmony_ci const data = fs.readFileSync(dest); 1471cb0ef41Sopenharmony_ci const buf = Buffer.concat([buffer, buffer2]); 1481cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, data); 1491cb0ef41Sopenharmony_ci} 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ciasync function doRead() { 1521cb0ef41Sopenharmony_ci const data = await fsPromises.readFile(dest); 1531cb0ef41Sopenharmony_ci const buf = fs.readFileSync(dest); 1541cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, data); 1551cb0ef41Sopenharmony_ci} 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ciasync function doReadWithEncoding() { 1581cb0ef41Sopenharmony_ci const data = await fsPromises.readFile(dest, 'utf-8'); 1591cb0ef41Sopenharmony_ci const syncData = fs.readFileSync(dest, 'utf-8'); 1601cb0ef41Sopenharmony_ci assert.strictEqual(typeof data, 'string'); 1611cb0ef41Sopenharmony_ci assert.deepStrictEqual(data, syncData); 1621cb0ef41Sopenharmony_ci} 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci(async () => { 1651cb0ef41Sopenharmony_ci await doWrite(); 1661cb0ef41Sopenharmony_ci await doWriteWithCancel(); 1671cb0ef41Sopenharmony_ci await doAppend(); 1681cb0ef41Sopenharmony_ci await doRead(); 1691cb0ef41Sopenharmony_ci await doReadWithEncoding(); 1701cb0ef41Sopenharmony_ci await doWriteStream(); 1711cb0ef41Sopenharmony_ci await doWriteStreamWithCancel(); 1721cb0ef41Sopenharmony_ci await doWriteIterable(); 1731cb0ef41Sopenharmony_ci await doWriteInvalidIterable(); 1741cb0ef41Sopenharmony_ci await doWriteIterableWithEncoding(); 1751cb0ef41Sopenharmony_ci await doWriteBufferIterable(); 1761cb0ef41Sopenharmony_ci await doWriteAsyncIterable(); 1771cb0ef41Sopenharmony_ci await doWriteAsyncLargeIterable(); 1781cb0ef41Sopenharmony_ci await doWriteInvalidValues(); 1791cb0ef41Sopenharmony_ci})().then(common.mustCall()); 180