Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 6fea4ad

Browse files
committed
fix: allow stdout/stderr to work with stdout-stderr for testing
1 parent 3439d23 commit 6fea4ad

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/action/base.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ export interface Options {
1313
stdout?: boolean
1414
}
1515

16-
const stdmockWrite = {
17-
stdout: process.stdout.write,
18-
stderr: process.stderr.write,
19-
}
20-
2116
export class ActionBase {
2217
type!: ActionType
2318
std: 'stdout' | 'stderr' = 'stderr'
2419
protected stdmocks?: ['stdout' | 'stderr', string[]][]
20+
private stdmockOrigs = {
21+
stdout: process.stdout.write,
22+
stderr: process.stderr.write,
23+
}
2524

2625
public start(action: string, status?: string, opts: Options = {}) {
2726
this.std = opts.stdout ? 'stdout' : 'stderr'
@@ -134,6 +133,11 @@ export class ActionBase {
134133
const outputs: ['stdout', 'stderr'] = ['stdout', 'stderr']
135134
if (toggle) {
136135
if (this.stdmocks) return
136+
this.stdmockOrigs = {
137+
stdout: process.stdout.write,
138+
stderr: process.stderr.write,
139+
}
140+
137141
this.stdmocks = []
138142
for (let std of outputs) {
139143
(process[std] as any).write = (...args: any[]) => {
@@ -144,7 +148,7 @@ export class ActionBase {
144148
if (!this.stdmocks) return
145149
// this._write('stderr', '\nresetstdmock\n\n\n')
146150
delete this.stdmocks
147-
for (let std of outputs) process[std].write = stdmockWrite[std] as any
151+
for (let std of outputs) process[std].write = this.stdmockOrigs[std] as any
148152
}
149153
} catch (err) {
150154
this._write('stderr', inspect(err))
@@ -178,6 +182,6 @@ export class ActionBase {
178182
* write to the real stdout/stderr
179183
*/
180184
protected _write(std: 'stdout' | 'stderr', s: string | string[]) {
181-
stdmockWrite[std].apply(process[std], _.castArray(s))
185+
this.stdmockOrigs[std].apply(process[std], _.castArray(s))
182186
}
183187
}

0 commit comments

Comments
 (0)