-
Notifications
You must be signed in to change notification settings - Fork 402
/
gulpfile.js
343 lines (312 loc) · 9.26 KB
/
gulpfile.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var gulp = require('gulp'),
Q = require('q'),
path = require('path'),
_ = require('lodash'),
args = require('yargs').argv,
urlLib = require('url'),
mochaStream = require('spawn-mocha-parallel').mochaStream,
httpProxy = require('http-proxy'),
sauceConnectLauncher = require('sauce-connect-launcher'),
async = require('async'),
log = require('fancy-log'),
eslint = require('gulp-eslint'),
gulpIf = require('gulp-if'),
debug = require('gulp-debug');
require('./test/helpers/env');
var VERBOSE = !!process.env.VERBOSE;
args.browsers = (args.browser || 'chrome').split(',');
args.sauce = args.sauce ? true : false;
var BROWSERS = ['chrome', 'firefox'];
if (args.sauce) { BROWSERS.push('explorer'); }
var MOBILE_BROWSERS = ['android', 'ios', 'iphone', 'ipad', 'android_phone'];
process.env.SAUCE_CONNECT_VERSION = process.env.SAUCE_CONNECT_VERSION || '4.5.1';
process.env.SAUCE_CONNECT_VERBOSE = false;
var PROXY_PORT = 5050;
var expressPort = 3000; // incremented after each test to avoid collision
function mochaErrorHandler (err) {
log.error(err);
process.exit(1);
}
function buildMochaOpts(opts) {
var mochaOpts = {
flags: {
u: 'bdd-with-opts',
R: 'spec',
c: true,
},
exit: true,
bin: path.join(__dirname, 'node_modules/.bin/' + ((process.platform !== "win32") ? 'mocha' : 'mocha.cmd')),
concurrency: args.concurrency | process.env.CONCURRENCY || 3
};
if(args.grep) {
mochaOpts.flags.g = args.grep;
}
mochaOpts.env = function() {
var env = _.clone(process.env);
if(opts.unit) {
// unit test
delete env.SAUCE;
delete env.SAUCE_USERNAME;
delete env.SAUCE_ACCESS_KEY;
} else {
// midway + e2e tests
env.BROWSER = opts.browser;
env.SAUCE = args.sauce;
}
if(opts.midway) {
// local server port
env.EXPRESS_PORT = expressPort;
expressPort++;
if(env.SAUCE) {
env.MIDWAY_ROOT_URL = 'http://127.0.0.1:' + PROXY_PORT + '/' +
env.EXPRESS_PORT;
}
if(process.env.TRAVIS_JOB_NUMBER) {
env.TUNNEL_IDENTIFIER = process.env.TRAVIS_JOB_NUMBER;
}
}
return env;
};
return mochaOpts;
}
function runSequence (...args) {
args = _.flattenDeep(args);
return Q.Promise(function (resolve) {
if (args.length === 0) {
return resolve();
}
// do the tasks in series
(gulp.series(...args, function finishSequence (done) {
done();
resolve();
}))();
});
}
gulp.task('lint', function() {
var opts = {
fix: process.argv.indexOf('--fix') !== -1,
};
return gulp.src(['*.js', 'lib/**/*.js', 'test/**/*.js', '!node_modules', '!**/node_modules', '!build/**'])
.pipe(gulpIf(!!process.env.VERBOSE, debug()))
.pipe(eslint(opts))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.pipe(gulpIf(function (file) {
return file.eslint && file.eslint.fixed;
}, gulp.dest(process.cwd())));
});
gulp.task('test:unit', function () {
var opts = buildMochaOpts({ unit: true });
var mocha = mochaStream(opts);
return gulp.src('test/specs/**/*-specs.js', {read: false})
.pipe(gulpIf(VERBOSE, debug()))
.pipe(mocha)
.on('error', mochaErrorHandler);
});
gulp.task('test:midway:multi', function () {
var opts = buildMochaOpts({ midway: true, browser: 'multi' });
var mocha = mochaStream(opts);
return gulp.src('test/midway/multi/**/*-specs.js', {
read: false})
.pipe(gulpIf(VERBOSE, debug()))
.pipe(mocha)
.on('error', mochaErrorHandler);
});
// create a test:midway: and test:e2e: task for each browser
_(BROWSERS).each(function(browser) {
gulp.task(`test:midway:${browser}`, function () {
var opts = buildMochaOpts({ midway: true, browser: browser });
var mocha = mochaStream(opts);
return gulp.src([
'test/midway/**/*-specs.js',
'!test/midway/multi/**'
], {read: false})
.pipe(gulpIf(VERBOSE, debug()))
.pipe(mocha)
.on('error', mochaErrorHandler);
});
gulp.task(`test:e2e:${browser}`, function () {
var opts = buildMochaOpts({ browser: browser });
var mocha = mochaStream(opts);
return gulp.src('test/e2e/**/*-specs.js', {read: false})
.pipe(gulpIf(VERBOSE, debug()))
.pipe(mocha)
.on('error', mochaErrorHandler);
});
});
// create a test:midway: task for each mobile browser
_(MOBILE_BROWSERS).each(function (browser) {
gulp.task(`test:midway:${browser}`, function () {
var opts = buildMochaOpts({ midway: true, browser: browser });
return gulp.src([
'test/midway/api-nav-specs.js',
'test/midway/api-el-specs.js',
'test/midway/api-exec-specs.js',
'test/midway/mobile-specs.js',
], {read: false})
.pipe(gulpIf(VERBOSE, debug()))
.pipe(mochaStream(opts))
.on('error', mochaErrorHandler);
});
});
gulp.task('test:midway', function() {
const midwayTestTasks = _.map(args.browsers, (browser) =>`test:midway:${browser}`);
return runSequence('pre:midway', ...midwayTestTasks)
.finally(function () {
return runSequence('post:midway');
});
});
gulp.task('test:e2e', function() {
var e2eTestTasks = [];
_(args.browsers).chain().without('multi').each(function(browser) {
e2eTestTasks.push(`test:e2e:${browser}`);
}).value();
return runSequence(e2eTestTasks);
});
gulp.task('test', function() {
var seq = ['lint', 'test:unit', 'test:midway:multi'];
_(BROWSERS).each(function(browser) {
seq.push(`test:midway:${browser}`, `test:e2e:${browser}`);
});
return runSequence.apply(null, seq);
});
var server;
gulp.task('proxy:start', function(done) {
var proxy = httpProxy.createProxyServer({});
var proxyQueue;
var throttle = args.throttle || process.env.THROTTLE;
if(throttle) {
proxyQueue = async.queue(function(task, done) {
proxy.web(task.req, task.res, { target: 'http://127.0.0.1:' + task.port });
task.res.on('finish', function() {
done();
});
}, parseInt(args.throttle, 10));
}
server = require('http').createServer(function(req, res) {
try {
if(req.url.match(/^\/favicon/)) {
res.write("404 Not Found\n");
res.end();
return;
}
// extracting port from url and rewriting url
var url = urlLib.parse(req.url);
var re = /\/(.*?)\//;
var m = re.exec(url.pathname);
var port = parseInt(m[1]);
url.pathname = url.pathname.replace(re, '/');
req.url = url.format();
if(throttle) {
proxyQueue.push({req: req, res: res, port: port});
} else {
proxy.web(req, res, { target: 'http://127.0.0.1:' + port });
}
} catch (err) {
try{
log.error('Proxy error for: ', req.url + ':' , err);
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong.');
} catch (ign) {}
}
});
server.on('error', function(err) {
log.error(`Proxy error: ${err}`);
});
log("Listening on port", PROXY_PORT);
server.listen(PROXY_PORT, done);
});
gulp.task('proxy:stop', function(done) {
// stop proxy, exit after 5 sec if hanging
done = _.once(done);
var timeout = setTimeout(function () {
done();
}, 5000);
if (server) {
server.close(function () {
clearTimeout(timeout);
done();
});
}
else {
done();
}
});
var sauceConnectProcess = null;
gulp.task('sc:start', function (done) {
var opts = {
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
verbose: process.env.SAUCE_CONNECT_VERBOSE,
directDomains: 'cdnjs.cloudflare.com,html5shiv.googlecode.com',
logger: log.bind(log),
};
if (process.env.TRAVIS_JOB_NUMBER) {
opts.tunnelIdentifier = process.env.TRAVIS_JOB_NUMBER;
}
var startTunnel = function (done, n) {
sauceConnectLauncher(opts, function (err, _sauceConnectProcess) {
if (err) {
if (n > 0) {
log('retrying sauce connect in 20 secs.');
setTimeout(function () {
startTunnel(done, n - 1);
}, 20000);
} else {
log.error(err.message);
done(err);
}
return;
}
sauceConnectProcess = _sauceConnectProcess;
log("Sauce Connect ready");
done();
});
};
startTunnel(done, 3);
});
gulp.task('sc:stop', function (done) {
if(sauceConnectProcess) {
sauceConnectProcess.close(done);
} else {
done();
}
});
gulp.task('pre:midway', function() {
var seq = args.sauce && !args.nosc
? ['sc:start', 'proxy:start']
: ['proxy:start'];
return runSequence(seq);
});
gulp.task('post:midway', function () {
var seq = args.sauce && !args['nosc']
? ['sc:stop', 'proxy:stop']
: ['proxy:stop'];
return runSequence(seq);
});
gulp.task('travis', function() {
var seq;
switch (args.config) {
case 'unit':
return runSequence(['test:unit']);
case 'multi':
args.browsers= [args.config];
return runSequence(['test:midway']);
case 'chrome':
case 'firefox':
case 'explorer':
args.browsers= [args.config];
return runSequence(['test:midway', 'test:e2e']);
case 'iphone':
case 'ipad':
case 'android_phone':
args.browsers= [args.config];
return runSequence(['test:midway']);
case 'chrome_e2e':
args.browsers= ['chrome'];
return runSequence(['test:e2e']);
}
return runSequence.apply(null, seq);
});