-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (143 loc) · 4.77 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
const debug = require('debug')('protractor-cucumber-framework');
const glob = require('glob');
const path = require('path');
const q = require('q');
const tmp = require('tmp');
const cucumberLoader = require('./lib/cucumberLoader');
const Cucumber = cucumberLoader.load();
const cucumberVersion = cucumberLoader.majorVersion();
const cwd = cucumberLoader.cwd();
const state = require('./lib/runState');
const extraFlags = ['cucumberOpts'];
/**
* Execute the Runner's test cases through Cucumber.
*
* @param {Runner} runner The current Protractor Runner.
* @param {Array} specs Array of Directory Path Strings.
* @return {q.Promise} Promise resolved with the test results
*/
exports.run = function(runner, specs) {
return runner.runTestPreparer(extraFlags).then(() => {
const results = {};
const config = runner.getConfig();
const opts = Object.assign(
{},
config.cucumberOpts,
config.capabilities.cucumberOpts
);
const cliArgs = buildCliArgsFrom(opts);
state.initialize(runner, results, opts.strict);
return q.promise(function(resolve, reject) {
runCucumber(cliArgs, () => {
try {
let complete = q();
if (runner.getConfig().onComplete) {
complete = q(runner.getConfig().onComplete());
}
complete.then(() => resolve(results));
} catch (err) {
reject(err);
}
});
});
});
function runCucumber(argv, done) {
debug('cucumber command: "' + argv.join(' ') + '"');
if (cucumberVersion >= 2) {
let cli = new Cucumber.Cli({
argv: argv,
cwd,
stdout: process.stdout
});
return cli.run().then(done);
} else {
Cucumber.Cli(argv).run(done);
}
}
function buildCliArgsFrom(opts) {
let argv = convertOptionsToCliArgs(opts);
let capturer = path.resolve(__dirname, 'lib', 'resultsCapturer.js');
if (cucumberVersion < 3) {
argv.push('--require', capturer);
} else {
let tempFile = tmp.fileSync();
argv.push('--format', `${capturer}:${tempFile.name}`);
}
if (opts.rerun) {
argv.push(opts.rerun);
} else {
argv = argv.concat(specs);
}
return argv;
}
function convertOptionsToCliArgs(options) {
let argv = ['node', 'cucumberjs'];
for (let option in options) {
if (option === 'rerun') continue;
let cliArgumentValues = convertOptionValueToCliValues(
option,
options[option]
);
if (Array.isArray(cliArgumentValues)) {
cliArgumentValues.forEach(value => argv.push('--' + option, value));
} else if (cliArgumentValues) {
argv.push('--' + option);
}
}
return argv;
}
function convertRequireOptionValuesToCliValues(values) {
let configDir = runner.getConfig().configDir;
return toArray(values)
.map(path => glob.sync(path, {cwd: configDir})) // Handle glob matching
.reduce((opts, globPaths) => opts.concat(globPaths), []) // Combine paths into flattened array
.map(requirePath => path.resolve(configDir, requirePath)) // Resolve require absolute path
.filter((item, pos, orig) => orig.indexOf(item) == pos); // Make sure requires are unique
}
function convertTagsToV2CliValues(values) {
let converted = toArray(values)
.filter(tag => !!tag.replace)
.map(tag => tag.replace(/~/, 'not '))
.join(' and ');
return converted ? [converted] : '';
}
function makeFormatPathsUnique(values) {
return toArray(values).map(function(format) {
let formatPathMatch = format.match(/(.+):(.+)/);
if (!formatPathMatch) return format;
let pathParts = formatPathMatch[2].split('.');
pathParts.splice(pathParts.length - 1 || 1, 0, process.pid);
return `${formatPathMatch[1]}:${pathParts.join('.')}`;
});
}
function convertGenericOptionValuesToCliValues(values) {
if (values === true || !values) {
return values;
} else {
return toArray(values);
}
}
function convertOptionValueToCliValues(option, values) {
if (option === 'require') {
return convertRequireOptionValuesToCliValues(values);
} else if (option === 'tags' && cucumberVersion >= 2) {
return convertTagsToV2CliValues(values);
} else if (option === 'format' && areUniquePathsRequired()) {
return makeFormatPathsUnique(values);
} else {
return convertGenericOptionValuesToCliValues(values);
}
}
function areUniquePathsRequired() {
let config = runner.getConfig();
return (
(Array.isArray(config.multiCapabilities) &&
config.multiCapabilities.length > 0) ||
typeof config.getMultiCapabilities === 'function' ||
config.capabilities.shardTestFiles
);
}
function toArray(values) {
return Array.isArray(values) ? values : [values];
}
};