Skip to content

Commit

Permalink
test: replace tinyexec with node:child_process
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 25, 2024
1 parent 1ef0491 commit 9c9e69b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
45 changes: 22 additions & 23 deletions test/config/test/console-color.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
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,
},
})
expect(proc.stdout).toContain('true\n')
}, undefined, undefined, undefined, { preserveAnsi: true })

expect(stdout).toContain('true\n')
expect(stdout).not.toContain('\x1B[33mtrue\x1B[39m\n')
})
32 changes: 15 additions & 17 deletions test/config/test/exec-args.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { x } from 'tinyexec'
import { spawnSync } from 'node:child_process'
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

Expand Down Expand Up @@ -35,20 +35,19 @@ test.each([
})

test('should not pass execArgv to workers when not specified in the config', async () => {
const { stdout, stderr } = await x('node', [
const { stdout, stderr } = spawnSync('node', [
'--title',
'this-works-only-on-main-thread',
'../../../../node_modules/vitest/vitest.mjs',
'--run',
], {
nodeOptions: {
cwd: `${process.cwd()}/fixtures/no-exec-args-fixtures`,
env: {
VITE_NODE_DEPS_MODULE_DIRECTORIES: '/node_modules/,/packages/',
NO_COLOR: '1',
},
encoding: 'utf-8',
cwd: `${process.cwd()}/fixtures/no-exec-args-fixtures`,
env: {
...process.env,
VITE_NODE_DEPS_MODULE_DIRECTORIES: '/node_modules/,/packages/',
NO_COLOR: '1',
},
throwOnError: false,
})

expect(stderr).not.toContain('Error: Initiated Worker with invalid execArgv flags: --title')
Expand All @@ -57,21 +56,20 @@ test('should not pass execArgv to workers when not specified in the config', asy
})

test('should let allowed args pass to workers', async () => {
const { stdout, stderr } = await x('node', [
const { stdout, stderr } = spawnSync('node', [
'--heap-prof',
'--diagnostic-dir=/tmp/vitest-diagnostics',
'--heap-prof-name=heap.prof',
'../../../../node_modules/vitest/vitest.mjs',
'--run',
], {
nodeOptions: {
cwd: `${process.cwd()}/fixtures/allowed-exec-args-fixtures`,
env: {
VITE_NODE_DEPS_MODULE_DIRECTORIES: '/node_modules/,/packages/',
NO_COLOR: '1',
},
encoding: 'utf-8',
cwd: `${process.cwd()}/fixtures/allowed-exec-args-fixtures`,
env: {
...process.env,
VITE_NODE_DEPS_MODULE_DIRECTORIES: '/node_modules/,/packages/',
NO_COLOR: '1',
},
throwOnError: false,
})

expect(stderr).toBe('')
Expand Down
7 changes: 5 additions & 2 deletions test/test-utils/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ 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.stdin = options.stdin
this.preserveAnsi = options.preserveAnsi

for (const source of (['stdout', 'stderr'] as const)) {
const stream = options[source]
Expand All @@ -37,7 +40,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 9c9e69b

Please sign in to comment.