From e465f7d2c82fcf96f9ae835d561d1f23ebc7332b Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Thu, 27 Jun 2024 11:36:17 +0200 Subject: [PATCH] Fix a bug with --raw flag about reading all lines if no newline is present at the end of the file --- npm/index.js | 1 + npm/test.js | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/npm/index.js b/npm/index.js index 76e0b60..2957a6b 100644 --- a/npm/index.js +++ b/npm/index.js @@ -290,6 +290,7 @@ function* readLine(stdin) { buffer += ch } } + if (buffer.length > 0) yield buffer return buffer } diff --git a/npm/test.js b/npm/test.js index fb358af..bf096b5 100644 --- a/npm/test.js +++ b/npm/test.js @@ -10,14 +10,14 @@ async function test(name, fn) { async function run(json, code = '') { const {spawnSync} = await import('node:child_process') - return spawnSync(`echo '${typeof json === 'string' ? json : JSON.stringify(json)}' | node index.js ${code}`, { + return spawnSync(`printf -- '${typeof json === 'string' ? json : JSON.stringify(json)}' | node index.js ${code}`, { stdio: 'pipe', encoding: 'utf8', shell: true }) } -async function runSimple(code = '') { +async function runNoPipe(code = '') { const {spawnSync} = await import('node:child_process') return spawnSync(`node index.js ${code}`, { stdio: 'pipe', @@ -197,6 +197,11 @@ void async function main() { t.equal(stdout, 'string\n') }) + await test('flags - raw reads entire input', async t => { + const {stdout} = await run('foo\bbar', `-r`) + t.equal(stdout, 'foo\bbar\n') + }) + await test('flags - slurp flag', async t => { const {stdout} = await run('{"foo": "bar"}\n{"foo": "baz"}', `-s '.[1].foo'`) t.equal(stdout, 'baz\n') @@ -208,12 +213,12 @@ void async function main() { }) await test('cli - first arg is file', async t => { - const {stdout} = await runSimple(`package.json .name`) + const {stdout} = await runNoPipe(`package.json .name`) t.equal(stdout, 'fx\n') }) await test('cli - last arg is file', async t => { - const {stdout} = await runSimple(`.name package.json`) + const {stdout} = await runNoPipe(`.name package.json`) t.equal(stdout, 'fx\n') }) }()