-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
116 lines (93 loc) · 3.64 KB
/
index.js
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
const path = require('path');
const childProcess = require('child_process');
const PluginError = require('plugin-error');
const log = require('fancy-log');
const chalk = require('chalk');
const through = require('through2');
const phantomjs = require('phantomjs-prebuilt');
let binPath = phantomjs.path;
module.exports = params => {
const options = params || {};
binPath = options.binPath || binPath;
return through.obj(function (file, enc, cb) {
const absolutePath = path.resolve(file.path);
const isAbsolutePath = absolutePath.indexOf(file.path) >= 0;
let childArgs = [];
const runner = options.runner || require.resolve('qunit-phantomjs-runner');
let proc;
let passed = true;
if (options['phantomjs-options'] && options['phantomjs-options'].length) {
if (Array.isArray(options['phantomjs-options'])) {
childArgs = childArgs.concat(options['phantomjs-options']);
} else {
childArgs.push(options['phantomjs-options']);
}
}
childArgs.push(
runner,
(isAbsolutePath ? `file:///${absolutePath.replace(/\\/g, '/')}` : file.path)
);
if (options.timeout) {
childArgs.push(options.timeout);
}
if (options.page) {
// Push default timeout value unless specified otherwise
if (!options.timeout) {
childArgs.push(5);
}
childArgs.push(JSON.stringify(options.page));
}
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError('gulp-qunit', 'Streaming not supported'));
return;
}
log(`Testing ${chalk.blue(file.relative)}`);
try {
proc = childProcess.spawn(binPath, childArgs);
proc.stdout.on('data', data => {
let out;
let test;
let message;
const line = data.toString().trim();
try {
out = JSON.parse(line);
} catch (err) {
log(line);
return;
}
if (out.exceptions) {
for (test in out.exceptions) {
log(`\n${chalk.red('Test failed')}: ${chalk.red(test)}: \n${out.exceptions[test].join('\n ')}`);
}
}
if (out.result) {
message = `Took ${out.result.runtime} ms to run ${out.result.total} tests. ${out.result.passed} passed, ${out.result.failed} failed.`;
log(out.result.failed > 0 ? chalk.red(message) : chalk.green(message));
}
});
proc.stderr.on('data', function (data) {
const stderr = data.toString().trim();
log(stderr);
this.emit('error', new PluginError('gulp-qunit', stderr));
passed = false;
});
proc.on('close', function (code) {
if (code === 1) {
log(`gulp-qunit: ${chalk.red('✖ ')}QUnit assertions failed in ${chalk.blue(file.relative)}`);
passed = false;
} else {
log(`gulp-qunit: ${chalk.green('✔ ')}QUnit assertions all passed in ${chalk.blue(file.relative)}`);
}
this.emit('gulp-qunit.finished', { 'passed': passed });
});
} catch (e) {
this.emit('error', new PluginError('gulp-qunit', e));
}
this.push(file);
cb();
});
};