-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
index.ts
117 lines (113 loc) · 2.57 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
BaseOpts,
PromiseWithSubtest,
Spawn,
TapPlugin,
TestBase,
} from '@tapjs/core'
import { StdioOptions } from 'child_process'
/**
* Options for `t.spawn()`
*
* All of the normal Base and Extra options are also allowed.
*/
export interface SpawnOpts extends BaseOpts {
/**
* the current working directory of the subprocess
*/
cwd?: string
/**
* the command to run (set by first argument to t.spawn)
*
* @internal
*/
command?: string
/**
* arguments to subprocess (set by second argument to t.spawn)
*
* @internal
*/
args?: string[]
/**
* ChildProcess `stdio` option.
* Standard output is always set to `'pipe'`, because that's how it
* communicates test results, and file descriptor 3 is set to an IPC
* channel for sending timeout signals.
*/
stdio?: StdioOptions
/**
* Environment variables for the subprocess
* Defaults to `process.env`
*/
env?: { [k: string]: string } | typeof process.env
/**
* Set on exit. The exit code of the process, or null if terminated
* with a signal.
*
* @internal
*/
exitCode?: number | null
/**
* Set on exit. Terminating signal, or null of not terminated with a
* signal.
*
* @internal
*/
signal?: string | null
}
export type PromiseWithSpawn = PromiseWithSubtest<Spawn>
export class SpawnPlugin {
#t: TestBase
constructor(t: TestBase) {
this.#t = t
}
/**
* Spawn a child process and parse its standard output as a subtest
*
* @group Subtest Methods
*/
spawn(cmd: string): PromiseWithSpawn
spawn(
cmd: string,
options: SpawnOpts,
name?: string,
): PromiseWithSpawn
spawn(
cmd: string,
args: string | string[],
name?: string,
): PromiseWithSpawn
spawn(
cmd: string,
args: string | string[],
options: SpawnOpts,
name?: string,
): PromiseWithSpawn
spawn(
cmd: string,
args?: string | string[] | SpawnOpts,
options?: SpawnOpts | string,
name?: string,
): PromiseWithSpawn {
if (typeof args === 'string') {
args = [args]
}
if (typeof options === 'string') {
name = options
options = {}
}
if (typeof args === 'object' && !Array.isArray(args)) {
options = args
args = []
}
options = options || {}
if (options.name === undefined && name !== undefined) {
options.name = name
}
options.command = cmd
options.args = args !== undefined ? args : []
return this.#t.sub(Spawn, options, this.#t.t.spawn)
}
}
export const plugin: TapPlugin<SpawnPlugin> = (t: TestBase) =>
new SpawnPlugin(t)