all.js 6.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6 7
/*eslint-env mocha*/
/*global define,run*/
E
Erich Gamma 已提交
8 9 10 11 12 13 14 15

var assert = require('assert');
var path = require('path');
var glob = require('glob');
var istanbul = require('istanbul');
var jsdom = require('jsdom-no-contextify');
var minimatch = require('minimatch');
var async = require('async');
A
Alex Dima 已提交
16 17
var fs = require('fs');
var vm = require('vm');
E
Erich Gamma 已提交
18 19 20 21 22 23 24
var TEST_GLOB = '**/test/**/*.test.js';

var optimist = require('optimist')
	.usage('Run the Code tests. All mocha options apply.')
	.describe('build', 'Run from out-build').boolean('build')
	.describe('run', 'Run a single file').string('run')
	.describe('coverage', 'Generate a coverage report').boolean('coverage')
A
Alex Dima 已提交
25
	.describe('forceLoad', 'Force loading').boolean('forceLoad')
E
Erich Gamma 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	.describe('browser', 'Run tests in a browser').boolean('browser')
	.alias('h', 'help').boolean('h')
	.describe('h', 'Show help');

var argv = optimist.argv;

if (argv.help) {
	optimist.showHelp();
	process.exit(1);
}

var out = argv.build ? 'out-build' : 'out';
var loader = require('../' + out + '/vs/loader');
var src = path.join(path.dirname(__dirname), out);

function loadSingleTest(test) {
	var moduleId = path.relative(src, path.resolve(test)).replace(/\.js$/, '');

	return function (cb) {
		define([moduleId], function () {
			cb(null);
J
Joao Moreno 已提交
47
		}, cb);
E
Erich Gamma 已提交
48 49 50 51 52 53 54 55 56
	};
}

function loadClientTests(cb) {
	glob(TEST_GLOB, { cwd: src }, function (err, files) {
		var modules = files.map(function (file) {
			return file.replace(/\.js$/, '');
		});

57
		// load all modules with the AMD loader
E
Erich Gamma 已提交
58 59
		define(modules, function () {
			cb(null);
J
Joao Moreno 已提交
60
		}, cb);
E
Erich Gamma 已提交
61 62 63 64 65 66 67
	});
}

function loadPluginTests(cb) {
	var root = path.join(path.dirname(__dirname), 'extensions');
	glob(TEST_GLOB, { cwd: root }, function (err, files) {

68
		// load modules with commonjs
E
Erich Gamma 已提交
69
		var modules = files.map(function (file) {
70
			return '../extensions/' + file.replace(/\.js$/, '');
E
Erich Gamma 已提交
71
		});
72 73
		modules.forEach(require);
		cb(null);
E
Erich Gamma 已提交
74 75 76 77 78 79 80 81 82
	});
}

function main() {
	process.on('uncaughtException', function (e) {
		console.error(e.stack || e);
	});

	var loaderConfig = {
A
Alex Dima 已提交
83 84
		nodeRequire: require,
		nodeMain: __filename,
85
		baseUrl: path.join(path.dirname(__dirname), 'src'),
E
Erich Gamma 已提交
86
		paths: {
87 88 89
			'vs': `../${ out }/vs`,
			'lib': `../${ out }/lib`,
			'bootstrap': `../${ out }/bootstrap`
E
Erich Gamma 已提交
90 91 92 93 94 95 96
		},
		catchError: true
	};

	if (argv.coverage) {
		var instrumenter = new istanbul.Instrumenter();

A
Alex Dima 已提交
97 98
		var seenSources = {};

E
Erich Gamma 已提交
99
		loaderConfig.nodeInstrumenter = function (contents, source) {
A
Alex Dima 已提交
100 101
			seenSources[source] = true;

E
Erich Gamma 已提交
102 103 104 105 106 107 108 109 110 111 112 113
			if (minimatch(source, TEST_GLOB)) {
				return contents;
			}

			return instrumenter.instrumentSync(contents, source);
		};

		process.on('exit', function (code) {
			if (code !== 0) {
				return;
			}

A
Alex Dima 已提交
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
			if (argv.forceLoad) {
				var allFiles = glob.sync(out + '/vs/**/*.js');
				allFiles = allFiles.map(function(source) {
					return path.join(__dirname, '..', source);
				});
				allFiles = allFiles.filter(function(source) {
					if (seenSources[source]) {
						return false;
					}
					if (minimatch(source, TEST_GLOB)) {
						return false;
					}
					if (/fixtures/.test(source)) {
						return false;
					}
					return true;
				});
				allFiles.forEach(function(source, index) {
					var contents = fs.readFileSync(source).toString();
					contents = instrumenter.instrumentSync(contents, source);
					var stopAt = contents.indexOf('}\n__cov');
					stopAt = contents.indexOf('}\n__cov', stopAt + 1);

					var str = '(function() {' + contents.substr(0, stopAt + 1) + '});';
					var r = vm.runInThisContext(str, source);
					r.call(global);
				});
			}

E
Erich Gamma 已提交
143 144 145
			var collector = new istanbul.Collector();
			collector.add(global.__coverage__);

146
			var reporter = new istanbul.Reporter(null, path.join(path.dirname(__dirname), '.build', 'coverage'));
A
Alex Dima 已提交
147
			reporter.addAll(['json', 'lcov', 'html']);
E
Erich Gamma 已提交
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
			reporter.write(collector, true, function () {});
		});
	}

	loader.config(loaderConfig);

	global.define = loader;
	global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
	global.self = global.window = global.document.parentWindow;

	global.Element = global.window.Element;
	global.HTMLElement = global.window.HTMLElement;
	global.Node = global.window.Node;
	global.navigator = global.window.navigator;
	global.XMLHttpRequest = global.window.XMLHttpRequest;

	var didErr = false;
	var write = process.stderr.write;
	process.stderr.write = function (data) {
		didErr = didErr || !!data;
		write.apply(process.stderr, arguments);
	};

	var loadTasks = [];

	if (argv.run) {
		var tests = (typeof argv.run === 'string') ? [argv.run] : argv.run;

		loadTasks = loadTasks.concat(tests.map(function (test) {
			return loadSingleTest(test);
		}));
	} else {
		loadTasks.push(loadClientTests);
181
		loadTasks.push(loadPluginTests);
E
Erich Gamma 已提交
182 183
	}

J
Joao Moreno 已提交
184 185 186 187 188 189
	async.parallel(loadTasks, function (err) {
		if (err) {
			console.error(err);
			return process.exit(1);
		}

E
Erich Gamma 已提交
190 191 192 193 194 195 196 197 198 199 200
		process.stderr.write = write;

		if (!argv.run) {
			// set up last test
			suite('Loader', function () {
				test('should not explode while loading', function () {
					assert.ok(!didErr, 'should not explode while loading');
				});
			});
		}

J
Joao Moreno 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		// report failing test for every unexpected error during any of the tests
		var unexpectedErrors = [];
		suite('Errors', function () {
			test('should not have unexpected errors in tests', function () {
				if (unexpectedErrors.length) {
					unexpectedErrors.forEach(function (stack) {
						console.error('');
						console.error(stack);
					});

					assert.ok(false);
				}
			});
		});

		// replace the default unexpected error handler to be useful during tests
217 218 219 220 221 222 223 224
		loader(['vs/base/common/errors'], function(errors) {
			errors.setUnexpectedErrorHandler(function (err) {
				try {
					throw new Error('oops');
				} catch (e) {
					unexpectedErrors.push((err && err.message ? err.message : err) + '\n' + e.stack);
				}
			});
J
Joao Moreno 已提交
225

226 227 228
			// fire up mocha
			run();
		});
E
Erich Gamma 已提交
229 230 231 232 233 234 235 236
	});
}

if (process.argv.some(function (a) { return /^--browser/.test(a); })) {
	require('./browser');
} else {
	main();
}