-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
world.ts
114 lines (107 loc) · 3.02 KB
/
world.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
import { setWorldConstructor, Cli } from '../../'
import { execFile } from 'child_process'
import { expect } from 'chai'
import toString from 'stream-to-string'
import { PassThrough } from 'stream'
import colors from 'colors/safe'
import fs, { WriteStream } from 'fs'
import path from 'path'
import VError from 'verror'
import _ from 'lodash'
import protobuf from 'protobufjs'
import { messages } from 'cucumber-messages'
interface ILastRun {
error: any
errorOutput: string
envelopes: messages.IEnvelope[]
output: string
}
interface IRunResult {
error: any
stderr: string
stdout: string
}
export class World {
public tmpDir: string
public spawn: boolean = false
public debug: boolean = false
public lastRun: ILastRun
public verifiedLastRunError: boolean
public localExecutablePath: string
public globalExecutablePath: string
async run(executablePath: string, inputArgs: string[]): Promise<void> {
const messageFilename = 'message.out'
const args = ['node', executablePath]
.concat(inputArgs, [
'--backtrace',
'--predictable-ids',
'--format',
`message:${messageFilename}`,
])
.map(arg => {
if (_.includes(arg, '/')) {
return path.normalize(arg)
}
return arg
})
const cwd = this.tmpDir
let result: IRunResult
if (this.spawn) {
result = await new Promise(resolve => {
execFile(args[0], args.slice(1), { cwd }, (error, stdout, stderr) => {
resolve({ error, stdout, stderr })
})
})
} else {
const stdout = (new PassThrough() as unknown) as WriteStream
const cli = new Cli({
argv: args,
cwd,
stdout,
})
let error: any, stderr: string
try {
const { success } = await cli.run()
if (!success) {
error = new Error('CLI exited with non-zero')
error.code = 42
}
stderr = ''
} catch (err) {
error = err
stderr = VError.fullStack(error)
}
stdout.end()
result = { error, stdout: await toString(stdout), stderr }
}
const envelopes: messages.Envelope[] = []
const messageOutputPath = path.join(cwd, messageFilename)
if (fs.existsSync(messageOutputPath)) {
const data = fs.readFileSync(messageOutputPath)
const reader = protobuf.Reader.create(data)
while (reader.pos < reader.len) {
envelopes.push(messages.Envelope.decodeDelimited(reader))
}
fs.writeFileSync(
path.join(cwd, 'message.out.json'),
JSON.stringify(
envelopes.map(e => e.toJSON()),
null,
2
)
)
}
if (this.debug) {
console.log(result.stdout + result.stderr) // eslint-disable-line no-console
}
this.lastRun = {
error: result.error,
errorOutput: result.stderr,
envelopes,
output: colors.strip(result.stdout),
}
this.verifiedLastRunError = false
expect(this.lastRun.output).to.not.include('Unhandled rejection')
}
}
setWorldConstructor(World)