vscode/test/electron/index.js

158 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-02-16 13:25:17 +00:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const { app, BrowserWindow, ipcMain } = require('electron');
const { tmpdir } = require('os');
const { join } = require('path');
const path = require('path');
const mocha = require('mocha');
const events = require('events');
2018-06-11 16:18:13 +00:00
const MochaJUnitReporter = require('mocha-junit-reporter');
2017-02-16 13:25:17 +00:00
2018-03-07 21:03:07 +00:00
const defaultReporterName = process.platform === 'win32' ? 'list' : 'spec';
2017-02-16 13:25:17 +00:00
const optimist = require('optimist')
2017-04-12 13:40:08 +00:00
.describe('grep', 'only run tests matching <pattern>').alias('grep', 'g').alias('grep', 'f').string('grep')
2017-02-16 13:49:59 +00:00
.describe('run', 'only run tests from <file>').string('run')
2017-05-22 14:09:58 +00:00
.describe('runGlob', 'only run tests matching <file_pattern>').alias('runGlob', 'runGrep').string('runGlob')
2017-04-04 13:47:15 +00:00
.describe('build', 'run with build output (out-build)').boolean('build')
2017-04-04 14:56:19 +00:00
.describe('coverage', 'generate coverage report').boolean('coverage')
2017-04-12 13:40:08 +00:00
.describe('debug', 'open dev tools, keep window open, reuse app data').string('debug')
2018-03-07 21:03:07 +00:00
.describe('reporter', 'the mocha reporter').string('reporter').default('reporter', defaultReporterName)
.describe('reporter-options', 'the mocha reporter options').string('reporter-options').default('reporter-options', '')
2018-06-12 15:24:23 +00:00
.describe('tfs').string('tfs')
2017-04-12 13:40:08 +00:00
.describe('help', 'show the help').alias('help', 'h');
2017-02-16 13:25:17 +00:00
2017-02-16 13:49:59 +00:00
const argv = optimist.argv;
2017-02-16 13:25:17 +00:00
2017-04-12 13:40:08 +00:00
if (argv.help) {
optimist.showHelp();
process.exit(0);
}
2017-04-04 10:19:44 +00:00
if (!argv.debug) {
2017-02-16 15:07:15 +00:00
app.setPath('userData', join(tmpdir(), `vscode-tests-${Date.now()}`));
}
2017-02-16 13:25:17 +00:00
function deserializeSuite(suite) {
return {
2018-03-07 21:03:07 +00:00
root: suite.root,
suites: suite.suites,
tests: suite.tests,
title: suite.title,
fullTitle: () => suite.fullTitle,
timeout: () => suite.timeout,
retries: () => suite.retries,
enableTimeouts: () => suite.enableTimeouts,
slow: () => suite.slow,
2017-11-11 06:26:47 +00:00
bail: () => suite.bail
};
}
function deserializeRunnable(runnable) {
return {
title: runnable.title,
fullTitle: () => runnable.fullTitle,
async: runnable.async,
slow: () => runnable.slow,
speed: runnable.speed,
duration: runnable.duration
};
}
function deserializeError(err) {
const inspect = err.inspect;
err.inspect = () => inspect;
return err;
}
class IPCRunner extends events.EventEmitter {
constructor() {
super();
this.didFail = false;
ipcMain.on('start', () => this.emit('start'));
ipcMain.on('end', () => this.emit('end'));
ipcMain.on('suite', (e, suite) => this.emit('suite', deserializeSuite(suite)));
ipcMain.on('suite end', (e, suite) => this.emit('suite end', deserializeSuite(suite)));
ipcMain.on('test', (e, test) => this.emit('test', deserializeRunnable(test)));
ipcMain.on('test end', (e, test) => this.emit('test end', deserializeRunnable(test)));
ipcMain.on('hook', (e, hook) => this.emit('hook', deserializeRunnable(hook)));
ipcMain.on('hook end', (e, hook) => this.emit('hook end', deserializeRunnable(hook)));
ipcMain.on('pass', (e, test) => this.emit('pass', deserializeRunnable(test)));
ipcMain.on('fail', (e, test, err) => {
this.didFail = true;
this.emit('fail', deserializeRunnable(test), deserializeError(err));
});
ipcMain.on('pending', (e, test) => this.emit('pending', deserializeRunnable(test)));
}
}
2018-03-07 21:03:07 +00:00
function parseReporterOption(value) {
let r = /^([^=]+)=(.*)$/.exec(value);
return r ? { [r[1]]: r[2] } : {};
}
2017-02-16 13:25:17 +00:00
app.on('ready', () => {
const win = new BrowserWindow({
height: 600,
width: 800,
show: false,
webPreferences: {
backgroundThrottling: false,
webSecurity: false
}
2017-02-16 13:25:17 +00:00
});
win.webContents.on('did-finish-load', () => {
2017-04-04 10:19:44 +00:00
if (argv.debug) {
win.show();
win.webContents.openDevTools({ mode: 'right' });
2017-02-16 13:25:17 +00:00
}
2017-04-04 10:19:44 +00:00
win.webContents.send('run', argv);
2017-02-16 13:25:17 +00:00
});
2017-02-16 13:49:59 +00:00
win.loadURL(`file://${__dirname}/renderer.html`);
2017-02-16 13:25:17 +00:00
2018-03-07 21:03:07 +00:00
const runner = new IPCRunner();
2017-02-16 13:25:17 +00:00
2018-03-07 21:03:07 +00:00
if (argv.tfs) {
2018-06-12 15:24:23 +00:00
new mocha.reporters.Spec(runner);
2018-06-11 19:32:48 +00:00
new MochaJUnitReporter(runner, {
reporterOptions: {
2018-06-12 15:24:23 +00:00
testsuitesTitle: `${argv.tfs} ${process.platform}`,
mochaFile: process.env.BUILD_ARTIFACTSTAGINGDIRECTORY ? path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${argv.tfs.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`) : undefined
2018-06-11 19:32:48 +00:00
}
});
2018-03-07 21:03:07 +00:00
} else {
const reporterPath = path.join(path.dirname(require.resolve('mocha')), 'lib', 'reporters', argv.reporter);
let Reporter;
try {
Reporter = require(reporterPath);
} catch (err) {
try {
Reporter = require(argv.reporter);
} catch (err) {
Reporter = process.platform === 'win32' ? mocha.reporters.List : mocha.reporters.Spec;
console.warn(`could not load reporter: ${argv.reporter}, using ${Reporter.name}`);
}
}
2017-02-16 13:25:17 +00:00
2018-03-07 21:03:07 +00:00
let reporterOptions = argv['reporter-options'];
reporterOptions = typeof reporterOptions === 'string' ? [reporterOptions] : reporterOptions;
reporterOptions = reporterOptions.reduce((r, o) => Object.assign(r, parseReporterOption(o)), {});
new Reporter(runner, { reporterOptions });
}
2017-02-16 13:25:17 +00:00
if (!argv.debug) {
ipcMain.on('all done', () => app.exit(runner.didFail ? 1 : 0));
}
2017-02-16 13:25:17 +00:00
});