Skip to content

Commit

Permalink
feat: switch stdioString default to true (#48)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: stdout and stderr will now be returned as strings by default, for buffers set stdioString to false
  • Loading branch information
nlf committed Nov 1, 2022
1 parent a854057 commit 6ab90b8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 71 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -11,7 +11,7 @@ const promiseSpawn = require('@npmcli/promise-spawn')

promiseSpawn('ls', [ '-laF', 'some/dir/*.js' ], {
cwd: '/tmp/some/path', // defaults to process.cwd()
stdioString: false, // stdout/stderr as strings rather than buffers
stdioString: true, // stdout/stderr as strings rather than buffers
stdio: 'pipe', // any node spawn stdio arg is valid here
// any other arguments to node child_process.spawn can go here as well,
}, {
Expand Down Expand Up @@ -49,7 +49,7 @@ spawned process.

#### Options

- `stdioString` Boolean, default `false`. Return stdout/stderr output as
- `stdioString` Boolean, default `true`. Return stdout/stderr output as
strings rather than buffers.
- `cwd` String, default `process.cwd()`. Current working directory for
running the script. Also the argument to `infer-owner` to determine
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -168,7 +168,7 @@ const isPipe = (stdio = 'pipe', fd) => {
return false
}

const stdioResult = (stdout, stderr, { stdioString, stdio }) => {
const stdioResult = (stdout, stderr, { stdioString = true, stdio }) => {
const result = {
stdout: null,
stderr: null,
Expand Down
100 changes: 50 additions & 50 deletions test/index.js
Expand Up @@ -10,16 +10,16 @@ t.afterEach(() => {
spawk.clean()
})

t.test('defaults to returning buffers', async (t) => {
t.test('defaults to returning strings', async (t) => {
const proc = spawk.spawn('pass', [], {})
.stdout(Buffer.from('OK\n'))

const result = await promiseSpawn('pass', [])
t.hasStrict(result, {
code: 0,
signal: null,
stdout: Buffer.from('OK\n'),
stderr: Buffer.from(''),
stdout: 'OK',
stderr: '',
})

t.ok(proc.called)
Expand All @@ -33,24 +33,24 @@ t.test('extra context is returned', async (t) => {
t.hasStrict(result, {
code: 0,
signal: null,
stdout: Buffer.from('OK\n'),
stderr: Buffer.from(''),
stdout: 'OK',
stderr: '',
extra: 'property',
})

t.ok(proc.called)
})

t.test('stdioString returns trimmed strings', async (t) => {
t.test('stdioString false returns buffers', async (t) => {
const proc = spawk.spawn('pass', [], {})
.stdout(Buffer.from('OK\n'))

const result = await promiseSpawn('pass', [], { stdioString: true })
const result = await promiseSpawn('pass', [], { stdioString: false })
t.hasStrict(result, {
code: 0,
signal: null,
stdout: 'OK',
stderr: '',
stdout: Buffer.from('OK\n'),
stderr: Buffer.from(''),
})

t.ok(proc.called)
Expand All @@ -71,11 +71,11 @@ t.test('stdout and stderr are null when stdio is inherit', async (t) => {
t.ok(proc.called)
})

t.test('stdout and stderr are null when stdio is inherit and stdioString is set', async (t) => {
t.test('stdout and stderr are null when stdio is inherit and stdioString is false', async (t) => {
const proc = spawk.spawn('pass', [], { stdio: 'inherit' })
.stdout(Buffer.from('OK\n'))

const result = await promiseSpawn('pass', [], { stdio: 'inherit', stdioString: true })
const result = await promiseSpawn('pass', [], { stdio: 'inherit', stdioString: false })
t.hasStrict(result, {
code: 0,
signal: null,
Expand All @@ -95,7 +95,7 @@ t.test('stdout is null when stdio is [pipe, inherit, pipe]', async (t) => {
code: 0,
signal: null,
stdout: null,
stderr: Buffer.from(''),
stderr: '',
})

t.ok(proc.called)
Expand All @@ -109,7 +109,7 @@ t.test('stderr is null when stdio is [pipe, pipe, inherit]', async (t) => {
t.hasStrict(result, {
code: 0,
signal: null,
stdout: Buffer.from('OK\n'),
stdout: 'OK',
stderr: null,
})

Expand All @@ -128,8 +128,8 @@ t.test('exposes stdin', async (t) => {
t.hasStrict(result, {
code: 0,
signal: null,
stdout: Buffer.from('hello'),
stderr: Buffer.from(''),
stdout: 'hello',
stderr: '',
})

t.ok(proc.called)
Expand All @@ -147,15 +147,15 @@ t.test('exposes process', async (t) => {
await t.rejects(p, {
code: 1,
signal: null,
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
})
} else {
await t.rejects(p, {
code: null,
signal: 'SIGFAKE',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
})
}

Expand All @@ -168,8 +168,8 @@ t.test('rejects when spawn errors', async (t) => {

await t.rejects(promiseSpawn('notfound', []), {
message: 'command not found',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
})

t.ok(proc.called)
Expand All @@ -181,8 +181,8 @@ t.test('spawn error includes extra', async (t) => {

await t.rejects(promiseSpawn('notfound', [], {}, { extra: 'property' }), {
message: 'command not found',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
extra: 'property',
})

Expand All @@ -193,10 +193,10 @@ t.test('spawn error respects stdioString', async (t) => {
const proc = spawk.spawn('notfound', [], {})
.spawnError(new Error('command not found'))

await t.rejects(promiseSpawn('notfound', [], { stdioString: true }), {
await t.rejects(promiseSpawn('notfound', [], { stdioString: false }), {
message: 'command not found',
stdout: '',
stderr: '',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
})

t.ok(proc.called)
Expand All @@ -223,8 +223,8 @@ t.test('rejects when command fails', async (t) => {
await t.rejects(promiseSpawn('fail', []), {
message: 'command failed',
code: 1,
stdout: Buffer.from(''),
stderr: Buffer.from('Error!\n'),
stdout: '',
stderr: 'Error!',
})

t.ok(proc.called)
Expand All @@ -238,8 +238,8 @@ t.test('failed command returns extra', async (t) => {
await t.rejects(promiseSpawn('fail', [], {}, { extra: 'property' }), {
message: 'command failed',
code: 1,
stdout: Buffer.from(''),
stderr: Buffer.from('Error!\n'),
stdout: '',
stderr: 'Error!',
extra: 'property',
})

Expand All @@ -251,11 +251,11 @@ t.test('failed command respects stdioString', async (t) => {
.stderr(Buffer.from('Error!\n'))
.exit(1)

await t.rejects(promiseSpawn('fail', [], { stdioString: true }), {
await t.rejects(promiseSpawn('fail', [], { stdioString: false }), {
message: 'command failed',
code: 1,
stdout: '',
stderr: 'Error!',
stdout: Buffer.from(''),
stderr: Buffer.from('Error!\n'),
})

t.ok(proc.called)
Expand Down Expand Up @@ -286,15 +286,15 @@ t.test('rejects when signal kills child', async (t) => {
await t.rejects(p, {
code: 1,
signal: null,
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
})
} else {
await t.rejects(p, {
code: null,
signal: 'SIGFAKE',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
})
}

Expand All @@ -311,16 +311,16 @@ t.test('signal death includes extra', async (t) => {
await t.rejects(p, {
code: 1,
signal: null,
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
extra: 'property',
})
} else {
await t.rejects(p, {
code: null,
signal: 'SIGFAKE',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
extra: 'property',
})
}
Expand All @@ -332,21 +332,21 @@ t.test('signal death respects stdioString', async (t) => {
const proc = spawk.spawn('signal', [], {})
.signal('SIGFAKE')

const p = promiseSpawn('signal', [], { stdioString: true })
const p = promiseSpawn('signal', [], { stdioString: false })
// there are no signals in windows, so we expect a different result
if (process.platform === 'win32') {
await t.rejects(p, {
code: 1,
signal: null,
stdout: '',
stderr: '',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
})
} else {
await t.rejects(p, {
code: null,
signal: 'SIGFAKE',
stdout: '',
stderr: '',
stdout: Buffer.from(''),
stderr: Buffer.from(''),
})
}

Expand Down Expand Up @@ -388,8 +388,8 @@ t.test('rejects when stdout errors', async (t) => {
message: 'stdout err',
code: null,
signal: null,
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
})

t.ok(proc.called)
Expand All @@ -405,8 +405,8 @@ t.test('rejects when stderr errors', async (t) => {
message: 'stderr err',
code: null,
signal: null,
stdout: Buffer.from(''),
stderr: Buffer.from(''),
stdout: '',
stderr: '',
})

t.ok(proc.called)
Expand Down

0 comments on commit 6ab90b8

Please sign in to comment.