Skip to content

Commit

Permalink
fix: colors on forks pool
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 17, 2024
1 parent 4e60333 commit 2a09cd5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Vitest } from './core'
import type { TestProject } from './project'
import type { TestSpecification } from './spec'
import type { BuiltinPool, Pool } from './types/pool-options'
import { isatty } from 'node:tty'
import mm from 'micromatch'
import { isWindows } from '../utils/env'
import { createForksPool } from './pools/forks'
Expand Down Expand Up @@ -124,6 +125,7 @@ export function createPool(ctx: Vitest): ProcessPool {
VITEST: 'true',
NODE_ENV: process.env.NODE_ENV || 'test',
VITEST_MODE: ctx.config.watch ? 'WATCH' : 'RUN',
FORCE_TTY: isatty(1) ? 'true' : '',
...process.env,
...ctx.config.env,
},
Expand Down
77 changes: 54 additions & 23 deletions test/config/test/console-color.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,63 @@
import { x } from 'tinyexec'
import { expect, test } from 'vitest'

// use "tinyexec" directly since "runVitestCli" strips color
import { runVitest } from '../../test-utils'

test('with color', async () => {
const proc = await x('vitest', ['run', '--root=./fixtures/console-color'], {
nodeOptions: {
env: {
CI: '1',
FORCE_COLOR: '1',
NO_COLOR: undefined,
GITHUB_ACTIONS: undefined,
},
const { stdout } = await runVitest({
root: 'fixtures/console-color',
env: {
CI: '1',
FORCE_COLOR: '1',
NO_COLOR: undefined,
GITHUB_ACTIONS: undefined,
},
})
expect(proc.stdout).toContain('\x1B[33mtrue\x1B[39m\n')
}, undefined, undefined, undefined, { preserveAnsi: true })

expect(stdout).toContain('\x1B[33mtrue\x1B[39m\n')
})

test('without color', async () => {
const proc = await x('vitest', ['run', '--root=./fixtures/console-color'], {
nodeOptions: {
env: {
CI: '1',
FORCE_COLOR: undefined,
NO_COLOR: '1',
GITHUB_ACTIONS: undefined,
},
const { stdout } = await runVitest({
root: 'fixtures/console-color',
env: {
CI: '1',
FORCE_COLOR: undefined,
NO_COLOR: '1',
GITHUB_ACTIONS: undefined,
},
}, undefined, undefined, undefined, { preserveAnsi: true })

expect(stdout).toContain('true\n')
expect(stdout).not.toContain('\x1B[33mtrue\x1B[39m\n')
})

test('without color, forks pool in non-TTY parent', async () => {
const { stdout } = await runVitest({
root: 'fixtures/console-color',
env: {
CI: undefined,
FORCE_COLOR: undefined,
NO_COLOR: undefined,
GITHUB_ACTIONS: undefined,

// Overrides current process's value, since we are running Vitest in Vitest here
FORCE_TTY: undefined,
},
}, undefined, undefined, undefined, { preserveAnsi: true })

expect(stdout).toContain('true\n')
expect(stdout).not.toContain('\x1B[33mtrue\x1B[39m\n')
})

test('with color, forks pool in TTY parent', async () => {
const { stdout } = await runVitest({
root: 'fixtures/console-color',
env: {
CI: undefined,
FORCE_COLOR: undefined,
NO_COLOR: undefined,
GITHUB_ACTIONS: undefined,
},
})
expect(proc.stdout).toContain('true\n')
}, undefined, undefined, undefined, { preserveAnsi: true })

expect(stdout).toContain('\x1B[33mtrue\x1B[39m\n')
})
6 changes: 4 additions & 2 deletions test/test-utils/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export class Cli {
private stdoutListeners: Listener[] = []
private stderrListeners: Listener[] = []
private stdin: ReadableOrWritable
private preserveAnsi?: boolean

constructor(options: { stdin: ReadableOrWritable; stdout: ReadableOrWritable; stderr: ReadableOrWritable }) {
constructor(options: { stdin: ReadableOrWritable; stdout: ReadableOrWritable; stderr: ReadableOrWritable; preserveAnsi?: boolean }) {
this.stdin = options.stdin
this.preserveAnsi = options.preserveAnsi

for (const source of (['stdout', 'stderr'] as const)) {
const stream = options[source]
Expand All @@ -37,7 +39,7 @@ export class Cli {
}

private capture(source: Source, data: any) {
const msg = stripVTControlCharacters(data.toString())
const msg = this.preserveAnsi ? data.toString() : stripVTControlCharacters(data.toString())
this[source] += msg
this[`${source}Listeners`].forEach(fn => fn())
}
Expand Down
3 changes: 2 additions & 1 deletion test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Object.assign(tinyrainbow.default, tinyrainbow.getDefaultColors())
interface VitestRunnerCLIOptions {
std?: 'inherit'
fails?: boolean
preserveAnsi?: boolean
}

export async function runVitest(
Expand Down Expand Up @@ -58,7 +59,7 @@ export async function runVitest(
const stdin = new Readable({ read: () => '' }) as NodeJS.ReadStream
stdin.isTTY = true
stdin.setRawMode = () => stdin
const cli = new Cli({ stdin, stdout, stderr })
const cli = new Cli({ stdin, stdout, stderr, preserveAnsi: runnerOptions.preserveAnsi })

let ctx: Vitest | undefined
let thrown = false
Expand Down

0 comments on commit 2a09cd5

Please sign in to comment.