mirror of
https://github.com/antonmedv/fx.git
synced 2024-10-26 20:24:05 +03:00
Fix a bug with --raw flag about reading all lines if no newline is present at the end of the file
This commit is contained in:
parent
6793ff4a0e
commit
e465f7d2c8
@ -290,6 +290,7 @@ function* readLine(stdin) {
|
|||||||
buffer += ch
|
buffer += ch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (buffer.length > 0) yield buffer
|
||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
npm/test.js
13
npm/test.js
@ -10,14 +10,14 @@ async function test(name, fn) {
|
|||||||
|
|
||||||
async function run(json, code = '') {
|
async function run(json, code = '') {
|
||||||
const {spawnSync} = await import('node:child_process')
|
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',
|
stdio: 'pipe',
|
||||||
encoding: 'utf8',
|
encoding: 'utf8',
|
||||||
shell: true
|
shell: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runSimple(code = '') {
|
async function runNoPipe(code = '') {
|
||||||
const {spawnSync} = await import('node:child_process')
|
const {spawnSync} = await import('node:child_process')
|
||||||
return spawnSync(`node index.js ${code}`, {
|
return spawnSync(`node index.js ${code}`, {
|
||||||
stdio: 'pipe',
|
stdio: 'pipe',
|
||||||
@ -197,6 +197,11 @@ void async function main() {
|
|||||||
t.equal(stdout, 'string\n')
|
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 => {
|
await test('flags - slurp flag', async t => {
|
||||||
const {stdout} = await run('{"foo": "bar"}\n{"foo": "baz"}', `-s '.[1].foo'`)
|
const {stdout} = await run('{"foo": "bar"}\n{"foo": "baz"}', `-s '.[1].foo'`)
|
||||||
t.equal(stdout, 'baz\n')
|
t.equal(stdout, 'baz\n')
|
||||||
@ -208,12 +213,12 @@ void async function main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
await test('cli - first arg is file', async t => {
|
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')
|
t.equal(stdout, 'fx\n')
|
||||||
})
|
})
|
||||||
|
|
||||||
await test('cli - last arg is file', async t => {
|
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')
|
t.equal(stdout, 'fx\n')
|
||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
|
Loading…
Reference in New Issue
Block a user