-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
297 lines (261 loc) · 8.44 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
'use strict';
var _ = require('lodash'),
exec = require('child_process').execSync,
execFile = require('child_process').execFile,
fs = require('fs'),
glob = require('glob'),
gutil = require('gulp-util'),
handlebar = require('handlebars'),
Jasmine = require('jasmine'),
path = require('path'),
through = require('through2');
/*
* Global variables
*
* gulpOptions: object of options passed in through Gulp
* jasmineCSS: string path to the jasmine.css file for the specRunner.html
* jasmineJS: array of string paths to JS needed for the specRunner.html
* specHtml: string path to the tmp specRunner.html to be written out to
* specRunner: string path to the specRunner JS file needed in the specRunner.html
**/
var phantomExecutable = 'phantomjs',
gulpOptions = {},
jasmineCss, jasmineJs,
vendorJs = [],
specHtml = path.join(__dirname, '/lib/specRunner.html'),
specRunner = path.join(__dirname, '/lib/specRunner.js');
function configJasmine(version) {
version = version || '2.0';
jasmineCss = path.join(__dirname, '/vendor/jasmine-' + version + '/jasmine.css');
jasmineJs = [
path.join(__dirname, '/vendor/jasmine-' + version + '/jasmine.js'),
path.join(__dirname, '/vendor/jasmine-' + version + '/jasmine-html.js'),
path.join(__dirname, '/vendor/jasmine-' + version + '/console.js'),
path.join(__dirname, '/vendor/jasmine-' + version + '/boot.js')
];
}
/**
* Removes the specRunner.html file
**/
function cleanup(path) {
fs.unlink(path);
}
function hasGlobalPhantom() {
if(process.platform === 'win32') {
try {
exec('where phantomjs');
} catch (e) {
return false;
}
} else {
try {
exec('which phantomjs');
} catch (e) {
return false;
}
}
return true;
}
/**
* execPhantom
*
* @param {string} phantom Path to phantom
* @param {array} childArguments Array of options to pass to Phantom
* @param {function} onComplete Callback function
*/
function execPhantom(phantom, childArguments, onComplete) {
execFile(phantom, childArguments, function(error, stdout, stderr) {
var success = null;
if(error !== null) {
success = new gutil.PluginError('gulp-jasmine-phantomjs', error.code + ': Tests contained failures. Check logs for details.');
}
if (stderr !== '') {
gutil.log('gulp-jasmine-phantom: Failed to open test runner ' + gutil.colors.blue(childArguments[1]));
gutil.log(gutil.colors.red('error: '), stderr);
success = new gutil.PluginError('gulp-jasmine-phantomjs', 'Failed to open test runner ' + gutil.colors.blue(childArguments[1]));
}
if(gulpOptions.specHtml === undefined && (gulpOptions.keepRunner === undefined || gulpOptions.keepRunner === false)) {
cleanup(childArguments[1]);
}
console.log(stdout);
onComplete(success);
});
}
/**
* Executes Phantom with the specified arguments
*
* childArguments: Array of options to pass Phantom
* [jasmine-runner.js, specRunner.html]
**/
function runPhantom(childArguments, onComplete) {
if(hasGlobalPhantom()) {
execPhantom(phantomExecutable, childArguments, onComplete);
} else {
gutil.log(gutil.colors.yellow('gulp-jasmine-phantom: Global Phantom undefined, trying to execute from node_modules/phantomjs'));
execPhantom(process.cwd() + '/node_modules/.bin/' + phantomExecutable, childArguments, onComplete);
}
}
/**
* Converts a file path into a form able to be loaded by phantom. An absolute path (e.g. c:\dir\file.js) will be converted to
* a file URL (e.g., file:///c:/dir/file.js)
*
* path: a file path to convert
*/
function fixupPath(path) {
if (path.match(/^http/)) {
return path;
}
return "file:///" + path.replace(/\\/g, '/');
}
/*
* Reads in the handlebar template and creates a data HTML object in memory to create
*
* options: list of options that can be passed to the function
* files: paths to files being tested
* onComplete: callback to call when everything is done
**/
function compileRunner(options) {
var filePaths = options.files || [],
onComplete = options.onComplete || {};
fs.readFile(path.join(__dirname, '/lib/specRunner.handlebars'), 'utf8', function(error, data) {
if (error) {
throw error;
}
var vendorScripts = gulpOptions.vendor;
if (vendorScripts) {
if (typeof vendorScripts === 'string') {
vendorScripts = [vendorScripts];
}
vendorScripts.forEach(function(fileGlob) {
if (fileGlob.match(/^http/)) {
vendorJs.push(fileGlob);
}
else {
glob.sync(fileGlob).forEach(function(newFile) {
vendorJs.push(path.join(process.cwd(), newFile));
});
}
});
}
// Create the compile version of the specRunner from Handlebars
var specData = handlebar.compile(data),
specCompiled = specData({
files: filePaths.map(fixupPath),
jasmineCss: fixupPath(jasmineCss),
jasmineJs: jasmineJs.map(fixupPath),
vendorJs: vendorJs.map(fixupPath),
specRunner: fixupPath(specRunner)
});
if(gulpOptions.keepRunner !== undefined && typeof gulpOptions.keepRunner === 'string') {
specHtml = path.join(path.resolve(gulpOptions.keepRunner), '/specRunner.html');
}
fs.writeFile(specHtml, specCompiled , function(error) {
if (error) {
throw error;
}
if (gulpOptions.integration) {
var childArgs = [
options.runner,
specHtml,
JSON.stringify(gulpOptions)
];
runPhantom(childArgs, onComplete);
} else {
onComplete(null);
}
});
});
}
module.exports = function (options) {
var filePaths = [];
gulpOptions = options || {};
configJasmine(gulpOptions.jasmineVersion);
if(!!gulpOptions.integration) {
return through.obj(
function (file, encoding, callback) {
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
callback(new gutil.PluginError('gulp-jasmine-phantom', 'Streaming not supported'));
return;
}
filePaths.push(file.path);
callback(null, file);
}, function (callback) {
gutil.log('Running Jasmine with PhantomJS');
try {
var runner = gulpOptions.runner || path.join(__dirname, '/lib/jasmine-runner.js');
if (gulpOptions.specHtml) {
runPhantom(
[
runner,
path.resolve(gulpOptions.specHtml),
JSON.stringify(gulpOptions)
], function(success) {
callback(success);
});
} else {
compileRunner({
files: filePaths,
onComplete: function(success) {
callback(success);
},
runner: runner
});
}
} catch(error) {
callback(new gutil.PluginError('gulp-jasmine-phantom', error));
}
}
);
}
return through.obj(
function(file, encoding, callback) {
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
callback(new gutil.PluginError('gulp-jasmine-phantom', 'Streaming not supported'));
return;
}
/**
* Get the cache object of the specs.js file,
* get its children and delete the childrens cache
*/
var modId = require.resolve(path.resolve(file.path));
var files = require.cache[modId];
if (typeof files !== 'undefined') {
for (var i in files.children) {
delete require.cache[files.children[i].id];
}
}
delete require.cache[modId];
filePaths.push(path.relative(process.cwd(), file.path));
callback(null, file);
},
function(callback) {
gutil.log('Running Jasmine in Node');
try {
var jasmine = new Jasmine(),
Reporter = gulpOptions.reporter || require('./lib/terminal-reporter.js').TerminalReporter;
jasmine.addReporter(new Reporter(_.defaults(gulpOptions, {showColors: true})));
jasmine.loadConfig({
random: _.get(gulpOptions, 'random', false),
spec_files: filePaths
});
if (_.has(gulpOptions, 'seed')) {
jasmine.seed(gulpOptions.seed);
}
jasmine.onComplete(function(passed) {
callback(null);
});
jasmine.execute();
} catch(error) {
callback(new gutil.PluginError('gulp-jasmine-phantom', error));
}
}
);
};