UNPKG

3 kBJavaScriptView Raw
1var q = require('q'),
2 path = require('path'),
3 glob = require('glob'),
4 debug = require('debug')('protractor-cucumber-framework'),
5 Cucumber = require('cucumber'),
6 state = require('./lib/runState');
7
8/**
9 * Execute the Runner's test cases through Cucumber.
10 *
11 * @param {Runner} runner The current Protractor Runner.
12 * @param {Array} specs Array of Directory Path Strings.
13 * @return {q.Promise} Promise resolved with the test results
14 */
15exports.run = function(runner, specs) {
16 var results = {}
17
18 return runner.runTestPreparer().then(function() {
19 var opts = runner.getConfig().cucumberOpts;
20 state.initialize(runner, results, opts.strict);
21
22 return q.promise(function(resolve, reject) {
23 var cliArguments = convertOptionsToCliArguments(opts);
24 cliArguments.push('--require', path.resolve(__dirname, 'lib', 'resultsCapturer.js'));
25 cliArguments = cliArguments.concat(specs);
26
27 debug('cucumber command: "' + cliArguments.join(' ') + '"');
28
29 Cucumber.Cli(cliArguments).run(function (isSuccessful) {
30 try {
31 var complete = q();
32 if (runner.getConfig().onComplete) {
33 complete = q(runner.getConfig().onComplete());
34 }
35 complete.then(function() {
36 resolve(results);
37 });
38 } catch (err) {
39 reject(err);
40 }
41 });
42 });
43 });
44
45 function convertOptionsToCliArguments(options) {
46 var cliArguments = ['node', 'cucumberjs'];
47
48 for (var option in options) {
49 var cliArgumentValues = convertOptionValueToCliValues(option, options[option]);
50
51 if (Array.isArray(cliArgumentValues)) {
52 cliArgumentValues.forEach(function (value) {
53 cliArguments.push('--' + option, value);
54 });
55 } else if (cliArgumentValues) {
56 cliArguments.push('--' + option);
57 }
58 }
59
60 return cliArguments;
61 }
62
63 function convertRequireOptionValuesToCliValues(values) {
64 var configDir = runner.getConfig().configDir;
65
66 return toArray(values).map(function(path) {
67 // Handle glob matching
68 return glob.sync(path, {cwd: configDir});
69 }).reduce(function(opts, globPaths) {
70 // Combine paths into flattened array
71 return opts.concat(globPaths);
72 }, []).map(function(requirePath) {
73 // Resolve require absolute path
74 return path.resolve(configDir, requirePath)
75 }).filter(function(item, pos, orig) {
76 // Make sure requires are unique
77 return orig.indexOf(item) == pos;
78 });
79 }
80
81 function convertGenericOptionValuesToCliValues(values) {
82 if (values === true || !values) {
83 return values;
84 } else {
85 return toArray(values);
86 }
87 }
88
89 function convertOptionValueToCliValues(option, values) {
90 if (option === 'require') {
91 return convertRequireOptionValuesToCliValues(values);
92 } else {
93 return convertGenericOptionValuesToCliValues(values);
94 }
95 }
96
97 function toArray(values) {
98 return Array.isArray(values) ? values : [values];
99 }
100};