-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
275 lines (253 loc) · 7.78 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { loadModuleResolvedFrom, loadModuleRelativeTo } from 'load-module'
import commandLineArgs from 'command-line-args'
import commandLineUsage from 'command-line-usage'
import path from 'path'
import fastGlob from 'fast-glob'
import walkBack from 'walk-back'
import Tom from '@test-runner/tom'
import TestRunnerCore from '@test-runner/core'
import * as origFs from 'fs'
import getModulePaths from 'current-module-paths'
import { pathToFileURL } from 'url'
const modulePath = getModulePaths(import.meta.url)
const __dirname = modulePath.__dirname
const fs = origFs.promises
/**
* @module test-runner
*/
/**
* @alias module:test-runner
*/
class TestRunnerCli {
/**
* @param {object} [optons]
* @param {function} [optons.errorLog]
*/
constructor (options = {}) {
this.options = options
this.errorLog = options.errorLog || console.error
this.optionDefinitions = [
{
name: 'files',
type: String,
multiple: true,
defaultOption: true,
description: 'One of more files, each of which export a test-object-model instance.'
},
{
name: 'tree',
type: Boolean,
alias: 't',
description: 'Inspect the supplied test-object-model structure without running the tests.'
},
{
name: 'silent',
type: Boolean,
alias: 's',
description: 'Run without printing any output. Useful when you\'re only interested in the exit code.'
},
{
name: 'max-file-concurrency',
type: Number,
description: 'Maximum number of input files to process concurrently. Defaults to 10.'
},
{
name: 'max-concurrency',
type: Number,
alias: 'c',
description: 'Maximum number of tests to process concurrently. Overrides all values set within the test object model.'
},
{
name: 'view',
type: String,
description: 'Attach an alternative view. Specifiy either "oneline", "live", the path to a view class or the name of an installed view module.'
},
{
name: 'help',
type: Boolean,
alias: 'h',
description: 'Print this usage guide.'
},
{
name: 'version',
type: Boolean,
description: 'Print the version number and exit.'
},
{
name: 'debug',
type: Boolean,
description: "Prints debug output. Use this when a test is misbehaving and you'd like to know why."
}
]
this.viewOptionDefinitions = []
}
/* Encapsulate this in load-module */
async loadUserModule (moduleId) {
let mod
mod = await loadModuleResolvedFrom(moduleId, [process.cwd(), __dirname])
if (mod === null) {
mod = await loadModuleRelativeTo(moduleId, [process.cwd()])
}
if (mod === null) {
throw new Error('Module not found: ' + moduleId)
}
return mod
}
async loadModule (moduleId) {
const mod = await import(moduleId)
return mod.default
}
async getViewClass (options = {}) {
let viewModule
if (options.view) {
if (options.view === 'live') {
viewModule = await this.loadModule('@test-runner/live-view')
} else if (options.view === 'oneline') {
viewModule = await this.loadModule('@test-runner/oneline-view')
} else {
viewModule = await this.loadUserModule(options.view)
}
} else {
viewModule = await this.loadModule('@test-runner/default-view')
}
return viewModule || null
}
async getAllOptionDefinitions () {
const allOptionDefinitions = this.optionDefinitions.slice()
const coreOptions = commandLineArgs(this.optionDefinitions, { camelCase: true, partial: true })
const ViewClass = await this.getViewClass(coreOptions)
if (ViewClass && ViewClass.optionDefinitions) {
this.viewOptionDefinitions = ViewClass.optionDefinitions() || []
allOptionDefinitions.push(...this.viewOptionDefinitions)
}
this.allOptionDefinitions = allOptionDefinitions
return allOptionDefinitions
}
async getOptions () {
const options = Object.assign({}, this.options, commandLineArgs(this.allOptionDefinitions, { camelCase: true }))
if (!options.silent) {
const ViewClass = await this.getViewClass(options)
const view = new ViewClass(options)
options._view = view
}
return options
}
async printUsage () {
this.errorLog(commandLineUsage([
{
header: 'test-runner',
content: 'Minimal, flexible, extensible command-line test runner.'
},
{
header: 'Synopsis',
content: '$ test-runner [<options>] {underline file} {underline ...}'
},
{
header: 'Options',
optionList: this.optionDefinitions,
hide: 'files'
},
{
header: 'View options',
optionList: this.viewOptionDefinitions
},
{
content: 'For more information see: {underline https://github.com/test-runner-js/test-runner}'
}
]))
}
async printVersion () {
const pkgFile = await fs.readFile(path.resolve(__dirname, 'package.json'), 'utf8')
const pkg = JSON.parse(pkgFile)
this.errorLog(pkg.version)
}
async printTree (tom) {
const TreeView = await this.loadModule(pathToFileURL(path.resolve(__dirname, './lib/tree.js')))
const treeView = new TreeView(tom)
this.errorLog(treeView.toString())
}
async expandGlobs (globs) {
const result = new Set()
const files = await fastGlob(globs, { onlyFiles: true })
for (const file of files) {
result.add(file)
}
return Array.from(result.values())
}
async getPackageName () {
const packagePath = walkBack(process.cwd(), 'package.json')
let name
if (packagePath) {
const pkgFile = await fs.readFile(packagePath, 'utf8')
const pkg = JSON.parse(pkgFile)
name = pkg.name
}
return name
}
/* TODO: Move this logic within Core making it default for test-runner-nature too. UNLESS isomorphism is required in Core. */
async getTom (files, options) {
const toms = []
for (const file of files) {
const tom = await this.loadModule(pathToFileURL(path.resolve(process.cwd(), file)))
if (tom) {
if (tom.name === 'tom') {
tom.name = file
}
toms.push(tom)
} else {
throw new Error('No TOM exported: ' + file)
}
}
const name = await this.getPackageName()
return Tom.combine(toms, name, options)
}
async runTests (tom, options) {
const runner = new TestRunnerCore(tom, { view: options._view, debug: options.debug })
runner.on('fail', () => {
process.exitCode = 1
})
return runner.start().then(() => runner)
}
/**
* Start test-runner.
* @return {Promise}
* @fulfil {TestRunnerCore}
*/
async start () {
await this.getAllOptionDefinitions()
const options = await this.getOptions()
/* --help */
if (options.help) {
await this.printUsage()
/* --version */
} else if (options.version) {
await this.printVersion()
/* --files */
} else {
if (options.files && options.files.length) {
const files = await this.expandGlobs(options.files)
if (files.length) {
const tom = await this.getTom(files, { maxConcurrency: options.maxFileConcurrency || 10 })
if (options.maxConcurrency) {
for (const test of tom) {
test.options.maxConcurrency = options.maxConcurrency
}
}
/* --tree */
if (options.tree) {
await this.printTree(tom)
} else {
return this.runTests(tom, options)
}
} else {
this.errorLog('one or more input files required')
await this.printUsage()
}
} else {
await this.printUsage()
}
}
}
}
TestRunnerCli.Tom = Tom
export default TestRunnerCli