Skip to content

Commit

Permalink
Fix a bug with --raw flag about reading all lines if no newline is pr…
Browse files Browse the repository at this point in the history
…esent at the end of the file
  • Loading branch information
antonmedv committed Jun 27, 2024
1 parent 6793ff4 commit e465f7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions npm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions npm/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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')
Expand All @@ -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')
})
}()

0 comments on commit e465f7d

Please sign in to comment.