Skip to content

Commit

Permalink
feat: add duration to ProcessOutput (#892)
Browse files Browse the repository at this point in the history
* feat: add duration to `ProcessOutput`

* test: check p duration after kill

---------

Co-authored-by: Anton Medvedev <[email protected]>
  • Loading branch information
antongolub and antonmedv authored Sep 15, 2024
1 parent 5d78a26 commit 4081a9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
// Stderr should be printed regardless of piping.
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
},
end: ({ error, stdout, stderr, stdall, status, signal }, c) => {
end: (
{ error, stdout, stderr, stdall, status, signal, duration },
c
) => {
self._resolved = true

// Ensures EOL
Expand All @@ -336,7 +339,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
stdout,
stderr,
stdall,
message
message,
duration
)
self._output = output
self._reject(output)
Expand All @@ -353,7 +357,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
stdout,
stderr,
stdall,
message
message,
duration
)
self._output = output
if (status === 0 || self.isNothrow()) {
Expand Down Expand Up @@ -567,21 +572,24 @@ export class ProcessOutput extends Error {
private readonly _stdout: string
private readonly _stderr: string
private readonly _combined: string
private readonly _duration: number

constructor(
code: number | null,
signal: NodeJS.Signals | null,
stdout: string,
stderr: string,
combined: string,
message: string
message: string,
duration: number = 0
) {
super(message)
this._code = code
this._signal = signal
this._stdout = stdout
this._stderr = stderr
this._combined = combined
this._duration = duration
}

toString() {
Expand Down Expand Up @@ -634,6 +642,10 @@ export class ProcessOutput extends Error {
return this._signal
}

get duration() {
return this._duration
}

static getExitMessage(
code: number | null,
signal: NodeJS.Signals | null,
Expand Down Expand Up @@ -674,7 +686,8 @@ export class ProcessOutput extends Error {
exitCodeInfo(this.exitCode)
? chalk.grey(' (' + exitCodeInfo(this.exitCode) + ')')
: ''
}
},
duration: ${this.duration}
}`
}
}
Expand All @@ -698,7 +711,7 @@ export function cd(dir: string | ProcessOutput) {
}

export async function kill(pid: number, signal = $.killSignal) {
let children = await ps.tree({ pid, recursive: true })
const children = await ps.tree({ pid, recursive: true })
for (const p of children) {
try {
process.kill(+p.pid, signal)
Expand Down
16 changes: 14 additions & 2 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,13 @@ describe('core', () => {

describe('kill()', () => {
test('just works', async () => {
let p = $`sleep 999`.nothrow()
const p = $`sleep 999`.nothrow()
setTimeout(() => {
p.kill()
}, 100)
await p
const o = await p
assert.equal(o.signal, 'SIGTERM')
assert.ok(o.duration >= 100 && o.duration < 1000)
})

test('a signal is passed with kill() method', async () => {
Expand Down Expand Up @@ -608,6 +610,16 @@ describe('core', () => {
})

describe('ProcessOutput', () => {
test('getters', async () => {
const o = new ProcessOutput(-1, 'SIGTERM', '', '', 'foo\n', 'msg', 20)

assert.equal(o.stdout, '')
assert.equal(o.stderr, '')
assert.equal(o.signal, 'SIGTERM')
assert.equal(o.exitCode, -1)
assert.equal(o.duration, 20)
})

test('toString()', async () => {
const o = new ProcessOutput(null, null, '', '', 'foo\n')
assert.equal(o.toString(), 'foo\n')
Expand Down

0 comments on commit 4081a9f

Please sign in to comment.