11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst fs = require('fs'); 51cb0ef41Sopenharmony_ciconst { join } = require('path'); 61cb0ef41Sopenharmony_ciconst readline = require('readline'); 71cb0ef41Sopenharmony_ciconst assert = require('assert'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 101cb0ef41Sopenharmony_citmpdir.refresh(); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst filename = join(tmpdir.path, 'test.txt'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst testContents = [ 151cb0ef41Sopenharmony_ci '', 161cb0ef41Sopenharmony_ci '\n', 171cb0ef41Sopenharmony_ci 'line 1', 181cb0ef41Sopenharmony_ci 'line 1\nline 2 南越国是前203年至前111年存在于岭南地区的一个国家\nline 3\ntrailing', 191cb0ef41Sopenharmony_ci 'line 1\nline 2\nline 3 ends with newline\n', 201cb0ef41Sopenharmony_ci]; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciasync function testSimple() { 231cb0ef41Sopenharmony_ci for (const fileContent of testContents) { 241cb0ef41Sopenharmony_ci fs.writeFileSync(filename, fileContent); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci const readable = fs.createReadStream(filename); 271cb0ef41Sopenharmony_ci const rli = readline.createInterface({ 281cb0ef41Sopenharmony_ci input: readable, 291cb0ef41Sopenharmony_ci crlfDelay: Infinity 301cb0ef41Sopenharmony_ci }); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci const iteratedLines = []; 331cb0ef41Sopenharmony_ci for await (const k of rli) { 341cb0ef41Sopenharmony_ci iteratedLines.push(k); 351cb0ef41Sopenharmony_ci } 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci const expectedLines = fileContent.split('\n'); 381cb0ef41Sopenharmony_ci if (expectedLines[expectedLines.length - 1] === '') { 391cb0ef41Sopenharmony_ci expectedLines.pop(); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci assert.deepStrictEqual(iteratedLines, expectedLines); 421cb0ef41Sopenharmony_ci assert.strictEqual(iteratedLines.join(''), fileContent.replace(/\n/g, '')); 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciasync function testMutual() { 471cb0ef41Sopenharmony_ci for (const fileContent of testContents) { 481cb0ef41Sopenharmony_ci fs.writeFileSync(filename, fileContent); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci const readable = fs.createReadStream(filename); 511cb0ef41Sopenharmony_ci const rli = readline.createInterface({ 521cb0ef41Sopenharmony_ci input: readable, 531cb0ef41Sopenharmony_ci crlfDelay: Infinity 541cb0ef41Sopenharmony_ci }); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci const expectedLines = fileContent.split('\n'); 571cb0ef41Sopenharmony_ci if (expectedLines[expectedLines.length - 1] === '') { 581cb0ef41Sopenharmony_ci expectedLines.pop(); 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci const iteratedLines = []; 611cb0ef41Sopenharmony_ci let iterated = false; 621cb0ef41Sopenharmony_ci for await (const k of rli) { 631cb0ef41Sopenharmony_ci // This outer loop should only iterate once. 641cb0ef41Sopenharmony_ci assert.strictEqual(iterated, false); 651cb0ef41Sopenharmony_ci iterated = true; 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci iteratedLines.push(k); 681cb0ef41Sopenharmony_ci for await (const l of rli) { 691cb0ef41Sopenharmony_ci iteratedLines.push(l); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci assert.deepStrictEqual(iteratedLines, expectedLines); 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci assert.deepStrictEqual(iteratedLines, expectedLines); 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci} 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_citestSimple().then(testMutual).then(common.mustCall()); 78