ccf60ba36a135bfaed578b3649e994b0fce88a18.svn-base
8.47 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* grunt
* http://gruntjs.com/
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
module.exports = function(grunt) {
// Nodejs libs.
var fs = require('fs');
var path = require('path');
// External libs.
var Tempfile = require('temporary/lib/file');
// Keep track of the last-started module, test and status.
var currentModule, currentTest, status;
// Keep track of the last-started test(s).
var unfinished = {};
// Allow an error message to retain its color when split across multiple lines.
function formatMessage(str) {
return String(str).split('\n').map(function(s) { return s.magenta; }).join('\n');
}
// Keep track of failed assertions for pretty-printing.
var failedAssertions = [];
function logFailedAssertions() {
var assertion;
// Print each assertion error.
while (assertion = failedAssertions.shift()) {
grunt.verbose.or.error(assertion.testName);
grunt.log.error('Message: ' + formatMessage(assertion.message));
if (assertion.actual !== assertion.expected) {
grunt.log.error('Actual: ' + formatMessage(assertion.actual));
grunt.log.error('Expected: ' + formatMessage(assertion.expected));
}
if (assertion.source) {
grunt.log.error(assertion.source.replace(/ {4}(at)/g, ' $1'));
}
grunt.log.writeln();
}
}
// Handle methods passed from PhantomJS, including QUnit hooks.
var phantomHandlers = {
// QUnit hooks.
moduleStart: function(name) {
unfinished[name] = true;
currentModule = name;
},
moduleDone: function(name, failed, passed, total) {
delete unfinished[name];
},
log: function(result, actual, expected, message, source) {
if (!result) {
failedAssertions.push({
actual: actual, expected: expected, message: message, source: source,
testName: currentTest
});
}
},
testStart: function(name) {
currentTest = (currentModule ? currentModule + ' - ' : '') + name;
grunt.verbose.write(currentTest + '...');
},
testDone: function(name, failed, passed, total) {
// Log errors if necessary, otherwise success.
if (failed > 0) {
// list assertions
if (grunt.option('verbose')) {
grunt.log.error();
logFailedAssertions();
} else {
grunt.log.write('F'.red);
}
} else {
grunt.verbose.ok().or.write('.');
}
},
done: function(failed, passed, total, duration) {
status.failed += failed;
status.passed += passed;
status.total += total;
status.duration += duration;
// Print assertion errors here, if verbose mode is disabled.
if (!grunt.option('verbose')) {
if (failed > 0) {
grunt.log.writeln();
logFailedAssertions();
} else {
grunt.log.ok();
}
}
},
// Error handlers.
done_fail: function(url) {
grunt.verbose.write('Running PhantomJS...').or.write('...');
grunt.log.error();
grunt.warn('PhantomJS unable to load "' + url + '" URI.', 90);
},
done_timeout: function() {
grunt.log.writeln();
grunt.warn('PhantomJS timed out, possibly due to a missing QUnit start() call.', 90);
},
// console.log pass-through.
console: console.log.bind(console),
// Debugging messages.
debug: grunt.log.debug.bind(grunt.log, 'phantomjs')
};
// ==========================================================================
// TASKS
// ==========================================================================
grunt.registerMultiTask('qunit', 'Run QUnit unit tests in a headless PhantomJS instance.', function() {
// Get files as URLs.
var urls = grunt.file.expandFileURLs(this.file.src);
// This task is asynchronous.
var done = this.async();
// Reset status.
status = {failed: 0, passed: 0, total: 0, duration: 0};
// Process each filepath in-order.
grunt.utils.async.forEachSeries(urls, function(url, next) {
var basename = path.basename(url);
grunt.verbose.subhead('Testing ' + basename).or.write('Testing ' + basename);
// Create temporary file to be used for grunt-phantom communication.
var tempfile = new Tempfile();
// Timeout ID.
var id;
// The number of tempfile lines already read.
var n = 0;
// Reset current module.
currentModule = null;
// Clean up.
function cleanup() {
clearTimeout(id);
tempfile.unlink();
}
// It's simple. As QUnit tests, assertions and modules begin and complete,
// the results are written as JSON to a temporary file. This polling loop
// checks that file for new lines, and for each one parses its JSON and
// executes the corresponding method with the specified arguments.
(function loopy() {
// Disable logging temporarily.
grunt.log.muted = true;
// Read the file, splitting lines on \n, and removing a trailing line.
var lines = grunt.file.read(tempfile.path).split('\n').slice(0, -1);
// Re-enable logging.
grunt.log.muted = false;
// Iterate over all lines that haven't already been processed.
var done = lines.slice(n).some(function(line) {
// Get args and method.
var args = JSON.parse(line);
var method = args.shift();
// Execute method if it exists.
if (phantomHandlers[method]) {
phantomHandlers[method].apply(null, args);
}
// If the method name started with test, return true. Because the
// Array#some method was used, this not only sets "done" to true,
// but stops further iteration from occurring.
return (/^done/).test(method);
});
if (done) {
// All done.
cleanup();
next();
} else {
// Update n so previously processed lines are ignored.
n = lines.length;
// Check back in a little bit.
id = setTimeout(loopy, 100);
}
}());
// Launch PhantomJS.
grunt.helper('phantomjs', {
code: 90,
args: [
// PhantomJS options.
'--config=' + grunt.task.getFile('qunit/phantom.json'),
// The main script file.
grunt.task.getFile('qunit/phantom.js'),
// The temporary file used for communications.
tempfile.path,
// The QUnit helper file to be injected.
grunt.task.getFile('qunit/qunit.js'),
// URL to the QUnit .html test file to run.
url
],
done: function(err) {
if (err) {
cleanup();
done();
}
},
});
}, function(err) {
// All tests have been run.
// Log results.
if (status.failed > 0) {
grunt.warn(status.failed + '/' + status.total + ' assertions failed (' +
status.duration + 'ms)', Math.min(99, 90 + status.failed));
} else {
grunt.verbose.writeln();
grunt.log.ok(status.total + ' assertions passed (' + status.duration + 'ms)');
}
// All done!
done();
});
});
// ==========================================================================
// HELPERS
// ==========================================================================
grunt.registerHelper('phantomjs', function(options) {
return grunt.utils.spawn({
cmd: 'phantomjs',
args: options.args
}, function(err, result, code) {
if (!err) { return options.done(null); }
// Something went horribly wrong.
grunt.verbose.or.writeln();
grunt.log.write('Running PhantomJS...').error();
if (code === 127) {
grunt.log.errorlns(
'In order for this task to work properly, PhantomJS must be ' +
'installed and in the system PATH (if you can run "phantomjs" at' +
' the command line, this task should work). Unfortunately, ' +
'PhantomJS cannot be installed automatically via npm or grunt. ' +
'See the grunt FAQ for PhantomJS installation instructions: ' +
'https://github.com/gruntjs/grunt/blob/master/docs/faq.md'
);
grunt.warn('PhantomJS not found.', options.code);
} else {
result.split('\n').forEach(grunt.log.error, grunt.log);
grunt.warn('PhantomJS exited unexpectedly with exit code ' + code + '.', options.code);
}
options.done(code);
});
});
};