Skip to content

fix(reporters): write buffered stdout/stderr on process exit #6932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2024
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
24 changes: 0 additions & 24 deletions packages/vitest/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1245,30 +1245,6 @@ Repository: git://github.com/feross/run-parallel.git

---------------------------------------

## signal-exit
License: ISC
By: Ben Coe
Repository: https://github.com/tapjs/signal-exit.git

> The ISC License
>
> Copyright (c) 2015, Contributors
>
> Permission to use, copy, modify, and/or distribute this software
> for any purpose with or without fee is hereby granted, provided
> that the above copyright notice and this permission notice
> appear in all copies.
>
> THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
> OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
> LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
> OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
> WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
> ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

---------------------------------------

## sisteransi
License: MIT
By: Terkel Gjervig
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
"expect-type": "^1.1.0",
"magic-string": "^0.30.12",
"pathe": "^1.1.2",
"restore-cursor": "^5.1.0",
"std-env": "^3.8.0",
"tinybench": "^2.9.0",
"tinyexec": "^0.3.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export abstract class BaseReporter implements Reporter {
}

else if (this.renderSucceed || anyFailed) {
this.log(` ${c.green(c.dim(F_CHECK))} ${getTestName(test, c.dim(' > '))}`)
this.log(` ${c.dim(getStateSymbol(test))} ${getTestName(test, c.dim(' > '))}`)
}
}
}
Expand Down
29 changes: 27 additions & 2 deletions packages/vitest/src/node/reporters/renderers/windowedRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Writable } from 'node:stream'
import type { Vitest } from '../../core'
import { stripVTControlCharacters } from 'node:util'
import restoreCursor from 'restore-cursor'

const DEFAULT_RENDER_INTERVAL = 16

Expand Down Expand Up @@ -49,9 +48,9 @@ export class WindowRenderer {
this.cleanups.push(
this.interceptStream(process.stdout, 'output'),
this.interceptStream(process.stderr, 'error'),
this.addProcessExitListeners(),
)

restoreCursor()
this.write(HIDE_CURSOR, 'output')

this.start()
Expand Down Expand Up @@ -175,6 +174,32 @@ export class WindowRenderer {
private write(message: string, type: 'output' | 'error' = 'output') {
(this.streams[type] as Writable['write'])(message)
}

private addProcessExitListeners() {
Copy link
Member Author

@AriPerkkio AriPerkkio Nov 21, 2024

Choose a reason for hiding this comment

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

At this point it's ok to have this here in windowedRenderer. But later on it might make sense to move this to logger.ts instead. 🤔

So that logger.ts would handle cursor hiding and showing. It shoud also expose callback that reporters could hook into, so that windowedRenderer could call flushBuffer on exit too.

const onExit = (signal?: string | number, exitCode?: number) => {
// Write buffered content on unexpected exits, e.g. direct `process.exit()` calls
this.flushBuffer()
this.stop()

// Interrupted signals don't set exit code automatically.
// Use same exit code as node: https://nodejs.org/api/process.html#signal-events
if (process.exitCode === undefined) {
process.exitCode = exitCode !== undefined ? (128 + exitCode) : Number(signal)
}

process.exit()
}

process.once('SIGINT', onExit)
process.once('SIGTERM', onExit)
process.once('exit', onExit)

return function cleanup() {
process.off('SIGINT', onExit)
process.off('SIGTERM', onExit)
process.off('exit', onExit)
}
}
}

/** Calculate the actual row count needed to render `rows` into `stream` */
Expand Down
26 changes: 0 additions & 26 deletions pnpm-lock.yaml

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