This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (121 loc) · 4.9 KB
/
index.js
File metadata and controls
148 lines (121 loc) · 4.9 KB
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
'use strict';
const _ = require('lodash');
const {Command} = require('commander');
const Events = require('../constants/events');
const Gemini = require('../gemini');
const pkg = require('../../package.json');
const {checkForDeprecations} = require('./deprecations');
const {handleErrors, handleUncaughtExceptions} = require('./errors');
let exitCode;
exports.run = () => {
const program = new Command();
program
.version(pkg.version)
.allowUnknownOption()
.option('-c, --config <file>', 'config file');
const configPath = preparseOption(program, 'config');
const gemini = mkGemini(configPath);
program
.option('-b, --browser <browser>', 'run test only in specified browser', collect)
.option('-s, --set <set>', 'set to run', collect)
.option('--grep <pattern>', 'run only suites matching the pattern', RegExp);
program.command('update [paths...]')
.allowUnknownOption()
.option('--diff', 'update only screenshots with diff')
.option('--new', 'save only new screenshots')
.description('update the changed screenshots or gather if they doesn\'t exist')
.action((paths, options) => mkRunFn(gemini, 'update', program)(paths, options).done());
program.command('test [paths...]')
.allowUnknownOption()
.option('-r, --reporter <reporter>', 'test result reporter (flat by default)', collect)
.description('run tests')
.on('--help', () => {
console.log(' Reporters:');
console.log(' flat console reporter');
console.log(' vflat verbose console reporter');
console.log('');
})
.action((paths, options) => mkRunFn(gemini, 'test', program)(paths, options).done());
program.command('list <key>')
.description(`Use 'list browsers' or 'list sets' to get all available browsers or sets.`)
.action((key) => logOptionFromConfig(key, gemini));
program.on('--help', () => {
console.log('');
console.log(' Overriding config:');
console.log('');
console.log(' To override any config option use full option path converted to --kebab-case.');
console.log('');
console.log(' Examples:');
console.log(' gemini test --system-debug true');
console.log(' gemini update --root-url http://example.com');
console.log(' gemini test --browsers-ie-8-window-size 1024x768');
console.log('');
console.log(' You can also use environment variables converted to snake_case with');
console.log(' gemini_ prefix.');
console.log('');
console.log(' Examples:');
console.log(' gemini_system_debug=true gemini test');
console.log(' gemini_root_url=http://example.com gemini update');
console.log(' gemini_browsers_ie8_window_size=1024x768 gemini test');
console.log('');
console.log(' If both cli flag and env var are used, cli flag takes precedence.') ;
console.log(' For more information see https://gemini-testing.github.io/doc/config.html') ;
console.log('');
});
gemini.extendCli(program);
program.parse(process.argv);
};
function preparseOption(program, option) {
// do not display any help, do not exit
const configFileParser = Object.create(program);
configFileParser.options = [].concat(program.options);
configFileParser.option('-h, --help');
configFileParser.parse(process.argv);
return configFileParser[option];
}
function mkGemini(configPath) {
checkForDeprecations();
const gemini = new Gemini(configPath, {cli: true, env: true});
gemini.on(Events.INTERRUPT, (data) => exitCode = data.exitCode);
return gemini;
}
function mkRunFn(gemini, method, globalOpts) {
return (paths, opts = {}) => {
handleUncaughtExceptions();
return gemini[method](paths, {
sets: globalOpts.set,
reporters: parseReporterOptions(opts),
grep: globalOpts.grep,
browsers: globalOpts.browser,
diff: opts.diff,
new: opts.new
})
.then((stats) => stats.failed > 0 ? 2 : 0)
.catch(handleErrors)
.then(exit);
};
}
function logOptionFromConfig(key, {config}) {
const data = {
sets: _.keys(config.sets).join(', '),
browsers: config.getBrowserIds().join(', ')
};
console.log(data[key] || `Cannot list option ${key}. Available options are: sets, browsers`);
}
function collect(newValue, array = []) {
return array.concat(newValue);
}
function parseReporterOptions(options) {
if (!options.reporter) {
return [{name: 'flat'}];
}
return options.reporter.map(function(name) {
return {
name,
path: options[`${name}ReporterPath`]
};
});
}
function exit(code) {
process.on('exit', () => process.exit(exitCode || code));
}