forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest
More file actions
executable file
·500 lines (451 loc) · 13.4 KB
/
Copy pathtest
File metadata and controls
executable file
·500 lines (451 loc) · 13.4 KB
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
#!/usr/bin/env node
'use strict';
require('colors');
const argv = require('yargs')
.option('core-main', {
describe: 'Run core main process tests',
boolean: true,
default: false
})
.option('skip-main', {
describe:
'Skip main process tests if they would otherwise run on your platform',
boolean: true,
default: false,
conflicts: 'core-main'
})
.option('core-renderer', {
describe: 'Run core renderer process tests',
boolean: true,
default: false
})
.option('core-benchmark', {
describe: 'Run core benchmarks',
boolean: true,
default: false
})
.option('package', {
describe: 'Run bundled package specs',
boolean: true,
default: false
})
.help().argv;
const assert = require('assert');
const async = require('async');
const childProcess = require('child_process');
const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const temp = require('temp').track();
const CONFIG = require('./config');
const backupNodeModules = require('./lib/backup-node-modules');
const runApmInstall = require('./lib/run-apm-install');
function assertExecutablePaths(executablePaths) {
assert(
executablePaths.length !== 0,
`No atom build found. Please run "script/build" and try again.`
);
assert(
executablePaths.length === 1,
`More than one application to run tests against was found. ${executablePaths.join(
','
)}`
);
}
const resourcePath = CONFIG.repositoryRootPath;
let executablePath;
if (process.platform === 'darwin') {
const executablePaths = glob.sync(path.join(CONFIG.buildOutputPath, '*.app'));
assertExecutablePaths(executablePaths);
executablePath = path.join(
executablePaths[0],
'Contents',
'MacOS',
path.basename(executablePaths[0], '.app')
);
} else if (process.platform === 'linux') {
const executablePaths = glob.sync(
path.join(CONFIG.buildOutputPath, 'atom-*', 'atom')
);
assertExecutablePaths(executablePaths);
executablePath = executablePaths[0];
} else if (process.platform === 'win32') {
const executablePaths = glob.sync(
path.join(CONFIG.buildOutputPath, '**', 'atom*.exe')
);
assertExecutablePaths(executablePaths);
executablePath = executablePaths[0];
} else {
throw new Error('##[error] Running tests on this platform is not supported.');
}
function prepareEnv(suiteName) {
const atomHomeDirPath = temp.mkdirSync(suiteName);
const env = Object.assign({}, process.env, { ATOM_HOME: atomHomeDirPath });
if (process.env.TEST_JUNIT_XML_ROOT) {
// Tell Jasmine to output this suite's results as a JUnit XML file to a subdirectory of the root, so that a
// CI system can interpret it.
const fileName = suiteName + '.xml';
const outputPath = path.join(process.env.TEST_JUNIT_XML_ROOT, fileName);
env.TEST_JUNIT_XML_PATH = outputPath;
}
return env;
}
function spawnTest(
executablePath,
testArguments,
options,
callback,
testName,
finalize = null
) {
const cp = childProcess.spawn(executablePath, testArguments, options);
// collect outputs and errors
let stderrOutput = '';
if (cp.stdout) {
cp.stderr.on('data', data => {
stderrOutput += data;
});
cp.stdout.on('data', data => {
stderrOutput += data;
});
}
// on error
cp.on('error', error => {
console.log(error, 'error');
if (finalize) {
finalize();
} // if finalizer provided
callback(error);
});
// on close
cp.on('close', exitCode => {
if (exitCode !== 0) {
retryOrFailTest(
stderrOutput,
exitCode,
executablePath,
testArguments,
options,
callback,
testName,
finalize
);
} else {
// successful test
if (finalize) {
finalize();
} // if finalizer provided
callback(null, {
exitCode,
step: testName,
testCommand: `You can run the test again using: \n\t ${executablePath} ${testArguments.join(
' '
)}`
});
}
});
}
const retryNumber = 6; // the number of times a tests repeats
const retriedTests = new Map(); // a cache of retried tests
// Retries the tests if it is timed out for a number of times. Fails the rest of the tests or those that are tried enough times.
function retryOrFailTest(
stderrOutput,
exitCode,
executablePath,
testArguments,
options,
callback,
testName,
finalize
) {
const testKey = createTestKey(executablePath, testArguments, testName);
if (isTimedOut(stderrOutput) && shouldTryAgain(testKey)) {
// retry the timed out test
let triedNumber = retriedTests.get(testKey) || 0;
retriedTests.set(testKey, triedNumber + 1);
console.warn(`\n##[warning] Retrying the timed out step: ${testName} \n`);
spawnTest(
executablePath,
testArguments,
options,
callback,
testName,
finalize
);
} else {
// fail the test
if (finalize) {
finalize();
} // if finalizer provided
console.log(`##[error] Tests for ${testName} failed.`.red);
console.log(stderrOutput);
callback(null, {
exitCode,
step: testName,
testCommand: `You can run the test again using: \n\t ${executablePath} ${testArguments.join(
' '
)}`
});
}
}
// creates a key that is specific to a certain test
function createTestKey(executablePath, testArguments, testName) {
return `${executablePath} ${testArguments.join(' ')} ${testName}`;
}
// check if a test is timed out
function isTimedOut(stderrOutput) {
if (stderrOutput) {
return (
stderrOutput.includes('timeout: timed out after') || // happens in core renderer tests
stderrOutput.includes('Error: Timed out waiting on') || // happens in core renderer tests
stderrOutput.includes('Error: timeout of') || // happens in core main tests
stderrOutput.includes(
'Error Downloading Update: Could not get code signature for running application'
) // happens in github tests
);
} else {
return false;
}
}
// check if a tests should be tried again
function shouldTryAgain(testKey) {
if (retriedTests.has(testKey)) {
return retriedTests.get(testKey) < retryNumber;
} else {
return true;
}
}
function runCoreMainProcessTests(callback) {
const testPath = path.join(CONFIG.repositoryRootPath, 'spec', 'main-process');
const testArguments = [
'--resource-path',
resourcePath,
'--test',
'--main-process',
testPath
];
if (process.env.CI && process.platform === 'linux') {
testArguments.push('--no-sandbox');
}
const testEnv = Object.assign({}, prepareEnv('core-main-process'), {
ATOM_GITHUB_INLINE_GIT_EXEC: 'true'
});
console.log('##[command] Executing core main process tests'.bold.green);
spawnTest(
executablePath,
testArguments,
{ stdio: 'inherit', env: testEnv },
callback,
'core-main-process'
);
}
function getCoreRenderProcessTestSuites() {
// Build an array of functions, each running tests for a different rendering test
const coreRenderProcessTestSuites = [];
const testPath = path.join(CONFIG.repositoryRootPath, 'spec');
let testFiles = glob.sync(
path.join(testPath, '*-spec.+(js|coffee|ts|jsx|tsx|mjs)')
);
for (let testFile of testFiles) {
const testArguments = ['--resource-path', resourcePath, '--test', testFile];
// the function which runs by async:
coreRenderProcessTestSuites.push(function(callback) {
const testEnv = prepareEnv('core-render-process');
console.log(
`##[command] Executing core render process tests for ${testFile}`.bold
.green
);
spawnTest(
executablePath,
testArguments,
{ env: testEnv },
callback,
`core-render-process in ${testFile}.`
);
});
}
return coreRenderProcessTestSuites;
}
function getPackageTestSuites() {
// Build an array of functions, each running tests for a different bundled package
const packageTestSuites = [];
for (let packageName in CONFIG.appMetadata.packageDependencies) {
if (process.env.ATOM_PACKAGES_TO_TEST) {
const packagesToTest = process.env.ATOM_PACKAGES_TO_TEST.split(',').map(
pkg => pkg.trim()
);
if (!packagesToTest.includes(packageName)) continue;
}
const repositoryPackagePath = path.join(
CONFIG.repositoryRootPath,
'node_modules',
packageName
);
const testSubdir = ['spec', 'test'].find(subdir =>
fs.existsSync(path.join(repositoryPackagePath, subdir))
);
if (!testSubdir) {
console.log(`No test folder found for package: ${packageName}`.yellow);
continue;
}
const testFolder = path.join(repositoryPackagePath, testSubdir);
const testArguments = [
'--resource-path',
resourcePath,
'--test',
testFolder
];
const pkgJsonPath = path.join(repositoryPackagePath, 'package.json');
const nodeModulesPath = path.join(repositoryPackagePath, 'node_modules');
// the function which runs by async:
packageTestSuites.push(function(callback) {
const testEnv = prepareEnv(`bundled-package-${packageName}`);
let finalize = () => null;
if (require(pkgJsonPath).atomTestRunner) {
console.log(
`##[command] Installing test runner dependencies for ${packageName}`
.bold.green
);
if (fs.existsSync(nodeModulesPath)) {
const backup = backupNodeModules(repositoryPackagePath);
finalize = backup.restore;
} else {
finalize = () => fs.removeSync(nodeModulesPath);
}
runApmInstall(repositoryPackagePath);
console.log(`##[command] Executing ${packageName} tests`.green);
} else {
console.log(`##[command] Executing ${packageName} tests`.bold.green);
}
spawnTest(
executablePath,
testArguments,
{ env: testEnv },
callback,
`${packageName} package`,
finalize
);
});
}
return packageTestSuites;
}
function runBenchmarkTests(callback) {
const benchmarksPath = path.join(CONFIG.repositoryRootPath, 'benchmarks');
const testArguments = ['--benchmark-test', benchmarksPath];
const testEnv = prepareEnv('benchmark');
console.log('##[command] Executing benchmark tests'.bold.green);
spawnTest(
executablePath,
testArguments,
{ stdio: 'inherit', env: testEnv },
callback,
`core-benchmarks`
);
}
let testSuitesToRun = requestedTestSuites(process.platform);
function requestedTestSuites(platform) {
// env variable or argv options
let coreAll = process.env.ATOM_RUN_CORE_TESTS === 'true';
let coreMain =
process.env.ATOM_RUN_CORE_MAIN_TESTS === 'true' || argv.coreMain;
let coreRenderer =
argv.coreRenderer || process.env.ATOM_RUN_CORE_RENDER_TESTS === 'true';
let coreRenderer1 = process.env.ATOM_RUN_CORE_RENDER_TESTS === '1';
let coreRenderer2 = process.env.ATOM_RUN_CORE_RENDER_TESTS === '2';
let packageAll =
argv.package || process.env.ATOM_RUN_PACKAGE_TESTS === 'true';
let packages1 = process.env.ATOM_RUN_PACKAGE_TESTS === '1';
let packages2 = process.env.ATOM_RUN_PACKAGE_TESTS === '2';
let benchmark = argv.coreBenchmark;
// Operating system overrides:
coreMain =
coreMain ||
platform === 'linux' ||
(platform === 'win32' && process.arch === 'x86');
// split package tests (used for macos in CI)
const PACKAGES_TO_TEST_IN_PARALLEL = 23;
// split core render test (used for windows x64 in CI)
const CORE_RENDER_TO_TEST_IN_PARALLEL = 45;
let suites = [];
// Core tess
if (coreAll) {
suites.push(
...[runCoreMainProcessTests, ...getCoreRenderProcessTestSuites()]
);
} else {
// Core main tests
if (coreMain) {
suites.push(runCoreMainProcessTests);
}
// Core renderer tests
if (coreRenderer) {
suites.push(...getCoreRenderProcessTestSuites());
} else {
// split
if (coreRenderer1) {
suites.push(
...getCoreRenderProcessTestSuites().slice(
0,
CORE_RENDER_TO_TEST_IN_PARALLEL
)
);
}
if (coreRenderer2) {
suites.push(
...getCoreRenderProcessTestSuites().slice(
CORE_RENDER_TO_TEST_IN_PARALLEL
)
);
}
}
}
// Package tests
if (packageAll) {
suites.push(...getPackageTestSuites());
} else {
// split
if (packages1) {
suites.push(
...getPackageTestSuites().slice(0, PACKAGES_TO_TEST_IN_PARALLEL)
);
}
if (packages2) {
suites.push(
...getPackageTestSuites().slice(PACKAGES_TO_TEST_IN_PARALLEL)
);
}
}
// Benchmark tests
if (benchmark) {
suites.push(runBenchmarkTests);
}
if (argv.skipMainProcessTests) {
suites = suites.filter(suite => suite !== runCoreMainProcessTests);
}
// Remove duplicates
suites = Array.from(new Set(suites));
if (suites.length === 0) {
throw new Error('No tests was requested');
}
return suites;
}
async.series(testSuitesToRun, function(err, results) {
if (err) {
console.error(err);
process.exit(1);
} else {
const failedSteps = results.filter(({ exitCode }) => exitCode !== 0);
if (failedSteps.length > 0) {
console.warn(
'\n \n ##[error] *** Reporting the errors that happened in all of the tests: *** \n \n'
);
for (const { step, testCommand } of failedSteps) {
console.error(
`##[error] The '${step}' test step finished with a non-zero exit code \n ${testCommand}`
);
}
process.exit(1);
}
process.exit(0);
}
});