2017-02-16 14:25:17 +01:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*eslint-env mocha*/
|
|
|
|
|
|
|
|
const { ipcRenderer } = require('electron');
|
|
|
|
const assert = require('assert');
|
|
|
|
const path = require('path');
|
2017-04-04 16:56:19 +02:00
|
|
|
const glob = require('glob');
|
|
|
|
const minimatch = require('minimatch');
|
|
|
|
const istanbul = require('istanbul');
|
|
|
|
const i_remap = require('remap-istanbul/lib/remap');
|
2017-11-22 12:54:11 +01:00
|
|
|
const util = require('util');
|
2018-09-07 12:23:09 +02:00
|
|
|
const bootstrap = require('../../src/bootstrap');
|
2017-11-22 12:54:11 +01:00
|
|
|
|
2017-11-22 12:56:17 +01:00
|
|
|
// Disabled custom inspect. See #38847
|
2017-11-22 12:54:11 +01:00
|
|
|
if (util.inspect && util.inspect['defaultOptions']) {
|
|
|
|
util.inspect['defaultOptions'].customInspect = false;
|
|
|
|
}
|
2017-04-04 16:56:19 +02:00
|
|
|
|
|
|
|
let _tests_glob = '**/test/**/*.test.js';
|
2017-04-04 12:19:44 +02:00
|
|
|
let loader;
|
|
|
|
let _out;
|
|
|
|
|
|
|
|
function initLoader(opts) {
|
|
|
|
let outdir = opts.build ? 'out-build' : 'out';
|
|
|
|
_out = path.join(__dirname, `../../${outdir}`);
|
2017-04-04 16:56:19 +02:00
|
|
|
|
|
|
|
// setup loader
|
2017-04-04 12:19:44 +02:00
|
|
|
loader = require(`${_out}/vs/loader`);
|
2017-04-04 16:56:19 +02:00
|
|
|
const loaderConfig = {
|
2017-04-04 12:19:44 +02:00
|
|
|
nodeRequire: require,
|
|
|
|
nodeMain: __filename,
|
|
|
|
catchError: true,
|
2018-09-07 12:23:09 +02:00
|
|
|
baseUrl: bootstrap.uriFromPath(path.join(__dirname, '../../src')),
|
2017-04-04 12:19:44 +02:00
|
|
|
paths: {
|
|
|
|
'vs': `../${outdir}/vs`,
|
|
|
|
'lib': `../${outdir}/lib`,
|
2018-09-07 12:23:09 +02:00
|
|
|
'bootstrap-fork': `../${outdir}/bootstrap-fork`
|
2017-04-04 12:19:44 +02:00
|
|
|
}
|
2017-04-04 16:56:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// nodeInstrumenter when coverage is requested
|
|
|
|
if (opts.coverage) {
|
|
|
|
const instrumenter = new istanbul.Instrumenter();
|
|
|
|
|
|
|
|
loaderConfig.nodeInstrumenter = function (contents, source) {
|
|
|
|
return minimatch(source, _tests_glob)
|
|
|
|
? contents // don't instrument tests itself
|
|
|
|
: instrumenter.instrumentSync(contents, source);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.require.config(loaderConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCoverageReport(opts) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
|
|
|
if (!opts.coverage) {
|
|
|
|
return resolve(undefined);
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:28:26 +01:00
|
|
|
const exclude = /\b((marked)|(raw\.marked)|(nls)|(css))\.js$/;
|
2017-04-04 16:56:19 +02:00
|
|
|
const remappedCoverage = i_remap(global.__coverage__, { exclude: exclude }).getFinalCoverage();
|
|
|
|
|
|
|
|
// The remapped coverage comes out with broken paths
|
|
|
|
function toUpperDriveLetter(str) {
|
|
|
|
if (/^[a-z]:/.test(str)) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.substr(1);
|
|
|
|
}
|
|
|
|
return str;
|
2017-04-28 11:42:06 +02:00
|
|
|
}
|
2017-04-04 16:56:19 +02:00
|
|
|
function toLowerDriveLetter(str) {
|
|
|
|
if (/^[A-Z]:/.test(str)) {
|
|
|
|
return str.charAt(0).toLowerCase() + str.substr(1);
|
|
|
|
}
|
|
|
|
return str;
|
2017-04-28 11:42:06 +02:00
|
|
|
}
|
2017-04-04 16:56:19 +02:00
|
|
|
|
|
|
|
const REPO_PATH = toUpperDriveLetter(path.join(__dirname, '../..'));
|
|
|
|
const fixPath = function (brokenPath) {
|
|
|
|
const startIndex = brokenPath.indexOf(REPO_PATH);
|
|
|
|
if (startIndex === -1) {
|
|
|
|
return toLowerDriveLetter(brokenPath);
|
|
|
|
}
|
|
|
|
return toLowerDriveLetter(brokenPath.substr(startIndex));
|
|
|
|
};
|
|
|
|
|
|
|
|
const finalCoverage = Object.create(null);
|
|
|
|
for (const entryKey in remappedCoverage) {
|
|
|
|
const entry = remappedCoverage[entryKey];
|
|
|
|
entry.path = fixPath(entry.path);
|
|
|
|
finalCoverage[fixPath(entryKey)] = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
const collector = new istanbul.Collector();
|
|
|
|
collector.add(finalCoverage);
|
|
|
|
|
|
|
|
let coveragePath = path.join(path.dirname(__dirname), '../.build/coverage');
|
|
|
|
let reportTypes = [];
|
2017-04-04 16:59:14 +02:00
|
|
|
if (opts.run || opts.runGlob) {
|
2017-04-04 16:56:19 +02:00
|
|
|
// single file running
|
|
|
|
coveragePath += '-single';
|
|
|
|
reportTypes = ['lcovonly'];
|
|
|
|
} else {
|
|
|
|
reportTypes = ['json', 'lcov', 'html'];
|
|
|
|
}
|
|
|
|
|
|
|
|
const reporter = new istanbul.Reporter(null, coveragePath);
|
|
|
|
reporter.addAll(reportTypes);
|
|
|
|
reporter.write(collector, true, resolve);
|
2017-04-04 12:19:44 +02:00
|
|
|
});
|
|
|
|
}
|
2017-02-16 14:25:17 +01:00
|
|
|
|
2017-02-16 14:49:59 +01:00
|
|
|
function loadTestModules(opts) {
|
|
|
|
|
|
|
|
if (opts.run) {
|
|
|
|
const files = Array.isArray(opts.run) ? opts.run : [opts.run];
|
|
|
|
const modules = files.map(file => {
|
2018-11-28 15:10:47 +01:00
|
|
|
file = file.replace(/^src/, 'out');
|
|
|
|
file = file.replace(/\.ts$/, '.js');
|
2017-04-04 12:19:44 +02:00
|
|
|
return path.relative(_out, file).replace(/\.js$/, '');
|
2017-02-16 14:49:59 +01:00
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
loader.require(modules, resolve, reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:59:14 +02:00
|
|
|
const pattern = opts.runGlob || _tests_glob;
|
|
|
|
|
2017-02-16 14:25:17 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-04-04 16:59:14 +02:00
|
|
|
glob(pattern, { cwd: _out }, (err, files) => {
|
2017-02-16 14:25:17 +01:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const modules = files.map(file => file.replace(/\.js$/, ''));
|
|
|
|
resolve(modules);
|
|
|
|
});
|
|
|
|
}).then(modules => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
loader.require(modules, resolve, reject);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:49:59 +01:00
|
|
|
function loadTests(opts) {
|
2017-02-16 14:25:17 +01:00
|
|
|
|
|
|
|
const _unexpectedErrors = [];
|
|
|
|
const _loaderErrors = [];
|
|
|
|
|
|
|
|
// collect loader errors
|
|
|
|
loader.require.config({
|
|
|
|
onError(err) {
|
|
|
|
_loaderErrors.push(err);
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// collect unexpected errors
|
|
|
|
loader.require(['vs/base/common/errors'], function (errors) {
|
|
|
|
errors.setUnexpectedErrorHandler(function (err) {
|
2017-04-14 23:44:24 +02:00
|
|
|
let stack = (err ? err.stack : null);
|
|
|
|
if (!stack) {
|
|
|
|
stack = new Error().stack;
|
2017-02-16 14:25:17 +01:00
|
|
|
}
|
2017-04-14 23:44:24 +02:00
|
|
|
|
|
|
|
_unexpectedErrors.push((err && err.message ? err.message : err) + '\n' + stack);
|
2017-02-16 14:25:17 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-16 14:49:59 +01:00
|
|
|
return loadTestModules(opts).then(() => {
|
2017-02-16 14:25:17 +01:00
|
|
|
suite('Unexpected Errors & Loader Errors', function () {
|
|
|
|
test('should not have unexpected errors', function () {
|
|
|
|
const errors = _unexpectedErrors.concat(_loaderErrors);
|
|
|
|
if (errors.length) {
|
|
|
|
errors.forEach(function (stack) {
|
|
|
|
console.error('');
|
|
|
|
console.error(stack);
|
|
|
|
});
|
2017-04-14 23:44:24 +02:00
|
|
|
assert.ok(false, errors);
|
2017-02-16 14:25:17 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-28 12:57:09 +02:00
|
|
|
function serializeSuite(suite) {
|
|
|
|
return {
|
2018-03-07 22:03:07 +01:00
|
|
|
root: suite.root,
|
|
|
|
suites: suite.suites.map(serializeSuite),
|
|
|
|
tests: suite.tests.map(serializeRunnable),
|
2017-04-28 12:57:09 +02:00
|
|
|
title: suite.title,
|
|
|
|
fullTitle: suite.fullTitle(),
|
|
|
|
timeout: suite.timeout(),
|
|
|
|
retries: suite.retries(),
|
|
|
|
enableTimeouts: suite.enableTimeouts(),
|
|
|
|
slow: suite.slow(),
|
|
|
|
bail: suite.bail()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeRunnable(runnable) {
|
|
|
|
return {
|
|
|
|
title: runnable.title,
|
|
|
|
fullTitle: runnable.fullTitle(),
|
|
|
|
async: runnable.async,
|
|
|
|
slow: runnable.slow(),
|
|
|
|
speed: runnable.speed,
|
|
|
|
duration: runnable.duration
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeError(err) {
|
|
|
|
return {
|
|
|
|
message: err.message,
|
|
|
|
stack: err.stack,
|
|
|
|
actual: err.actual,
|
|
|
|
expected: err.expected,
|
|
|
|
uncaught: err.uncaught,
|
|
|
|
showDiff: err.showDiff,
|
|
|
|
inspect: typeof err.inspect === 'function' ? err.inspect() : ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class IPCReporter {
|
|
|
|
|
|
|
|
constructor(runner) {
|
|
|
|
runner.on('start', () => ipcRenderer.send('start'));
|
|
|
|
runner.on('end', () => ipcRenderer.send('end'));
|
|
|
|
runner.on('suite', suite => ipcRenderer.send('suite', serializeSuite(suite)));
|
|
|
|
runner.on('suite end', suite => ipcRenderer.send('suite end', serializeSuite(suite)));
|
|
|
|
runner.on('test', test => ipcRenderer.send('test', serializeRunnable(test)));
|
|
|
|
runner.on('test end', test => ipcRenderer.send('test end', serializeRunnable(test)));
|
|
|
|
runner.on('hook', hook => ipcRenderer.send('hook', serializeRunnable(hook)));
|
|
|
|
runner.on('hook end', hook => ipcRenderer.send('hook end', serializeRunnable(hook)));
|
|
|
|
runner.on('pass', test => ipcRenderer.send('pass', serializeRunnable(test)));
|
|
|
|
runner.on('fail', (test, err) => ipcRenderer.send('fail', serializeRunnable(test), serializeError(err)));
|
|
|
|
runner.on('pending', test => ipcRenderer.send('pending', serializeRunnable(test)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:49:59 +01:00
|
|
|
function runTests(opts) {
|
2017-02-16 14:25:17 +01:00
|
|
|
|
2017-02-16 14:49:59 +01:00
|
|
|
return loadTests(opts).then(() => {
|
|
|
|
|
|
|
|
if (opts.grep) {
|
|
|
|
mocha.grep(opts.grep);
|
|
|
|
}
|
2017-02-16 14:25:17 +01:00
|
|
|
|
2017-04-28 12:57:09 +02:00
|
|
|
if (!opts.debug) {
|
|
|
|
mocha.reporter(IPCReporter);
|
|
|
|
}
|
2017-02-16 14:25:17 +01:00
|
|
|
|
2017-06-06 12:57:46 +02:00
|
|
|
const runner = mocha.run(() => {
|
2017-04-28 12:57:09 +02:00
|
|
|
createCoverageReport(opts).then(() => {
|
|
|
|
ipcRenderer.send('all done');
|
2017-02-16 14:25:17 +01:00
|
|
|
});
|
|
|
|
});
|
2017-06-06 12:57:46 +02:00
|
|
|
|
|
|
|
if (opts.debug) {
|
|
|
|
runner.on('fail', (test, err) => {
|
|
|
|
|
|
|
|
console.error(test.fullTitle());
|
|
|
|
console.error(err.stack);
|
|
|
|
});
|
|
|
|
}
|
2017-02-16 14:25:17 +01:00
|
|
|
});
|
2017-02-16 14:49:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ipcRenderer.on('run', (e, opts) => {
|
2017-04-04 12:19:44 +02:00
|
|
|
initLoader(opts);
|
2019-06-24 16:47:26 +02:00
|
|
|
runTests(opts).catch(err => {
|
2019-07-01 09:48:59 +02:00
|
|
|
if (typeof err !== 'string') {
|
2019-06-24 16:47:26 +02:00
|
|
|
err = JSON.stringify(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
ipcRenderer.send('error', err);
|
|
|
|
});
|
2017-02-16 14:49:59 +01:00
|
|
|
});
|