-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestrunner.js
More file actions
executable file
·191 lines (182 loc) · 5.97 KB
/
testrunner.js
File metadata and controls
executable file
·191 lines (182 loc) · 5.97 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
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
//#!/usr/bin/env node
/**
*
*/
// TODO: clean this up, it looks like crap.
//////////////////////////////////
var se = require('se-interpreter');
var fs = require('fs');
var glob = require('glob');
var _ = require('underscore');
var argv = require('optimist').demand(1).usage('WTF').argv;
var colors = require('colors');
var util = require('util');
//////////////////////////////////
//////////////////////////////////
var override_settings = {
"driverOptions" : {},
"browserOptions" : {}
};
var listenerOptions = {};
var configurations = [];
//////////////////////////////////
/**************************************************
* This is the same listener generator that is in *
* se-interpreter, except that it uses the colors *
* module. I am doing this because the way the *
* SGR params are set by se-interpreter sometimes *
* breaks PuTTY. *
**************************************************/
function getInterpreterListener(testRun) {
return {
'startTestRun': function(testRun, info) {
if (info.success) {
console.log(testRun.name + (": Starting test " + testRun.name).green);
} else {
console.log(testRun.name + (": Unable to start test " + testRun.name + ": " + util.inspect(info.error)).red);
}
},
'endTestRun': function(testRun, info) {
if (info.success) {
console.log(testRun.name + ": Test passed".bold.green);
} else {
if (info.error) {
console.log(testRun.name + (": Test failed: " + util.inspect(info.error)).bold.red);
} else {
console.log(testRun.name + ": Test failed".bold.red);
}
}
},
'startStep': function(testRun, step) {
console.log(testRun.name + ": " + JSON.stringify(step));
},
'endStep': function(testRun, step, info) {
if (info.success) {
console.log(testRun.name + ": Success".green);
} else {
if (info.error) {
console.log(testRun.name + (": " + util.inspect(info.error)).red);
} else {
console.log(testRun.name + ": Failed".yellow);
}
}
}
};
}
// Iterate over argv keys and add to override_settings.
for (var key in argv) {
var ar = null;
if ( ar = /^browser-(.*)$/.exec(key)) {
// browser option
override_settings.browserOptions[ar[1]] = argv[key];
} else if ( ar = /^driver-(.*)$/.exec(key)) {
// driver option
override_settings.driverOptions[ar[1]] = argv[key];
} else if ( ar = /^listener-(.*)$/.exec(key)) {
listenerOptions[ar[1]] = argv[key];
}
}
// Iterate over paths and parse configs.
// TODO: refactor into functions.
argv._.forEach(function(path2glob) {
//debugger;
glob.sync(path2glob).forEach(function(path) {
// debugger;
if (/\.json$/.test(path)) {
// Parse JSON file
var conf = null;
try {
conf = JSON.parse(fs.readFileSync(path));
} catch(er) {
console.error("Cannot load " + path + " : " + er);
process.exit(1);
}
// Check that type is "interpreter-config+"
if (conf.type !== 'interpreter-config' && conf.type !== 'interpreter-config+') {
return;
}
// Iterate over configurations
conf.configurations.forEach(function(configuration) {
// Get settings and scripts
var settings = configuration.settings;
var scripts = configuration.scripts;
// Iterate over settings.
settings.forEach(function(setting) {
//debugger;
// apply override_settings.
setting.browserOptions = _.extend(setting.browserOptions, override_settings.browserOptions);
setting.driverOptions = _.extend(setting.driverOptions, override_settings.driverOptions);
// Iterate over scripts
scripts.forEach(function(script) {
// If string then glob and foreach path create test run
// debugger;
if ( typeof script === 'string') {
glob.sync(script).forEach(function(s) {
var tr = se.createTestRun(s, (argv.noPrint || argv.silent), getInterpreterListener, null, setting.browserOptions, setting.driverOptions, listenerOptions);
configurations.push(tr);
});
}// Else If obj then
else if ( typeof script === 'object') {
// get overrides
var overrides = script.overrides;
for (var i = 0; i < script.scripts.length; i++) {
glob.sync(script.scripts[i]).forEach(function(s) {
var name = /.*(?:\/|\\)(.*)\.json/.exec(s)[1];
try {
s = JSON.parse(fs.readFileSync(s));
} catch(er) {
console.error("Problem loading script " + s + ": " + er);
return;
}
// foreach step, foreach override
//debugger;
s.steps.forEach(function(step) {
_.where(overrides, {
type : step.type
}).forEach(function(o) {
if (step[o.key] === o.from) {
step[o.key] = o.to;
}
});
});
name = s.name || name;
var tr = new se.TestRun(s, name);
tr.browserOptions = setting.browserOptions;
tr.driverOptions = setting.driverOptions;
tr.silencePrints = argv.noPrint || argv.silent;
tr.listener = getInterpreterListener(tr, listenerOptions);
configurations.push(tr);
});
}
}
});
});
});
}
});
});
// DEBUG //
//console.log(configurations);
// DEBUG //
// run tests
var index = -1;
var success = 0;
function runNext() {
index++;
if (index < configurations.length) {
configurations[index].run(function(info) {
success += (info.success) ? 1 : 0;
runNext();
});
} else if (index === configurations.length) {
if (!argv.silent) {
//console.log("\x1b[" + (success == configurations.length ? "32" : "31") + "m\x1b[1m" + success + '/' + configurations.length + ' tests ran successfully. Exiting.\x1b[30m\x1b[0m');
var msg = success + '/' + configurations.length + ' tests ran successfully. Exiting.';
console.log(msg[success === configurations.length ? "green" : "red"]);
}
process.on('exit', function() {
process.exit(success == configurations.length ? 0 : 1);
});
}
}
runNext();