index.js 3.8 KB
Newer Older
J
Johannes Rieken 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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');
9 10 11
const path = require('path');
const mocha = require('mocha');
const events = require('events');
J
Johannes Rieken 已提交
12 13

const optimist = require('optimist')
J
Joao Moreno 已提交
14
	.describe('grep', 'only run tests matching <pattern>').alias('grep', 'g').alias('grep', 'f').string('grep')
J
Johannes Rieken 已提交
15
	.describe('run', 'only run tests from <file>').string('run')
J
Johannes Rieken 已提交
16
	.describe('runGrep', 'only run tests matching <file_pattern>').boolean('runGrep')
17
	.describe('build', 'run with build output (out-build)').boolean('build')
J
Johannes Rieken 已提交
18
	.describe('coverage', 'generate coverage report').boolean('coverage')
J
Joao Moreno 已提交
19
	.describe('debug', 'open dev tools, keep window open, reuse app data').string('debug')
20
	.describe('reporter', 'the mocha reporter').string('reporter').default('reporter', 'spec')
J
Joao Moreno 已提交
21
	.describe('help', 'show the help').alias('help', 'h');
J
Johannes Rieken 已提交
22

J
Johannes Rieken 已提交
23
const argv = optimist.argv;
J
Johannes Rieken 已提交
24

J
Joao Moreno 已提交
25 26 27 28 29
if (argv.help) {
	optimist.showHelp();
	process.exit(0);
}

J
Johannes Rieken 已提交
30
if (!argv.debug) {
31 32
	app.setPath('userData', join(tmpdir(), `vscode-tests-${Date.now()}`));
}
J
Johannes Rieken 已提交
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
function deserializeSuite(suite) {
	return {
		title: suite.title,
		fullTitle: () => suite.fullTitle,
		timeout: () => suite.timeout,
		retries: () => suite.retries,
		enableTimeouts: () => suite.enableTimeouts,
		slow: () => suite.slow,
		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)));
	}
}

J
Johannes Rieken 已提交
87 88 89 90 91
app.on('ready', () => {

	const win = new BrowserWindow({
		height: 600,
		width: 800,
92
		show: false,
93 94 95 96
		webPreferences: {
			backgroundThrottling: false,
			webSecurity: false
		}
J
Johannes Rieken 已提交
97 98 99
	});

	win.webContents.on('did-finish-load', () => {
J
Johannes Rieken 已提交
100
		if (argv.debug) {
101
			win.show();
J
Johannes Rieken 已提交
102 103
			win.webContents.openDevTools('right');
		}
J
Johannes Rieken 已提交
104
		win.webContents.send('run', argv);
J
Johannes Rieken 已提交
105 106
	});

J
Johannes Rieken 已提交
107
	win.loadURL(`file://${__dirname}/renderer.html`);
J
Johannes Rieken 已提交
108

109 110
	const reporterPath = path.join(path.dirname(require.resolve('mocha')), 'lib', 'reporters', argv.reporter);
	let Reporter;
J
Johannes Rieken 已提交
111

112 113 114 115 116 117
	try {
		Reporter = require(reporterPath);
	} catch (err) {
		console.warn(`could not load reporter: ${argv.reporter}`);
		Reporter = mocha.reporters.Spec;
	}
J
Johannes Rieken 已提交
118

119 120
	const runner = new IPCRunner();
	new Reporter(runner);
J
Johannes Rieken 已提交
121

122 123 124
	if (!argv.debug) {
		ipcMain.on('all done', () => app.exit(runner.didFail ? 1 : 0));
	}
J
Johannes Rieken 已提交
125
});