Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,18 @@ export class ProcessOutput extends Error {
return this._combined
}

json() {
return JSON.parse(this._combined)
}

buffer() {
return Buffer.from(this._combined, 'utf8')
}

text() {
return this._combined
}

valueOf() {
return this._combined.trim()
}
Expand Down
15 changes: 15 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ describe('core', () => {
assert.equal((await p).toString(), 'foo\n')
})

test('ProcessPromise: implements json()', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ProcessPromise here but ProcessOutput.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for text() and buffer()? I just followed toString() test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antongolub should I also change ProcessOutput for toString() test?

const p = $`echo '{"key":"value"}'`
assert.deepEqual((await p).json(), { key: 'value' })
})

test('ProcessPromise: implements text()', async () => {
const p = $`echo foo`
assert.equal((await p).toString(), 'foo\n')
})

test('ProcessPromise: implements buffer()', async () => {
const p = $`echo foo`
assert.equal((await p).buffer().compare(Buffer.from('foo\n', 'utf-8')), 0)
})

test('ProcessPromise: implements valueOf()', async () => {
const p = $`echo foo`
assert.equal((await p).valueOf(), 'foo')
Expand Down