-
Notifications
You must be signed in to change notification settings - Fork 73
/
runner.js
119 lines (96 loc) · 3.91 KB
/
runner.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
const fs = require('fs')
const path = require('path')
const tmp = require('tmp')
const glob = require('glob')
const mkdirp = require('mkdirp')
const pify = require('pify')
const dummyTestModulesFolder = tmp.dirSync({ unsafeCleanup: true }).name
const NightwatchApi = require('./nightwatch-api')
const CucumberApi = require('./cucumber-api')
tmp.setGracefulCleanup()
module.exports = class Runner {
constructor () {
this.originalFeaturePath = {}
}
generateDummyTestModules () {
this.featureFiles.forEach((featureSource) => {
const featureFiles = glob.sync(path.join(featureSource, '**/*.feature'))
featureFiles.forEach((featureFile) => {
const dummyTestModuleFile = this.featurePathToDummyPath(featureFile)
mkdirp.sync(path.dirname(dummyTestModuleFile))
fs.writeFileSync(dummyTestModuleFile, '')
})
})
}
getTags (nightwatchArgv) {
let result = nightwatchArgv.tag || []
if (typeof result === 'string') {
result = [result]
}
return result.map((tag) => `@${tag}`)
}
getSkipTags (nightwatchArgv) {
if (!nightwatchArgv.skiptags) return []
return nightwatchArgv.skiptags.split(',').map((tag) => `@${tag}`)
}
featurePathToDummyPath (featureFile) {
if (!featureFile.match(/\.feature$/)) featureFile += '.feature'
const dummyPath = path.join(dummyTestModulesFolder, featureFile.replace(/\.feature$/, '.js'))
this.originalFeaturePath[dummyPath] = featureFile
return dummyPath
}
dummyPathToFeaturePath (dummyPath) {
if (!dummyPath.match(/\.js$/)) dummyPath += '.js'
return this.originalFeaturePath[dummyPath] || dummyPath
}
addIndexToFileName (fileName, index) {
const cutIndex = fileName.lastIndexOf('.')
const ext = fileName.slice(cutIndex)
const rest = fileName.slice(0, cutIndex)
return `${rest}-${index}${ext}`
}
* executeTestModules (dummyTestModules) {
const featureFiles = dummyTestModules.map((dummyTestModule) => this.dummyPathToFeaturePath(dummyTestModule))
if (this.nightwatchApi.isRunningInParallel()) {
const workerIndex = this.nightwatchApi.getWorkerIndex()
this.jsonReport = this.addIndexToFileName(this.jsonReport, workerIndex)
}
if (this.jsonReport) {
yield pify(mkdirp)(path.dirname(this.jsonReport))
}
const cucumberArgs = this.cucumberApi.getAdjustedArgs({
args: this.options.cucumberArgs,
featureFiles,
jsonReport: this.jsonReport,
tags: this.getTags(this.nightwatchApi.nightwatchArgv),
skipTags: this.getSkipTags(this.nightwatchApi.nightwatchArgv)
})
return yield * this.cucumberApi.run(cucumberArgs)
}
run (options) {
const self = this
this.options = options
this.nightwatchApi = new NightwatchApi(options)
this.cucumberApi = new CucumberApi(options)
this.jsonReport = this.cucumberApi.getJSONReportName(options.cucumberArgs)
this.featureFiles = this.cucumberApi.getFeatureFiles(options.cucumberArgs)
this.featureFiles.forEach((featureSource) => {
try {
fs.statSync(featureSource)
} catch (err) {
throw new Error(`Feature source ${featureSource} doesn't exists`)
}
})
const dummyPaths = this.featureFiles.map((srcPath) => path.join(dummyTestModulesFolder, srcPath))
this.generateDummyTestModules()
this.cucumberApi.addAttachmentProvider(this.nightwatchApi.takeScreenshot.bind(this.nightwatchApi))
this.nightwatchApi.addTestModulePaths(dummyPaths)
this.nightwatchApi.addPathConverter(this.featurePathToDummyPath.bind(this), this.dummyPathToFeaturePath.bind(this))
this.nightwatchApi.overrideOriginalStartTestWorkers()
this.nightwatchApi.addTestRunner(this.executeTestModules.bind(this))
this.nightwatchApi.addHookAfterChildProcesses(function * () {
const reports = yield pify(glob)(self.addIndexToFileName(self.jsonReport, '*'))
yield * self.cucumberApi.mergeCucumberJsonReports(reports, self.jsonReport)
})
}
}