Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
125 lines (98 loc) · 3.91 KB
/
Copy pathindex.js
File metadata and controls
125 lines (98 loc) · 3.91 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
/**
* Module dependencies
*/
var _ = require('lodash');
var path = require('path');
var RawMachinepackTestRunner = require('test-machinepack').rawTestRunner;
// Customize generic test driver for mocha
module.exports = function mochaDriver(pathToMachinepackDir) {
// Use cwd as our path unless overridden by the arg above
pathToMachinepackDir = pathToMachinepackDir || process.cwd();
var opts = {};
RawMachinepackTestRunner(pathToMachinepackDir,function beforeRunningAnyTests(_opts, done){
// Expose provided options via closure for use throughout this module.
opts = _opts || {};
// TODO: set mocha.opts based on `opts.driver.*` (if provided)
done();
}, function eachMachineSuite(machineIdentity, runTests){
describe('`'+machineIdentity+'` machine', function (){
runTests(function onTest(testCase, nextTestCase){
var jsonInputVals;
try {
jsonInputVals = JSON.stringify(testCase.using);
}
catch (e) {
}
if (testCase.todo) {
it('should exit with `'+testCase.outcome+'`'+(_.isUndefined(jsonInputVals)?'':' given input values: `'+jsonInputVals+'`') );
return nextTestCase();
}
it('should exit with `'+testCase.outcome+'`'+ (_.isUndefined(jsonInputVals)?'':' given input values: `'+jsonInputVals+'`'), function (done){
return nextTestCase(done);
});
});
});
}, function afterRunningAllTests(err, missingSuites) {
if (err) {
console.error('test-machinepack-mocha encountered an error while running tests:\n',err);
return;
}
// If `showMissingSuites` option is set, log a message to the console
// enumerating untested machines.
if (opts.showMissingSuites) {
after(function(){
if (_.isArray(missingSuites) && missingSuites.length > 0) {
showMissingSuites(missingSuites);
}
});
}
});
};
function showMissingSuites(missingSuites) {
// Use setTimeout so that these logs appear after the mocha output
setTimeout(function() {
// Pad a line with spaces
var pad = function(line, lineLength) {
if (line.length < lineLength) {
line += Array((lineLength - line.length) + 1).join(" ");
}
return line;
};
// Find the longest machine ID
var longestMachineId = _.reduce(missingSuites, function(memo, machineId) {return machineId.length > memo ? machineId.length : memo;}, 0);
// Static instructional lines
var instrLine1 = "The following machines are missing test suites:";
var instrLine2 = "Try using `mp scrub` to generate test stubs!";
// Determine the length of each line in the output (either using the longest machine name, or the first instruction)
var lineLength = longestMachineId < instrLine1.length ? instrLine1.length : longestMachineId;
// Create a line of stars for the top and bottom
var borderLine = Array(lineLength + 5).join("*");
// Create a line with stars on each end and spaces in the middle
var spacerLine = "*" + Array(lineLength + 3).join(" ") + "*";
// Output the top
console.error(borderLine);
// Output a spacer line
console.error(spacerLine);
// Output instruction 1
console.error("* " + pad(instrLine1, lineLength) + " *");
// Output a spacer line
console.error(spacerLine);
// Output the names of each missing machine suite
_.each(missingSuites, function(machineId) {
console.error("* " + pad(machineId, lineLength) + " *");
});
// Output a spacer line
console.error(spacerLine);
// Output instruction 2
console.error("* " + pad(instrLine2, lineLength) + " *");
// Output a spacer line
console.error(spacerLine);
// Output the bottom
console.error(borderLine);
});
}
/**
* Programmatic access to chainable helper that facilitates
* manual test-writing for individual machines.
*/
module.exports.testMachineWithMocha = require('./test-machine-with-mocha');