forked from DevExpress/testcafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
664 lines (552 loc) · 21.6 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
var babel = require('babel-core');
var gulp = require('gulp');
var gulpBabel = require('gulp-babel');
var less = require('gulp-less');
var filter = require('gulp-filter');
var globby = require('globby');
var qunitHarness = require('gulp-qunit-harness');
var mocha = require('gulp-mocha');
var mustache = require('gulp-mustache');
var rename = require('gulp-rename');
var webmake = require('gulp-webmake');
var util = require('gulp-util');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var ll = require('gulp-ll');
var del = require('del');
var fs = require('fs');
var path = require('path');
var opn = require('opn');
var connect = require('connect');
var spawn = require('cross-spawn');
var serveStatic = require('serve-static');
var Promise = require('pinkie');
var markdownlint = require('markdownlint');
var prompt = require('gulp-prompt');
var nodeVer = require('node-version');
var functionalTestConfig = require('./test/functional/config');
var assignIn = require('lodash').assignIn;
var runSequence = require('run-sequence');
var yaml = require('js-yaml');
var childProcess = require('child_process');
ll
.tasks([
'lint',
'server-scripts'
])
.onlyInDebug([
'styles',
'client-scripts',
'client-scripts-bundle'
]);
var CLIENT_TESTS_PATH = 'test/client/fixtures';
var CLIENT_TESTS_LEGACY_PATH = 'test/client/legacy-fixtures';
var CLIENT_TESTS_SETTINGS_BASE = {
port: 2000,
crossDomainPort: 2001,
scripts: [
{ src: '/async.js', path: 'test/client/vendor/async.js' },
{ src: '/hammerhead.js', path: 'node_modules/testcafe-hammerhead/lib/client/hammerhead.js' },
{ src: '/core.js', path: 'lib/client/core/index.js' },
{ src: '/ui.js', path: 'lib/client/ui/index.js' },
{ src: '/automation.js', path: 'lib/client/automation/index.js' },
{ src: '/driver.js', path: 'lib/client/driver/index.js' },
{ src: '/legacy-runner.js', path: 'node_modules/testcafe-legacy-api/lib/client/index.js' },
{ src: '/before-test.js', path: 'test/client/before-test.js' }
],
configApp: require('./test/client/config-qunit-server-app')
};
var CLIENT_TESTS_SETTINGS = assignIn({}, CLIENT_TESTS_SETTINGS_BASE, { basePath: CLIENT_TESTS_PATH });
var CLIENT_TESTS_LEGACY_SETTINGS = assignIn({}, CLIENT_TESTS_SETTINGS_BASE, { basePath: CLIENT_TESTS_LEGACY_PATH });
var CLIENT_TESTS_DESKTOP_BROWSERS = [
{
platform: 'Windows 10',
browserName: 'microsoftedge'
},
{
platform: 'Windows 10',
browserName: 'chrome'
},
{
platform: 'Windows 10',
browserName: 'firefox'
},
{
platform: 'Windows 10',
browserName: 'internet explorer',
version: '11.0'
},
{
platform: 'OS X 10.11',
browserName: 'safari',
version: '9.0'
},
{
platform: 'OS X 10.11',
browserName: 'chrome'
},
{
platform: 'OS X 10.11',
browserName: 'firefox'
}
];
var CLIENT_TESTS_OLD_BROWSERS = [
{
platform: 'Windows 8',
browserName: 'internet explorer',
version: '10.0'
},
{
platform: 'Windows 7',
browserName: 'internet explorer',
version: '9.0'
}
];
var CLIENT_TESTS_MOBILE_BROWSERS = [
{
platform: 'Linux',
browserName: 'android',
version: '5.1',
deviceName: 'Android Emulator'
},
{
platform: 'OS X 10.10',
browserName: 'iphone',
version: '8.1',
deviceName: 'iPad Simulator'
},
{
platform: 'iOS',
browserName: 'Safari',
// NOTE: https://github.com/DevExpress/testcafe/issues/471
// problem with extra scroll reproduced only on saucelabs
// virtual machines with ios device emulators
version: '9.3',
deviceName: 'iPhone 6 Plus'
}
];
var CLIENT_TESTS_SAUCELABS_SETTINGS = {
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
build: process.env.TRAVIS_BUILD_ID || '',
tags: [process.env.TRAVIS_BRANCH || 'master'],
name: 'testcafe client tests',
timeout: 720
};
var PUBLISH_TAG = JSON.parse(fs.readFileSync(path.join(__dirname, '.publishrc')).toString()).publishTag;
var websiteServer = null;
gulp.task('clean', function () {
return del('lib');
});
// Lint
gulp.task('lint', function () {
// TODO: eslint supports node version 4 or higher.
// Remove this condition once we get rid of node 0.10 support.
if (nodeVer.major === '0')
return null;
var eslint = require('gulp-eslint');
return gulp
.src([
'examples/**/*.js',
'src/**/*.js',
'test/**/*.js',
'!test/client/vendor/**/*.*',
'Gulpfile.js'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// Build
gulp.task('client-scripts', ['client-scripts-bundle'], function () {
var scripts = [
{ wrapper: 'src/client/core/index.js.wrapper.mustache', src: 'lib/client/core/index.js' },
{ wrapper: 'src/client/ui/index.js.wrapper.mustache', src: 'lib/client/ui/index.js' },
{ wrapper: 'src/client/automation/index.js.wrapper.mustache', src: 'lib/client/automation/index.js' },
{ wrapper: 'src/client/driver/index.js.wrapper.mustache', src: 'lib/client/driver/index.js' }
];
return Promise
.all(scripts.map(function (script) {
return gulp
.src(script.wrapper)
.pipe(mustache({ source: fs.readFileSync(script.src).toString() }))
.pipe(rename(path.basename(script.src)))
.pipe(gulpif(!util.env.dev, uglify()))
.pipe(gulp.dest(path.dirname(script.src)));
}));
});
gulp.task('client-scripts-bundle', ['clean'], function () {
var filterBrowserIdlePage = filter('browser/idle-page/index.js', { restore: true });
return gulp
.src([
'src/client/core/index.js',
'src/client/driver/index.js',
'src/client/ui/index.js',
'src/client/automation/index.js',
'src/client/browser/idle-page/index.js'
], { base: 'src' })
.pipe(webmake({
sourceMap: false,
transform: function (filename, code) {
var transformed = babel.transform(code, {
sourceMap: false,
ast: false,
filename: filename,
// NOTE: force usage of client .babelrc for all
// files, regardless of their location
babelrc: false,
extends: path.join(__dirname, './src/client/.babelrc')
});
// HACK: babel-plugin-transform-es2015-modules-commonjs forces
// 'use strict' insertion. We need to remove it manually because
// of https://github.com/DevExpress/testcafe/issues/258
return { code: transformed.code.replace(/^('|")use strict('|");?/, '') };
}
}))
.pipe(filterBrowserIdlePage)
.pipe(gulpif(!util.env.dev, uglify()))
.pipe(filterBrowserIdlePage.restore)
.pipe(gulp.dest('lib'));
});
gulp.task('server-scripts', ['clean'], function () {
return gulp
.src([
'src/**/*.js',
'!src/client/**/*.js'
])
.pipe(gulpBabel())
.pipe(gulp.dest('lib'));
});
gulp.task('styles', ['clean'], function () {
return gulp
.src('src/**/*.less')
.pipe(less())
.pipe(gulp.dest('lib'));
});
gulp.task('templates', ['clean'], function () {
return gulp
.src([
'src/**/*.mustache',
'!src/**/*.js.wrapper.mustache'
])
.pipe(gulp.dest('lib'));
});
gulp.task('images', ['clean'], function () {
return gulp
.src([
'src/**/*.png',
'src/**/*.ico',
'src/**/*.svg'
])
.pipe(gulp.dest('lib'));
});
gulp.task('ts-definitions', ['clean'], function () {
return gulp
.src('src/**/*.d.ts')
.pipe(gulp.dest('lib'));
});
gulp.task('fast-build', ['server-scripts', 'client-scripts', 'styles', 'images', 'templates', 'ts-definitions']);
gulp.task('build', ['lint', 'fast-build']);
// Test
gulp.task('test-server', ['build'], function () {
return gulp
.src('test/server/*-test.js')
.pipe(mocha({
ui: 'bdd',
reporter: 'spec',
timeout: typeof v8debug === 'undefined' ? 2000 : Infinity // NOTE: disable timeouts in debug
}));
});
function testClient (tests, settings, sauselabsSettings) {
return gulp
.src(tests)
.pipe(qunitHarness(settings, sauselabsSettings));
}
gulp.task('test-client', ['build'], function () {
return testClient('test/client/fixtures/**/*-test.js', CLIENT_TESTS_SETTINGS);
});
gulp.task('test-client-legacy', ['build'], function () {
return testClient('test/client/legacy-fixtures/**/*-test.js', CLIENT_TESTS_LEGACY_SETTINGS);
});
gulp.task('test-client-travis', ['build'], function () {
var saucelabsSettings = CLIENT_TESTS_SAUCELABS_SETTINGS;
saucelabsSettings.browsers = CLIENT_TESTS_DESKTOP_BROWSERS;
return testClient('test/client/fixtures/**/*-test.js', CLIENT_TESTS_SETTINGS, saucelabsSettings);
});
gulp.task('test-client-old-browsers-travis', ['build'], function () {
var saucelabsSettings = CLIENT_TESTS_SAUCELABS_SETTINGS;
saucelabsSettings.browsers = CLIENT_TESTS_OLD_BROWSERS;
return testClient('test/client/fixtures/**/*-test.js', CLIENT_TESTS_SETTINGS, saucelabsSettings);
});
gulp.task('test-client-travis-mobile', ['build'], function () {
var saucelabsSettings = CLIENT_TESTS_SAUCELABS_SETTINGS;
saucelabsSettings.browsers = CLIENT_TESTS_MOBILE_BROWSERS;
return testClient('test/client/fixtures/**/*-test.js', CLIENT_TESTS_SETTINGS, saucelabsSettings);
});
gulp.task('test-client-legacy-travis', ['build'], function () {
var saucelabsSettings = CLIENT_TESTS_SAUCELABS_SETTINGS;
saucelabsSettings.browsers = CLIENT_TESTS_DESKTOP_BROWSERS;
return testClient('test/client/legacy-fixtures/**/*-test.js', CLIENT_TESTS_LEGACY_SETTINGS, saucelabsSettings);
});
gulp.task('test-client-legacy-travis-mobile', ['build'], function () {
var saucelabsSettings = CLIENT_TESTS_SAUCELABS_SETTINGS;
saucelabsSettings.browsers = CLIENT_TESTS_MOBILE_BROWSERS;
return testClient('test/client/legacy-fixtures/**/*-test.js', CLIENT_TESTS_LEGACY_SETTINGS, saucelabsSettings);
});
gulp.task('travis', [process.env.GULP_TASK || '']);
//Documentation
gulp.task('generate-docs-readme', function () {
function generateItem (name, url, level) {
return ' '.repeat(level * 2) + '* [' + name + '](articles' + url + ')\n';
}
function generateDirectory (tocItems, level) {
var res = '';
tocItems.forEach(function (item) {
res += generateItem(item.name ? item.name : item.url, item.url, level);
if (item.content)
res += generateDirectory(item.content, level + 1);
});
return res;
}
function generateReadme (toc) {
var tocList = generateDirectory(toc, 0);
return '# Documentation\n\n> This is a development version of the documentation. ' +
'The functionality described here may not be included in the current release version. ' +
'Unreleased functionality may change or be dropped before the next release. ' +
'Documentation for the release version is available at the [TestCafe website](https://devexpress.github.io/testcafe/documentation/getting-started/).\n\n' +
tocList;
}
var toc = yaml.safeLoad(fs.readFileSync('docs/nav/nav-menu.yml', 'utf8'));
var readme = generateReadme(toc);
fs.writeFileSync('docs/README.md', readme);
});
gulp.task('lint-docs', function () {
function lintFiles (files, config) {
return new Promise(function (resolve, reject) {
markdownlint({ files: files, config: config }, function (err, result) {
var lintErr = err || result && result.toString();
if (lintErr)
reject(lintErr);
else
resolve();
});
});
}
var lintDocsAndExamples = globby([
'docs/articles/**/*.md',
'!docs/articles/documentation/recipes/**/*.md',
'examples/**/*.md'
]).then(function (files) {
return lintFiles(files, require('./.md-lint/docs.json'));
});
var lintRecipes = globby([
'docs/articles/documentation/recipes/**/*.md'
]).then(function (files) {
return lintFiles(files, require('./.md-lint/recipes.json'));
});
var lintReadme = lintFiles('README.md', require('./.md-lint/readme.json'));
var lintChangelog = lintFiles('CHANGELOG.md', require('./.md-lint/changelog.json'));
return Promise.all([lintDocsAndExamples, lintReadme, lintChangelog, lintRecipes]);
});
gulp.task('clean-website', function () {
return del('site');
});
gulp.task('fetch-assets-repo', ['clean-website'], function (cb) {
// HACK: A dependency of gulp-git is broken on node 8: https://github.com/aseemk/requireDir/issues/45
var git = require('gulp-git');
git.clone('https://github.com/DevExpress/testcafe-gh-page-assets.git', { args: 'site' }, cb);
});
gulp.task('put-in-articles', ['fetch-assets-repo'], function () {
return gulp
.src(['docs/articles/**/*', '!docs/articles/blog/**/*'])
.pipe(gulp.dest('site/src'));
});
gulp.task('put-in-posts', ['fetch-assets-repo'], function () {
return gulp
.src('docs/articles/blog/**/*')
.pipe(gulp.dest('site/src/_posts'));
});
gulp.task('put-in-navigation', ['fetch-assets-repo'], function () {
return gulp
.src('docs/nav/**/*')
.pipe(gulp.dest('site/src/_data'));
});
gulp.task('prepare-website', ['put-in-articles', 'put-in-navigation', 'put-in-posts', 'lint-docs']);
function buildWebsite (mode, cb) {
var options = mode ? { stdio: 'inherit', env: { JEKYLL_ENV: mode } } : { stdio: 'inherit' };
spawn('jekyll', ['build', '--source', 'site/src/', '--destination', 'site/deploy'], options)
.on('exit', cb);
}
// NOTE: we have three website build configurations.
//
// * production - used when the website is built for publishing. Gulp task 'build-website-production'.
// * development - used when the website is built for local deployment. Gulp task 'build-website-development'.
// * testing - used when the website is built for testing. Gulp task 'build-website-testing'.
//
// This is how they affect the website.
//
// * Blog comments.
// - Do not appear in testing mode.
// - In development mode, comments from an internal 'staging' thread are displayed.
// - In production mode, public comment threads are displayed.
// * Google Analytics is enabled in production mode only.
gulp.task('build-website-production', ['prepare-website'], function (cb) {
buildWebsite('production', cb);
});
gulp.task('build-website-development', ['prepare-website'], function (cb) {
buildWebsite('development', cb);
});
gulp.task('build-website-testing', ['prepare-website'], function (cb) {
buildWebsite('testing', cb);
});
gulp.task('build-website', ['prepare-website'], function (cb) {
buildWebsite('', cb);
});
gulp.task('serve-website', function (cb) {
var app = connect()
.use('/testcafe', serveStatic('site/deploy'));
websiteServer = app.listen(8080, cb);
});
gulp.task('preview-website', function () {
return new Promise(function (resolve) {
runSequence('build-website-development', 'serve-website', resolve);
})
.then(function () {
return opn('http://localhost:8080/testcafe');
});
});
function testWebsite (isTravis) {
return new Promise(function (resolve) {
var buildTask = isTravis ? 'build-website' : 'build-website-testing';
runSequence(buildTask, 'serve-website', resolve);
})
.then(function () {
var WebsiteTester = require('./test/website/test.js');
var websiteTester = new WebsiteTester();
return websiteTester.checkLinks();
})
.then(function (failed) {
return new Promise(function (resolve, reject) {
websiteServer.close(function () {
if (failed)
reject('Broken links found!');
else
resolve();
});
});
});
}
gulp.task('test-website', function () {
return testWebsite(false);
});
gulp.task('test-website-travis', function () {
return testWebsite(true);
});
gulp.task('publish-website', ['build-website-production'], function () {
// NOTE: it's accidentally stopped being compatible with node 0.10 without
// major version bump due to https://github.com/floridoo/gulp-sourcemaps/issues/236,
// so we require it here.
var ghpages = require('gulp-gh-pages');
return gulp
.src('site/deploy/**/*')
.pipe(rename(function (filePath) {
filePath.dirname = filePath.dirname.toLowerCase();
return filePath;
}))
.pipe(prompt.confirm({
message: 'Are you sure you want to publish the website?',
default: false
}))
.pipe(ghpages());
});
gulp.task('test-docs-travis', ['test-website-travis', 'lint']);
function testFunctional (fixturesDir, testingEnvironmentName, browserProviderName) {
process.env.TESTING_ENVIRONMENT = testingEnvironmentName;
process.env.BROWSER_PROVIDER = browserProviderName;
return gulp
.src(['test/functional/setup.js', fixturesDir + '/**/test.js'])
.pipe(mocha({
ui: 'bdd',
reporter: 'spec',
timeout: typeof v8debug === 'undefined' ? 30000 : Infinity // NOTE: disable timeouts in debug
}));
}
gulp.task('test-functional-travis-desktop-osx-and-ms-edge', ['build'], function () {
return testFunctional('test/functional/fixtures', functionalTestConfig.testingEnvironmentNames.osXDesktopAndMSEdgeBrowsers, functionalTestConfig.browserProviderNames.browserstack);
});
gulp.task('test-functional-travis-mobile', ['build'], function () {
return testFunctional('test/functional/fixtures', functionalTestConfig.testingEnvironmentNames.mobileBrowsers, functionalTestConfig.browserProviderNames.browserstack);
});
gulp.task('test-functional-local', ['build'], function () {
return testFunctional('test/functional/fixtures', functionalTestConfig.testingEnvironmentNames.localBrowsers);
});
gulp.task('test-functional-travis-legacy', ['build'], function () {
return testFunctional('test/functional/legacy-fixtures', functionalTestConfig.testingEnvironmentNames.legacy, functionalTestConfig.browserProviderNames.sauceLabs);
});
gulp.task('test-functional-travis-old-browsers', ['build'], function () {
return testFunctional('test/functional/fixtures', functionalTestConfig.testingEnvironmentNames.oldBrowsers, functionalTestConfig.browserProviderNames.sauceLabs);
});
function getDockerEnv (machineName) {
return childProcess
.execSync('docker-machine env --shell bash ' + machineName)
.toString()
.split('\n')
.map(function (line) {
return line.match(/export\s*(.*)="(.*)"$/);
})
.filter(function (match) {
return !!match;
})
.reduce(function (env, match) {
env[match[1]] = match[2];
return env;
}, {});
}
function isDockerMachineRunning (machineName) {
try {
return childProcess.execSync('docker-machine status ' + machineName).toString().match(/Running/);
}
catch (e) {
return false;
}
}
function isDockerMachineExist (machineName) {
try {
childProcess.execSync('docker-machine status ' + machineName);
return true;
}
catch (e) {
return !e.message.match(/Host does not exist/);
}
}
function startDocker () {
var dockerMachineName = process.env['DOCKER_MACHINE_NAME'] || 'default';
if (!isDockerMachineExist(dockerMachineName))
childProcess.execSync('docker-machine create -d virtualbox ' + dockerMachineName);
if (!isDockerMachineRunning(dockerMachineName))
childProcess.execSync('docker-machine start ' + dockerMachineName);
var dockerEnv = getDockerEnv(dockerMachineName);
assignIn(process.env, dockerEnv);
}
gulp.task('docker-build', function () {
if (!process.env['DOCKER_HOST']) {
try {
startDocker();
}
catch (e) {
throw new Error('Unable to initialize Docker environment. Use Docker terminal to run this task.\n' +
e.stack);
}
}
var imageId = childProcess
.execSync('docker build -q -t testcafe -f docker/Dockerfile .', { env: process.env })
.toString()
.replace(/\n/g, '');
childProcess.execSync('docker tag ' + imageId + ' testcafe/testcafe:' + PUBLISH_TAG, {
stdio: 'inherit',
env: process.env
});
});
gulp.task('docker-publish', ['docker-build'], function () {
childProcess.execSync('docker push testcafe/testcafe:' + PUBLISH_TAG, { stdio: 'inherit', env: process.env });
});