/
runtime-test.js
237 lines (212 loc) · 7.02 KB
/
runtime-test.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
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var argv = require('minimist')(process.argv.slice(2));
var gulp = require('gulp-help')(require('gulp'));
var glob = require('glob');
var Karma = require('karma').Server;
var config = require('../config');
var read = require('file-reader');
var fs = require('fs');
var path = require('path');
var util = require('gulp-util');
var webserver = require('gulp-webserver');
var app = require('../test-server').app;
var karmaDefault = require('./karma.conf');
/**
* Read in and process the configuration settings for karma
* @return {!Object} Karma configuration
*/
function getConfig() {
if (argv.safari) {
return Object.assign({}, karmaDefault, {browsers: ['Safari']});
}
if (argv.firefox) {
return Object.assign({}, karmaDefault, {browsers: ['Firefox']});
}
if (argv.edge) {
return Object.assign({}, karmaDefault, {browsers: ['Edge']});
}
if (argv.saucelabs) {
if (!process.env.SAUCE_USERNAME) {
throw new Error('Missing SAUCE_USERNAME Env variable');
}
if (!process.env.SAUCE_ACCESS_KEY) {
throw new Error('Missing SAUCE_ACCESS_KEY Env variable');
}
return Object.assign({}, karmaDefault, {
reporters: ['dots', 'saucelabs'],
browsers: argv.oldchrome
? ['SL_Chrome_45']
: [
'SL_Chrome_android',
'SL_Chrome_latest',
'SL_Chrome_45',
'SL_Firefox_latest',
//'SL_Safari_8' // Disabled due to flakiness and low market share
'SL_Safari_9',
'SL_Edge_latest',
//'SL_iOS_8_4', // Disabled due to flakiness and low market share
'SL_iOS_9_1',
'SL_iOS_10_0',
//'SL_IE_11',
],
});
}
return karmaDefault;
}
function getAdTypes() {
const namingExceptions = {
// We recommend 3P ad networks use the same string for filename and type.
// Write exceptions here in alphabetic order.
// filename: [type1, type2, ... ]
adblade: ['adblade', 'industrybrains'],
mantis: ['mantis-display', 'mantis-recommend'],
weborama: ['weborama-display'],
};
// Start with Google ad types
const adTypes = ['adsense', 'doubleclick'];
// Add all other ad types
const files = fs.readdirSync('./ads/');
for (var i = 0; i < files.length; i++) {
if (path.extname(files[i]) == '.js'
&& files[i][0] != '_' && files[i] != 'ads.extern.js') {
const adType = path.basename(files[i], '.js');
const expanded = namingExceptions[adType];
if (expanded) {
for (var j = 0; j < expanded.length; j++) {
adTypes.push(expanded[j]);
}
} else {
adTypes.push(adType);
}
}
}
return adTypes;
}
/**
* Run tests.
*/
gulp.task('test', 'Runs tests', argv.nobuild ? [] : ['build'], function(done) {
if (argv.saucelabs && process.env.MAIN_REPO &&
// Sauce Labs does not work on Pull Requests directly.
// The @ampsauce bot builds these.
process.env.TRAVIS_PULL_REQUEST != 'false') {
console./*OK*/info('Deactivated for pull requests. ' +
'The @ampsauce bots build eligible PRs.');
return;
}
if (!argv.integration && process.env.AMPSAUCE_REPO) {
console./*OK*/info('Deactivated for ampsauce repo')
}
var c = getConfig();
if (argv.watch || argv.w) {
c.singleRun = false;
}
if (argv.verbose || argv.v) {
c.client.captureConsole = true;
}
if (argv.files) {
c.files = [].concat(config.commonTestPaths, argv.files);
} else if (argv.integration) {
c.files = config.integrationTestPaths;
} else if (argv.randomize || argv.glob) {
/** Randomize the order of the test running */
var testPaths = [
'test/**/*.js',
'ads/**/test/test-*.js',
'extensions/**/test/**/*.js',
];
var testFiles = [];
for (index in testPaths) {
testFiles = testFiles.concat(glob.sync(testPaths[index]));
}
if (argv.randomize) {
testFiles = shuffleArray(testFiles);
}
// we need to replace the test init with something that won't match
// any file. _init_tests gets added twice due to the regex matching.
testFiles[testFiles.indexOf('test/_init_tests.js')] = '_WONTMATCH.qqq';
c.files = config.commonTestPaths.concat(testFiles);
util.log(util.colors.blue(JSON.stringify(c.files)));
util.log(util.colors.yellow("Save the above files in a .json file to reuse"));
} else if (argv.testlist) {
var file = read.file(argv.testlist);
util.log(file);
c.files = file;
} else {
c.files = config.testPaths;
}
// c.client is available in test browser via window.parent.karma.config
c.client.amp = {
useCompiledJs: !!argv.compiled,
saucelabs: !!argv.saucelabs,
adTypes: getAdTypes(),
};
if (argv.compiled) {
process.env.SERVE_MODE = 'compiled';
} else {
process.env.SERVE_MODE = 'default';
}
if (argv.grep) {
c.client.mocha = {
'grep': argv.grep,
};
}
// Run fake-server to test XHR responses.
var server = gulp.src(process.cwd())
.pipe(webserver({
port: 31862,
host: 'localhost',
directoryListing: true,
middleware: [app],
}));
util.log(util.colors.yellow(
'Started test responses server on localhost:31862'));
new Karma(c, function(exitCode) {
util.log(util.colors.yellow(
'Shutting down test responses server on localhost:31862'));
server.emit('kill');
done(exitCode);
}).start();
}, {
options: {
'verbose': ' With logging enabled',
'watch': ' Watches for changes in files, runs corresponding test(s)',
'saucelabs': ' Runs test on saucelabs (requires setup)',
'safari': ' Runs tests in Safari',
'firefox': ' Runs tests in Firefox',
'edge': ' Runs tests in Edge',
'integration': 'Run only integration tests.',
'compiled': 'Changes integration tests to use production JS ' +
'binaries for execution',
'oldchrome': 'Runs test with an old chrome. Saucelabs only.',
'grep': 'Runs tests that match the pattern',
'files': 'Runs tests for specific files',
'randomize': 'Runs entire test suite in random order',
'testlist': 'Runs tests specified in JSON by supplied file',
'glob': 'Explicility expands test paths using glob before passing' +
'to Karma',
}
});
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}