Squashed 'lib/vscode/' content from commit e5a624b788
git-subtree-dir: lib/vscode git-subtree-split: e5a624b788d92b8d34d1392e4c4d9789406efe8f
This commit is contained in:
40
test/unit/README.md
Normal file
40
test/unit/README.md
Normal file
@ -0,0 +1,40 @@
|
||||
# Unit Tests
|
||||
|
||||
## Run (inside Electron)
|
||||
|
||||
./scripts/test.[sh|bat]
|
||||
|
||||
All unit tests are run inside a electron-browser environment which access to DOM and Nodejs api. This is the closest to the environment in which VS Code itself ships. Notes:
|
||||
|
||||
- use the `--debug` to see an electron window with dev tools which allows for debugging
|
||||
- to run only a subset of tests use the `--run` or `--glob` options
|
||||
|
||||
For instance, `./scripts/test.sh --debug --glob **/extHost*.test.js` runs all tests from `extHost`-files and enables you to debug them.
|
||||
|
||||
## Run (inside browser)
|
||||
|
||||
yarn test-browser --browser webkit --browser chromium
|
||||
|
||||
Unit tests from layers `common` and `browser` are run inside `chromium`, `webkit`, and (soon’ish) `firefox` (using playwright). This complements our electron-based unit test runner and adds more coverage of supported platforms. Notes:
|
||||
|
||||
- these tests are part of the continuous build, that means you might have test failures that only happen with webkit on _windows_ or _chromium_ on linux
|
||||
- you can run these tests locally via yarn `test-browser --browser chromium --browser webkit`
|
||||
- to debug, open `<vscode>/test/unit/browser/renderer.html` inside a browser and use the `?m=<amd_module>`-query to specify what AMD module to load, e.g `file:///Users/jrieken/Code/vscode/test/unit/browser/renderer.html?m=vs/base/test/common/strings.test` runs all tests from `strings.test.ts`
|
||||
- to run only a subset of tests use the `--run` or `--glob` options
|
||||
|
||||
## Run (with node)
|
||||
|
||||
yarn run mocha --run src/vs/editor/test/browser/controller/cursor.test.ts
|
||||
|
||||
|
||||
## Coverage
|
||||
|
||||
The following command will create a `coverage` folder at the root of the workspace:
|
||||
|
||||
**OS X and Linux**
|
||||
|
||||
./scripts/test.sh --coverage
|
||||
|
||||
**Windows**
|
||||
|
||||
scripts\test --coverage
|
471
test/unit/assert.js
Normal file
471
test/unit/assert.js
Normal file
@ -0,0 +1,471 @@
|
||||
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
|
||||
//
|
||||
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
|
||||
//
|
||||
// Copyright (c) 2011 Jxck
|
||||
//
|
||||
// Originally from node.js (http://nodejs.org)
|
||||
// Copyright Joyent, Inc.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the 'Software'), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
(function(root, factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define([], factory); // AMD
|
||||
} else if (typeof exports === 'object') {
|
||||
module.exports = factory(); // CommonJS
|
||||
} else {
|
||||
root.assert = factory(); // Global
|
||||
}
|
||||
})(this, function() {
|
||||
|
||||
// UTILITY
|
||||
|
||||
// Object.create compatible in IE
|
||||
var create = Object.create || function(p) {
|
||||
if (!p) throw Error('no type');
|
||||
function f() {};
|
||||
f.prototype = p;
|
||||
return new f();
|
||||
};
|
||||
|
||||
// UTILITY
|
||||
var util = {
|
||||
inherits: function(ctor, superCtor) {
|
||||
ctor.super_ = superCtor;
|
||||
ctor.prototype = create(superCtor.prototype, {
|
||||
constructor: {
|
||||
value: ctor,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
},
|
||||
isArray: function(ar) {
|
||||
return Array.isArray(ar);
|
||||
},
|
||||
isBoolean: function(arg) {
|
||||
return typeof arg === 'boolean';
|
||||
},
|
||||
isNull: function(arg) {
|
||||
return arg === null;
|
||||
},
|
||||
isNullOrUndefined: function(arg) {
|
||||
return arg == null;
|
||||
},
|
||||
isNumber: function(arg) {
|
||||
return typeof arg === 'number';
|
||||
},
|
||||
isString: function(arg) {
|
||||
return typeof arg === 'string';
|
||||
},
|
||||
isSymbol: function(arg) {
|
||||
return typeof arg === 'symbol';
|
||||
},
|
||||
isUndefined: function(arg) {
|
||||
return arg === undefined;
|
||||
},
|
||||
isRegExp: function(re) {
|
||||
return util.isObject(re) && util.objectToString(re) === '[object RegExp]';
|
||||
},
|
||||
isObject: function(arg) {
|
||||
return typeof arg === 'object' && arg !== null;
|
||||
},
|
||||
isDate: function(d) {
|
||||
return util.isObject(d) && util.objectToString(d) === '[object Date]';
|
||||
},
|
||||
isError: function(e) {
|
||||
return isObject(e) &&
|
||||
(objectToString(e) === '[object Error]' || e instanceof Error);
|
||||
},
|
||||
isFunction: function(arg) {
|
||||
return typeof arg === 'function';
|
||||
},
|
||||
isPrimitive: function(arg) {
|
||||
return arg === null ||
|
||||
typeof arg === 'boolean' ||
|
||||
typeof arg === 'number' ||
|
||||
typeof arg === 'string' ||
|
||||
typeof arg === 'symbol' || // ES6 symbol
|
||||
typeof arg === 'undefined';
|
||||
},
|
||||
objectToString: function(o) {
|
||||
return Object.prototype.toString.call(o);
|
||||
}
|
||||
};
|
||||
|
||||
var pSlice = Array.prototype.slice;
|
||||
|
||||
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
|
||||
var Object_keys = typeof Object.keys === 'function' ? Object.keys : (function() {
|
||||
var hasOwnProperty = Object.prototype.hasOwnProperty,
|
||||
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
|
||||
dontEnums = [
|
||||
'toString',
|
||||
'toLocaleString',
|
||||
'valueOf',
|
||||
'hasOwnProperty',
|
||||
'isPrototypeOf',
|
||||
'propertyIsEnumerable',
|
||||
'constructor'
|
||||
],
|
||||
dontEnumsLength = dontEnums.length;
|
||||
|
||||
return function(obj) {
|
||||
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
|
||||
throw new TypeError('Object.keys called on non-object');
|
||||
}
|
||||
|
||||
var result = [], prop, i;
|
||||
|
||||
for (prop in obj) {
|
||||
if (hasOwnProperty.call(obj, prop)) {
|
||||
result.push(prop);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasDontEnumBug) {
|
||||
for (i = 0; i < dontEnumsLength; i++) {
|
||||
if (hasOwnProperty.call(obj, dontEnums[i])) {
|
||||
result.push(dontEnums[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
|
||||
// 1. The assert module provides functions that throw
|
||||
// AssertionError's when particular conditions are not met. The
|
||||
// assert module must conform to the following interface.
|
||||
|
||||
var assert = ok;
|
||||
|
||||
// 2. The AssertionError is defined in assert.
|
||||
// new assert.AssertionError({ message: message,
|
||||
// actual: actual,
|
||||
// expected: expected })
|
||||
|
||||
assert.AssertionError = function AssertionError(options) {
|
||||
this.name = 'AssertionError';
|
||||
this.actual = options.actual;
|
||||
this.expected = options.expected;
|
||||
this.operator = options.operator;
|
||||
if (options.message) {
|
||||
this.message = options.message;
|
||||
this.generatedMessage = false;
|
||||
} else {
|
||||
this.message = getMessage(this);
|
||||
this.generatedMessage = true;
|
||||
}
|
||||
var stackStartFunction = options.stackStartFunction || fail;
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, stackStartFunction);
|
||||
} else {
|
||||
// try to throw an error now, and from the stack property
|
||||
// work out the line that called in to assert.js.
|
||||
try {
|
||||
this.stack = (new Error).stack.toString();
|
||||
} catch (e) {}
|
||||
}
|
||||
};
|
||||
|
||||
// assert.AssertionError instanceof Error
|
||||
util.inherits(assert.AssertionError, Error);
|
||||
|
||||
function replacer(key, value) {
|
||||
if (util.isUndefined(value)) {
|
||||
return '' + value;
|
||||
}
|
||||
if (util.isNumber(value) && (isNaN(value) || !isFinite(value))) {
|
||||
return value.toString();
|
||||
}
|
||||
if (util.isFunction(value) || util.isRegExp(value)) {
|
||||
return value.toString();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function truncate(s, n) {
|
||||
if (util.isString(s)) {
|
||||
return s.length < n ? s : s.slice(0, n);
|
||||
} else {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
function getMessage(self) {
|
||||
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
|
||||
self.operator + ' ' +
|
||||
truncate(JSON.stringify(self.expected, replacer), 128);
|
||||
}
|
||||
|
||||
// At present only the three keys mentioned above are used and
|
||||
// understood by the spec. Implementations or sub modules can pass
|
||||
// other keys to the AssertionError's constructor - they will be
|
||||
// ignored.
|
||||
|
||||
// 3. All of the following functions must throw an AssertionError
|
||||
// when a corresponding condition is not met, with a message that
|
||||
// may be undefined if not provided. All assertion methods provide
|
||||
// both the actual and expected values to the assertion error for
|
||||
// display purposes.
|
||||
|
||||
function fail(actual, expected, message, operator, stackStartFunction) {
|
||||
throw new assert.AssertionError({
|
||||
message: message,
|
||||
actual: actual,
|
||||
expected: expected,
|
||||
operator: operator,
|
||||
stackStartFunction: stackStartFunction
|
||||
});
|
||||
}
|
||||
|
||||
// EXTENSION! allows for well behaved errors defined elsewhere.
|
||||
assert.fail = fail;
|
||||
|
||||
// 4. Pure assertion tests whether a value is truthy, as determined
|
||||
// by !!guard.
|
||||
// assert.ok(guard, message_opt);
|
||||
// This statement is equivalent to assert.equal(true, !!guard,
|
||||
// message_opt);. To test strictly for the value true, use
|
||||
// assert.strictEqual(true, guard, message_opt);.
|
||||
|
||||
function ok(value, message) {
|
||||
if (!value) fail(value, true, message, '==', assert.ok);
|
||||
}
|
||||
assert.ok = ok;
|
||||
|
||||
// 5. The equality assertion tests shallow, coercive equality with
|
||||
// ==.
|
||||
// assert.equal(actual, expected, message_opt);
|
||||
|
||||
assert.equal = function equal(actual, expected, message) {
|
||||
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
|
||||
};
|
||||
|
||||
// 6. The non-equality assertion tests for whether two objects are not equal
|
||||
// with != assert.notEqual(actual, expected, message_opt);
|
||||
|
||||
assert.notEqual = function notEqual(actual, expected, message) {
|
||||
if (actual == expected) {
|
||||
fail(actual, expected, message, '!=', assert.notEqual);
|
||||
}
|
||||
};
|
||||
|
||||
// 7. The equivalence assertion tests a deep equality relation.
|
||||
// assert.deepEqual(actual, expected, message_opt);
|
||||
|
||||
assert.deepEqual = function deepEqual(actual, expected, message) {
|
||||
if (!_deepEqual(actual, expected, false)) {
|
||||
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
|
||||
}
|
||||
};
|
||||
|
||||
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
||||
if (!_deepEqual(actual, expected, true)) {
|
||||
fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual);
|
||||
}
|
||||
};
|
||||
|
||||
function _deepEqual(actual, expected, strict) {
|
||||
// 7.1. All identical values are equivalent, as determined by ===.
|
||||
if (actual === expected) {
|
||||
return true;
|
||||
// } else if (actual instanceof Buffer && expected instanceof Buffer) {
|
||||
// return compare(actual, expected) === 0;
|
||||
|
||||
// 7.2. If the expected value is a Date object, the actual value is
|
||||
// equivalent if it is also a Date object that refers to the same time.
|
||||
} else if (util.isDate(actual) && util.isDate(expected)) {
|
||||
return actual.getTime() === expected.getTime();
|
||||
|
||||
// 7.3 If the expected value is a RegExp object, the actual value is
|
||||
// equivalent if it is also a RegExp object with the same source and
|
||||
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
|
||||
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
|
||||
return actual.source === expected.source &&
|
||||
actual.global === expected.global &&
|
||||
actual.multiline === expected.multiline &&
|
||||
actual.lastIndex === expected.lastIndex &&
|
||||
actual.ignoreCase === expected.ignoreCase;
|
||||
|
||||
// 7.4. Other pairs that do not both pass typeof value == 'object',
|
||||
// equivalence is determined by ==.
|
||||
} else if ((actual === null || typeof actual !== 'object') &&
|
||||
(expected === null || typeof expected !== 'object')) {
|
||||
return strict ? actual === expected : actual == expected;
|
||||
|
||||
// 7.5 For all other Object pairs, including Array objects, equivalence is
|
||||
// determined by having the same number of owned properties (as verified
|
||||
// with Object.prototype.hasOwnProperty.call), the same set of keys
|
||||
// (although not necessarily the same order), equivalent values for every
|
||||
// corresponding key, and an identical 'prototype' property. Note: this
|
||||
// accounts for both named and indexed properties on Arrays.
|
||||
} else {
|
||||
return objEquiv(actual, expected, strict);
|
||||
}
|
||||
}
|
||||
|
||||
function isArguments(object) {
|
||||
return Object.prototype.toString.call(object) == '[object Arguments]';
|
||||
}
|
||||
|
||||
function objEquiv(a, b, strict) {
|
||||
if (a === null || a === undefined || b === null || b === undefined)
|
||||
return false;
|
||||
// if one is a primitive, the other must be same
|
||||
if (util.isPrimitive(a) || util.isPrimitive(b))
|
||||
return a === b;
|
||||
if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
|
||||
return false;
|
||||
var aIsArgs = isArguments(a),
|
||||
bIsArgs = isArguments(b);
|
||||
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
|
||||
return false;
|
||||
if (aIsArgs) {
|
||||
a = pSlice.call(a);
|
||||
b = pSlice.call(b);
|
||||
return _deepEqual(a, b, strict);
|
||||
}
|
||||
var ka = Object.keys(a),
|
||||
kb = Object.keys(b),
|
||||
key, i;
|
||||
// having the same number of owned properties (keys incorporates
|
||||
// hasOwnProperty)
|
||||
if (ka.length !== kb.length)
|
||||
return false;
|
||||
//the same set of keys (although not necessarily the same order),
|
||||
ka.sort();
|
||||
kb.sort();
|
||||
//~~~cheap key test
|
||||
for (i = ka.length - 1; i >= 0; i--) {
|
||||
if (ka[i] !== kb[i])
|
||||
return false;
|
||||
}
|
||||
//equivalent values for every corresponding key, and
|
||||
//~~~possibly expensive deep test
|
||||
for (i = ka.length - 1; i >= 0; i--) {
|
||||
key = ka[i];
|
||||
if (!_deepEqual(a[key], b[key], strict)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 8. The non-equivalence assertion tests for any deep inequality.
|
||||
// assert.notDeepEqual(actual, expected, message_opt);
|
||||
|
||||
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
||||
if (_deepEqual(actual, expected, false)) {
|
||||
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
|
||||
}
|
||||
};
|
||||
|
||||
assert.notDeepStrictEqual = notDeepStrictEqual;
|
||||
function notDeepStrictEqual(actual, expected, message) {
|
||||
if (_deepEqual(actual, expected, true)) {
|
||||
fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 9. The strict equality assertion tests strict equality, as determined by ===.
|
||||
// assert.strictEqual(actual, expected, message_opt);
|
||||
|
||||
assert.strictEqual = function strictEqual(actual, expected, message) {
|
||||
if (actual !== expected) {
|
||||
fail(actual, expected, message, '===', assert.strictEqual);
|
||||
}
|
||||
};
|
||||
|
||||
// 10. The strict non-equality assertion tests for strict inequality, as
|
||||
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
|
||||
|
||||
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
||||
if (actual === expected) {
|
||||
fail(actual, expected, message, '!==', assert.notStrictEqual);
|
||||
}
|
||||
};
|
||||
|
||||
function expectedException(actual, expected) {
|
||||
if (!actual || !expected) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Object.prototype.toString.call(expected) == '[object RegExp]') {
|
||||
return expected.test(actual);
|
||||
} else if (actual instanceof expected) {
|
||||
return true;
|
||||
} else if (expected.call({}, actual) === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function _throws(shouldThrow, block, expected, message) {
|
||||
var actual;
|
||||
|
||||
if (typeof block !== 'function') {
|
||||
throw new TypeError('block must be a function');
|
||||
}
|
||||
|
||||
if (typeof expected === 'string') {
|
||||
message = expected;
|
||||
expected = null;
|
||||
}
|
||||
|
||||
try {
|
||||
block();
|
||||
} catch (e) {
|
||||
actual = e;
|
||||
}
|
||||
|
||||
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
|
||||
(message ? ' ' + message : '.');
|
||||
|
||||
if (shouldThrow && !actual) {
|
||||
fail(actual, expected, 'Missing expected exception' + message);
|
||||
}
|
||||
|
||||
if (!shouldThrow && expectedException(actual, expected)) {
|
||||
fail(actual, expected, 'Got unwanted exception' + message);
|
||||
}
|
||||
|
||||
if ((shouldThrow && actual && expected &&
|
||||
!expectedException(actual, expected)) || (!shouldThrow && actual)) {
|
||||
throw actual;
|
||||
}
|
||||
}
|
||||
|
||||
// 11. Expected to throw an error:
|
||||
// assert.throws(block, Error_opt, message_opt);
|
||||
|
||||
assert.throws = function(block, /*optional*/error, /*optional*/message) {
|
||||
_throws.apply(this, [true].concat(pSlice.call(arguments)));
|
||||
};
|
||||
|
||||
// EXTENSION! This is annoying to write outside this module.
|
||||
assert.doesNotThrow = function(block, /*optional*/message) {
|
||||
_throws.apply(this, [false].concat(pSlice.call(arguments)));
|
||||
};
|
||||
|
||||
assert.ifError = function(err) { if (err) {throw err;}};
|
||||
return assert;
|
||||
});
|
263
test/unit/browser/index.js
Normal file
263
test/unit/browser/index.js
Normal file
@ -0,0 +1,263 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
//@ts-check
|
||||
|
||||
const path = require('path');
|
||||
const glob = require('glob');
|
||||
const fs = require('fs');
|
||||
const events = require('events');
|
||||
const mocha = require('mocha');
|
||||
const MochaJUnitReporter = require('mocha-junit-reporter');
|
||||
const url = require('url');
|
||||
const minimatch = require('minimatch');
|
||||
const playwright = require('playwright');
|
||||
|
||||
// opts
|
||||
const defaultReporterName = process.platform === 'win32' ? 'list' : 'spec';
|
||||
const optimist = require('optimist')
|
||||
// .describe('grep', 'only run tests matching <pattern>').alias('grep', 'g').alias('grep', 'f').string('grep')
|
||||
.describe('build', 'run with build output (out-build)').boolean('build')
|
||||
.describe('run', 'only run tests matching <relative_file_path>').string('run')
|
||||
.describe('glob', 'only run tests matching <glob_pattern>').string('glob')
|
||||
.describe('debug', 'do not run browsers headless').boolean('debug')
|
||||
.describe('browser', 'browsers in which tests should run').string('browser').default('browser', ['chromium', 'firefox', 'webkit'])
|
||||
.describe('reporter', 'the mocha reporter').string('reporter').default('reporter', defaultReporterName)
|
||||
.describe('reporter-options', 'the mocha reporter options').string('reporter-options').default('reporter-options', '')
|
||||
.describe('tfs', 'tfs').string('tfs')
|
||||
.describe('help', 'show the help').alias('help', 'h');
|
||||
|
||||
// logic
|
||||
const argv = optimist.argv;
|
||||
|
||||
if (argv.help) {
|
||||
optimist.showHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const withReporter = (function () {
|
||||
if (argv.tfs) {
|
||||
{
|
||||
return (browserType, runner) => {
|
||||
new mocha.reporters.Spec(runner);
|
||||
new MochaJUnitReporter(runner, {
|
||||
reporterOptions: {
|
||||
testsuitesTitle: `${argv.tfs} ${process.platform}`,
|
||||
mochaFile: process.env.BUILD_ARTIFACTSTAGINGDIRECTORY ? path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${process.arch}-${browserType}-${argv.tfs.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`) : undefined
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const reporterPath = path.join(path.dirname(require.resolve('mocha')), 'lib', 'reporters', argv.reporter);
|
||||
let ctor;
|
||||
|
||||
try {
|
||||
ctor = require(reporterPath);
|
||||
} catch (err) {
|
||||
try {
|
||||
ctor = require(argv.reporter);
|
||||
} catch (err) {
|
||||
ctor = process.platform === 'win32' ? mocha.reporters.List : mocha.reporters.Spec;
|
||||
console.warn(`could not load reporter: ${argv.reporter}, using ${ctor.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
function parseReporterOption(value) {
|
||||
let r = /^([^=]+)=(.*)$/.exec(value);
|
||||
return r ? { [r[1]]: r[2] } : {};
|
||||
}
|
||||
|
||||
let reporterOptions = argv['reporter-options'];
|
||||
reporterOptions = typeof reporterOptions === 'string' ? [reporterOptions] : reporterOptions;
|
||||
reporterOptions = reporterOptions.reduce((r, o) => Object.assign(r, parseReporterOption(o)), {});
|
||||
|
||||
return (_, runner) => new ctor(runner, { reporterOptions })
|
||||
}
|
||||
})()
|
||||
|
||||
const outdir = argv.build ? 'out-build' : 'out';
|
||||
const out = path.join(__dirname, `../../../${outdir}`);
|
||||
|
||||
function ensureIsArray(a) {
|
||||
return Array.isArray(a) ? a : [a];
|
||||
}
|
||||
|
||||
const testModules = (async function () {
|
||||
|
||||
const excludeGlob = '**/{node,electron-sandbox,electron-browser,electron-main}/**/*.test.js';
|
||||
let isDefaultModules = true;
|
||||
let promise;
|
||||
|
||||
if (argv.run) {
|
||||
// use file list (--run)
|
||||
isDefaultModules = false;
|
||||
promise = Promise.resolve(ensureIsArray(argv.run).map(file => {
|
||||
file = file.replace(/^src/, 'out');
|
||||
file = file.replace(/\.ts$/, '.js');
|
||||
return path.relative(out, file);
|
||||
}));
|
||||
|
||||
} else {
|
||||
// glob patterns (--glob)
|
||||
const defaultGlob = '**/*.test.js';
|
||||
const pattern = argv.glob || defaultGlob
|
||||
isDefaultModules = pattern === defaultGlob;
|
||||
|
||||
promise = new Promise((resolve, reject) => {
|
||||
glob(pattern, { cwd: out }, (err, files) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(files)
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return promise.then(files => {
|
||||
const modules = [];
|
||||
for (let file of files) {
|
||||
if (!minimatch(file, excludeGlob)) {
|
||||
modules.push(file.replace(/\.js$/, ''));
|
||||
|
||||
} else if (!isDefaultModules) {
|
||||
console.warn(`DROPPONG ${file} because it cannot be run inside a browser`);
|
||||
}
|
||||
}
|
||||
return modules;
|
||||
})
|
||||
})();
|
||||
|
||||
|
||||
async function runTestsInBrowser(testModules, browserType) {
|
||||
const args = process.platform === 'linux' && browserType === 'chromium' ? ['--no-sandbox'] : undefined; // disable sandbox to run chrome on certain Linux distros
|
||||
const browser = await playwright[browserType].launch({ headless: !Boolean(argv.debug), args });
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
const target = url.pathToFileURL(path.join(__dirname, 'renderer.html'));
|
||||
if (argv.build) {
|
||||
target.search = `?build=true`;
|
||||
}
|
||||
await page.goto(target.href);
|
||||
|
||||
const emitter = new events.EventEmitter();
|
||||
await page.exposeFunction('mocha_report', (type, data1, data2) => {
|
||||
emitter.emit(type, data1, data2)
|
||||
});
|
||||
|
||||
page.on('console', async msg => {
|
||||
console[msg.type()](msg.text(), await Promise.all(msg.args().map(async arg => await arg.jsonValue())));
|
||||
});
|
||||
|
||||
withReporter(browserType, new EchoRunner(emitter, browserType.toUpperCase()));
|
||||
|
||||
// collection failures for console printing
|
||||
const fails = [];
|
||||
emitter.on('fail', (test, err) => {
|
||||
if (err.stack) {
|
||||
const regex = /(vs\/.*\.test)\.js/;
|
||||
for (let line of String(err.stack).split('\n')) {
|
||||
const match = regex.exec(line);
|
||||
if (match) {
|
||||
fails.push(match[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
// @ts-expect-error
|
||||
await page.evaluate(modules => loadAndRun(modules), testModules);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
await browser.close();
|
||||
|
||||
if (fails.length > 0) {
|
||||
return `to DEBUG, open ${browserType.toUpperCase()} and navigate to ${target.href}?${fails.map(module => `m=${module}`).join('&')}`;
|
||||
}
|
||||
}
|
||||
|
||||
class EchoRunner extends events.EventEmitter {
|
||||
|
||||
constructor(event, title = '') {
|
||||
super();
|
||||
event.on('start', () => this.emit('start'));
|
||||
event.on('end', () => this.emit('end'));
|
||||
event.on('suite', (suite) => this.emit('suite', EchoRunner.deserializeSuite(suite, title)));
|
||||
event.on('suite end', (suite) => this.emit('suite end', EchoRunner.deserializeSuite(suite, title)));
|
||||
event.on('test', (test) => this.emit('test', EchoRunner.deserializeRunnable(test)));
|
||||
event.on('test end', (test) => this.emit('test end', EchoRunner.deserializeRunnable(test)));
|
||||
event.on('hook', (hook) => this.emit('hook', EchoRunner.deserializeRunnable(hook)));
|
||||
event.on('hook end', (hook) => this.emit('hook end', EchoRunner.deserializeRunnable(hook)));
|
||||
event.on('pass', (test) => this.emit('pass', EchoRunner.deserializeRunnable(test)));
|
||||
event.on('fail', (test, err) => this.emit('fail', EchoRunner.deserializeRunnable(test, title), EchoRunner.deserializeError(err)));
|
||||
event.on('pending', (test) => this.emit('pending', EchoRunner.deserializeRunnable(test)));
|
||||
}
|
||||
|
||||
static deserializeSuite(suite, titleExtra) {
|
||||
return {
|
||||
root: suite.root,
|
||||
suites: suite.suites,
|
||||
tests: suite.tests,
|
||||
title: titleExtra && suite.title ? `${suite.title} - /${titleExtra}/` : suite.title,
|
||||
fullTitle: () => suite.fullTitle,
|
||||
timeout: () => suite.timeout,
|
||||
retries: () => suite.retries,
|
||||
enableTimeouts: () => suite.enableTimeouts,
|
||||
slow: () => suite.slow,
|
||||
bail: () => suite.bail
|
||||
};
|
||||
}
|
||||
|
||||
static deserializeRunnable(runnable, titleExtra) {
|
||||
return {
|
||||
title: runnable.title,
|
||||
fullTitle: () => titleExtra && runnable.fullTitle ? `${runnable.fullTitle} - /${titleExtra}/` : runnable.fullTitle,
|
||||
async: runnable.async,
|
||||
slow: () => runnable.slow,
|
||||
speed: runnable.speed,
|
||||
duration: runnable.duration
|
||||
};
|
||||
}
|
||||
|
||||
static deserializeError(err) {
|
||||
const inspect = err.inspect;
|
||||
err.inspect = () => inspect;
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
testModules.then(async modules => {
|
||||
|
||||
// run tests in selected browsers
|
||||
const browserTypes = Array.isArray(argv.browser)
|
||||
? argv.browser : [argv.browser];
|
||||
|
||||
const promises = browserTypes.map(async browserType => {
|
||||
try {
|
||||
return await runTestsInBrowser(modules, browserType);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
// aftermath
|
||||
let didFail = false;
|
||||
const messages = await Promise.all(promises);
|
||||
for (let msg of messages) {
|
||||
if (msg) {
|
||||
didFail = true;
|
||||
console.log(msg);
|
||||
}
|
||||
}
|
||||
process.exit(didFail ? 1 : 0);
|
||||
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
});
|
148
test/unit/browser/renderer.html
Normal file
148
test/unit/browser/renderer.html
Normal file
@ -0,0 +1,148 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>VSCode Tests</title>
|
||||
<link href="../../../node_modules/mocha/mocha.css" rel="stylesheet" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script src="../../../node_modules/mocha/mocha.js"></script>
|
||||
<script>
|
||||
// !!! DO NOT CHANGE !!!
|
||||
// Our unit tests may run in environments without
|
||||
// display (e.g. from builds) and tests may by
|
||||
// accident bring up native dialogs or even open
|
||||
// windows. This we cannot allow as it may crash
|
||||
// the test run.
|
||||
// !!! DO NOT CHANGE !!!
|
||||
window.open = function () { throw new Error('window.open() is not supported in tests!'); };
|
||||
window.alert = function () { throw new Error('window.alert() is not supported in tests!'); }
|
||||
window.confirm = function () { throw new Error('window.confirm() is not supported in tests!'); }
|
||||
|
||||
// Ignore uncaught cancelled promise errors
|
||||
window.addEventListener('unhandledrejection', e => {
|
||||
const name = e && e.reason && e.reason.name;
|
||||
|
||||
if (name === 'Canceled') {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
});
|
||||
|
||||
mocha.setup({
|
||||
ui: 'tdd',
|
||||
timeout: 5000
|
||||
});
|
||||
</script>
|
||||
|
||||
<!-- Depending on --build or not, load loader from known locations -->
|
||||
<script src="../../../out/vs/loader.js"></script>
|
||||
<script src="../../../out-build/vs/loader.js"></script>
|
||||
|
||||
<script>
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const isBuild = urlParams.get('build');
|
||||
|
||||
// configure loader
|
||||
const baseUrl = window.location.href;
|
||||
require.config({
|
||||
catchError: true,
|
||||
baseUrl: new URL('../../../src', baseUrl).href,
|
||||
paths: {
|
||||
vs: new URL(`../../../${!!isBuild ? 'out-build' : 'out'}/vs`, baseUrl).href,
|
||||
assert: new URL('../assert.js', baseUrl).href,
|
||||
sinon: new URL('../../../node_modules/sinon/pkg/sinon-1.17.7.js', baseUrl).href,
|
||||
xterm: new URL('../../../node_modules/xterm/lib/xterm.js', baseUrl).href,
|
||||
'iconv-lite-umd': new URL('../../../node_modules/iconv-lite-umd/lib/iconv-lite-umd.js', baseUrl).href,
|
||||
jschardet: new URL('../../../node_modules/jschardet/dist/jschardet.min.js', baseUrl).href
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
function serializeSuite(suite) {
|
||||
return {
|
||||
root: suite.root,
|
||||
suites: suite.suites.map(serializeSuite),
|
||||
tests: suite.tests.map(serializeRunnable),
|
||||
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() : ''
|
||||
};
|
||||
}
|
||||
function PlaywrightReporter(runner) {
|
||||
runner.on('start', () => window.mocha_report('start'));
|
||||
runner.on('end', () => window.mocha_report('end'));
|
||||
runner.on('suite', suite => window.mocha_report('suite', serializeSuite(suite)));
|
||||
runner.on('suite end', suite => window.mocha_report('suite end', serializeSuite(suite)));
|
||||
runner.on('test', test => window.mocha_report('test', serializeRunnable(test)));
|
||||
runner.on('test end', test => window.mocha_report('test end', serializeRunnable(test)));
|
||||
runner.on('hook', hook => window.mocha_report('hook', serializeRunnable(hook)));
|
||||
runner.on('hook end', hook => window.mocha_report('hook end', serializeRunnable(hook)));
|
||||
runner.on('pass', test => window.mocha_report('pass', serializeRunnable(test)));
|
||||
runner.on('fail', (test, err) => window.mocha_report('fail', serializeRunnable(test), serializeError(err)));
|
||||
runner.on('pending', test => window.mocha_report('pending', serializeRunnable(test)));
|
||||
};
|
||||
|
||||
window.loadAndRun = async function loadAndRun(modules, manual = false) {
|
||||
// load
|
||||
await Promise.all(modules.map(module => new Promise((resolve, reject) => {
|
||||
require([module], resolve, err => {
|
||||
console.log("BAD " + module + JSON.stringify(err, undefined, '\t'));
|
||||
// console.log(module);
|
||||
resolve({});
|
||||
});
|
||||
})));
|
||||
// await new Promise((resolve, reject) => {
|
||||
// require(modules, resolve, err => {
|
||||
// console.log(err);
|
||||
// reject(err);
|
||||
// });
|
||||
// });
|
||||
|
||||
// run
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!manual) {
|
||||
mocha.reporter(PlaywrightReporter);
|
||||
}
|
||||
mocha.run(failCount => resolve(failCount === 0));
|
||||
});
|
||||
}
|
||||
|
||||
const modules = new URL(window.location.href).searchParams.getAll('m');
|
||||
if (Array.isArray(modules) && modules.length > 0) {
|
||||
console.log('MANUALLY running tests', modules);
|
||||
|
||||
loadAndRun(modules, true).then(() => console.log('done'), err => console.log(err));
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
87
test/unit/coverage.js
Normal file
87
test/unit/coverage.js
Normal file
@ -0,0 +1,87 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
const minimatch = require('minimatch');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const iLibInstrument = require('istanbul-lib-instrument');
|
||||
const iLibCoverage = require('istanbul-lib-coverage');
|
||||
const iLibSourceMaps = require('istanbul-lib-source-maps');
|
||||
const iLibReport = require('istanbul-lib-report');
|
||||
const iReports = require('istanbul-reports');
|
||||
|
||||
const REPO_PATH = toUpperDriveLetter(path.join(__dirname, '../../'));
|
||||
|
||||
exports.initialize = function (loaderConfig) {
|
||||
const instrumenter = iLibInstrument.createInstrumenter();
|
||||
loaderConfig.nodeInstrumenter = function (contents, source) {
|
||||
if (minimatch(source, '**/test/**')) {
|
||||
// tests don't get instrumented
|
||||
return contents;
|
||||
}
|
||||
// Try to find a .map file
|
||||
let map = undefined;
|
||||
try {
|
||||
map = JSON.parse(fs.readFileSync(`${source}.map`).toString());
|
||||
} catch (err) {
|
||||
// missing source map...
|
||||
}
|
||||
return instrumenter.instrumentSync(contents, source, map);
|
||||
};
|
||||
};
|
||||
|
||||
exports.createReport = function (isSingle) {
|
||||
const mapStore = iLibSourceMaps.createSourceMapStore();
|
||||
const coverageMap = iLibCoverage.createCoverageMap(global.__coverage__);
|
||||
return mapStore.transformCoverage(coverageMap).then((transformed) => {
|
||||
// Paths come out all broken
|
||||
let newData = Object.create(null);
|
||||
Object.keys(transformed.data).forEach((file) => {
|
||||
const entry = transformed.data[file];
|
||||
const fixedPath = fixPath(entry.path);
|
||||
entry.data.path = fixedPath;
|
||||
newData[fixedPath] = entry;
|
||||
});
|
||||
transformed.data = newData;
|
||||
|
||||
const context = iLibReport.createContext({
|
||||
dir: path.join(REPO_PATH, `.build/coverage${isSingle ? '-single' : ''}`),
|
||||
coverageMap: transformed
|
||||
});
|
||||
const tree = context.getTree('flat');
|
||||
|
||||
let reports = [];
|
||||
if (isSingle) {
|
||||
reports.push(iReports.create('lcovonly'));
|
||||
} else {
|
||||
reports.push(iReports.create('json'));
|
||||
reports.push(iReports.create('lcov'));
|
||||
reports.push(iReports.create('html'));
|
||||
}
|
||||
reports.forEach(report => tree.visit(report, context));
|
||||
});
|
||||
};
|
||||
|
||||
function toUpperDriveLetter(str) {
|
||||
if (/^[a-z]:/.test(str)) {
|
||||
return str.charAt(0).toUpperCase() + str.substr(1);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
function toLowerDriveLetter(str) {
|
||||
if (/^[A-Z]:/.test(str)) {
|
||||
return str.charAt(0).toLowerCase() + str.substr(1);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
function fixPath(brokenPath) {
|
||||
const startIndex = brokenPath.lastIndexOf(REPO_PATH);
|
||||
if (startIndex === -1) {
|
||||
return toLowerDriveLetter(brokenPath);
|
||||
}
|
||||
return toLowerDriveLetter(brokenPath.substr(startIndex));
|
||||
}
|
175
test/unit/electron/index.js
Normal file
175
test/unit/electron/index.js
Normal file
@ -0,0 +1,175 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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');
|
||||
const MochaJUnitReporter = require('mocha-junit-reporter');
|
||||
const url = require('url');
|
||||
|
||||
// Disable render process reuse, we still have
|
||||
// non-context aware native modules in the renderer.
|
||||
app.allowRendererProcessReuse = false;
|
||||
|
||||
const defaultReporterName = process.platform === 'win32' ? 'list' : 'spec';
|
||||
|
||||
const optimist = require('optimist')
|
||||
.describe('grep', 'only run tests matching <pattern>').alias('grep', 'g').alias('grep', 'f').string('grep')
|
||||
.describe('run', 'only run tests from <file>').string('run')
|
||||
.describe('runGlob', 'only run tests matching <file_pattern>').alias('runGlob', 'glob').alias('runGlob', 'runGrep').string('runGlob')
|
||||
.describe('build', 'run with build output (out-build)').boolean('build')
|
||||
.describe('coverage', 'generate coverage report').boolean('coverage')
|
||||
.describe('debug', 'open dev tools, keep window open, reuse app data').string('debug')
|
||||
.describe('reporter', 'the mocha reporter').string('reporter').default('reporter', defaultReporterName)
|
||||
.describe('reporter-options', 'the mocha reporter options').string('reporter-options').default('reporter-options', '')
|
||||
.describe('tfs').string('tfs')
|
||||
.describe('help', 'show the help').alias('help', 'h');
|
||||
|
||||
const argv = optimist.argv;
|
||||
|
||||
if (argv.help) {
|
||||
optimist.showHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (!argv.debug) {
|
||||
app.setPath('userData', join(tmpdir(), `vscode-tests-${Date.now()}`));
|
||||
}
|
||||
|
||||
function deserializeSuite(suite) {
|
||||
return {
|
||||
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,
|
||||
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,
|
||||
currentRetry: () => runnable.currentRetry
|
||||
};
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
function parseReporterOption(value) {
|
||||
let r = /^([^=]+)=(.*)$/.exec(value);
|
||||
return r ? { [r[1]]: r[2] } : {};
|
||||
}
|
||||
|
||||
app.on('ready', () => {
|
||||
|
||||
ipcMain.on('error', (_, err) => {
|
||||
if (!argv.debug) {
|
||||
console.error(err);
|
||||
app.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
const win = new BrowserWindow({
|
||||
height: 600,
|
||||
width: 800,
|
||||
show: false,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, '..', '..', '..', 'src', 'vs', 'base', 'parts', 'sandbox', 'electron-browser', 'preload.js'), // ensure similar environment as VSCode as tests may depend on this
|
||||
nodeIntegration: true,
|
||||
enableWebSQL: false,
|
||||
enableRemoteModule: false,
|
||||
spellcheck: false,
|
||||
nativeWindowOpen: true,
|
||||
webviewTag: true
|
||||
}
|
||||
});
|
||||
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
if (argv.debug) {
|
||||
win.show();
|
||||
win.webContents.openDevTools();
|
||||
}
|
||||
win.webContents.send('run', argv);
|
||||
});
|
||||
|
||||
win.loadURL(url.format({ pathname: path.join(__dirname, 'renderer.html'), protocol: 'file:', slashes: true }));
|
||||
|
||||
const runner = new IPCRunner();
|
||||
|
||||
if (argv.tfs) {
|
||||
new mocha.reporters.Spec(runner);
|
||||
new MochaJUnitReporter(runner, {
|
||||
reporterOptions: {
|
||||
testsuitesTitle: `${argv.tfs} ${process.platform}`,
|
||||
mochaFile: process.env.BUILD_ARTIFACTSTAGINGDIRECTORY ? path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${process.arch}-${argv.tfs.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`) : undefined
|
||||
}
|
||||
});
|
||||
} 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}`);
|
||||
}
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
if (!argv.debug) {
|
||||
ipcMain.on('all done', () => app.exit(runner.didFail ? 1 : 0));
|
||||
}
|
||||
});
|
44
test/unit/electron/renderer.html
Normal file
44
test/unit/electron/renderer.html
Normal file
@ -0,0 +1,44 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>VSCode Tests</title>
|
||||
<link href="../../../node_modules/mocha/mocha.css" rel="stylesheet" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script src="../../../node_modules/mocha/mocha.js"></script>
|
||||
|
||||
<script>
|
||||
|
||||
// !!! DO NOT CHANGE !!!
|
||||
// Our unit tests may run in environments without
|
||||
// display (e.g. from builds) and tests may by
|
||||
// accident bring up native dialogs or even open
|
||||
// windows. This we cannot allow as it may crash
|
||||
// the test run.
|
||||
// !!! DO NOT CHANGE !!!
|
||||
window.open = function () { throw new Error('window.open() is not supported in tests!'); };
|
||||
window.alert = function () { throw new Error('window.alert() is not supported in tests!'); }
|
||||
window.confirm = function () { throw new Error('window.confirm() is not supported in tests!'); }
|
||||
|
||||
// Ignore uncaught cancelled promise errors
|
||||
window.addEventListener('unhandledrejection', e => {
|
||||
const name = e && e.reason && e.reason.name;
|
||||
|
||||
if (name === 'Canceled') {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
});
|
||||
|
||||
mocha.setup({
|
||||
ui: 'tdd',
|
||||
timeout: 5000
|
||||
});
|
||||
require('./renderer');
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
224
test/unit/electron/renderer.js
Normal file
224
test/unit/electron/renderer.js
Normal file
@ -0,0 +1,224 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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');
|
||||
const glob = require('glob');
|
||||
const util = require('util');
|
||||
const bootstrap = require('../../../src/bootstrap');
|
||||
const coverage = require('../coverage');
|
||||
|
||||
// Disabled custom inspect. See #38847
|
||||
if (util.inspect && util.inspect['defaultOptions']) {
|
||||
util.inspect['defaultOptions'].customInspect = false;
|
||||
}
|
||||
|
||||
let _tests_glob = '**/test/**/*.test.js';
|
||||
let loader;
|
||||
let _out;
|
||||
|
||||
function initLoader(opts) {
|
||||
let outdir = opts.build ? 'out-build' : 'out';
|
||||
_out = path.join(__dirname, `../../../${outdir}`);
|
||||
|
||||
// setup loader
|
||||
loader = require(`${_out}/vs/loader`);
|
||||
const loaderConfig = {
|
||||
nodeRequire: require,
|
||||
nodeMain: __filename,
|
||||
catchError: true,
|
||||
baseUrl: bootstrap.fileUriFromPath(path.join(__dirname, '../../../src'), { isWindows: process.platform === 'win32' }),
|
||||
paths: {
|
||||
'vs': `../${outdir}/vs`,
|
||||
'lib': `../${outdir}/lib`,
|
||||
'bootstrap-fork': `../${outdir}/bootstrap-fork`
|
||||
}
|
||||
};
|
||||
|
||||
if (opts.coverage) {
|
||||
// initialize coverage if requested
|
||||
coverage.initialize(loaderConfig);
|
||||
}
|
||||
|
||||
loader.require.config(loaderConfig);
|
||||
}
|
||||
|
||||
function createCoverageReport(opts) {
|
||||
if (opts.coverage) {
|
||||
return coverage.createReport(opts.run || opts.runGlob);
|
||||
}
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
|
||||
function loadTestModules(opts) {
|
||||
|
||||
if (opts.run) {
|
||||
const files = Array.isArray(opts.run) ? opts.run : [opts.run];
|
||||
const modules = files.map(file => {
|
||||
file = file.replace(/^src/, 'out');
|
||||
file = file.replace(/\.ts$/, '.js');
|
||||
return path.relative(_out, file).replace(/\.js$/, '');
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
loader.require(modules, resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
const pattern = opts.runGlob || _tests_glob;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
glob(pattern, { cwd: _out }, (err, files) => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function loadTests(opts) {
|
||||
|
||||
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) {
|
||||
let stack = (err ? err.stack : null);
|
||||
if (!stack) {
|
||||
stack = new Error().stack;
|
||||
}
|
||||
|
||||
_unexpectedErrors.push((err && err.message ? err.message : err) + '\n' + stack);
|
||||
});
|
||||
});
|
||||
|
||||
return loadTestModules(opts).then(() => {
|
||||
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);
|
||||
});
|
||||
assert.ok(false, errors);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function serializeSuite(suite) {
|
||||
return {
|
||||
root: suite.root,
|
||||
suites: suite.suites.map(serializeSuite),
|
||||
tests: suite.tests.map(serializeRunnable),
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
function runTests(opts) {
|
||||
|
||||
return loadTests(opts).then(() => {
|
||||
|
||||
if (opts.grep) {
|
||||
mocha.grep(opts.grep);
|
||||
}
|
||||
|
||||
if (!opts.debug) {
|
||||
mocha.reporter(IPCReporter);
|
||||
}
|
||||
|
||||
const runner = mocha.run(() => {
|
||||
createCoverageReport(opts).then(() => {
|
||||
ipcRenderer.send('all done');
|
||||
});
|
||||
});
|
||||
|
||||
if (opts.debug) {
|
||||
runner.on('fail', (test, err) => {
|
||||
|
||||
console.error(test.fullTitle());
|
||||
console.error(err.stack);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ipcRenderer.on('run', (e, opts) => {
|
||||
initLoader(opts);
|
||||
runTests(opts).catch(err => {
|
||||
if (typeof err !== 'string') {
|
||||
err = JSON.stringify(err);
|
||||
}
|
||||
|
||||
console.error(err);
|
||||
ipcRenderer.send('error', err);
|
||||
});
|
||||
});
|
172
test/unit/node/all.js
Normal file
172
test/unit/node/all.js
Normal file
@ -0,0 +1,172 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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*/
|
||||
/*global define,run*/
|
||||
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
const glob = require('glob');
|
||||
const jsdom = require('jsdom-no-contextify');
|
||||
const TEST_GLOB = '**/test/**/*.test.js';
|
||||
const coverage = require('../coverage');
|
||||
|
||||
const 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')
|
||||
.describe('browser', 'Run tests in a browser').boolean('browser')
|
||||
.alias('h', 'help').boolean('h')
|
||||
.describe('h', 'Show help');
|
||||
|
||||
const argv = optimist.argv;
|
||||
|
||||
if (argv.help) {
|
||||
optimist.showHelp();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const REPO_ROOT = path.join(__dirname, '../../../');
|
||||
const out = argv.build ? 'out-build' : 'out';
|
||||
const loader = require(`../../../${out}/vs/loader`);
|
||||
const src = path.join(REPO_ROOT, out);
|
||||
|
||||
function main() {
|
||||
process.on('uncaughtException', function (e) {
|
||||
console.error(e.stack || e);
|
||||
});
|
||||
|
||||
const loaderConfig = {
|
||||
nodeRequire: require,
|
||||
nodeMain: __filename,
|
||||
baseUrl: path.join(REPO_ROOT, 'src'),
|
||||
paths: {
|
||||
'vs/css': '../test/unit/node/css.mock',
|
||||
'vs': `../${out}/vs`,
|
||||
'lib': `../${out}/lib`,
|
||||
'bootstrap-fork': `../${out}/bootstrap-fork`
|
||||
},
|
||||
catchError: true
|
||||
};
|
||||
|
||||
if (argv.coverage) {
|
||||
coverage.initialize(loaderConfig);
|
||||
|
||||
process.on('exit', function (code) {
|
||||
if (code !== 0) {
|
||||
return;
|
||||
}
|
||||
coverage.createReport(argv.run || argv.runGlob);
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
let didErr = false;
|
||||
const write = process.stderr.write;
|
||||
process.stderr.write = function (data) {
|
||||
didErr = didErr || !!data;
|
||||
write.apply(process.stderr, arguments);
|
||||
};
|
||||
|
||||
let loadFunc = null;
|
||||
|
||||
if (argv.runGlob) {
|
||||
loadFunc = (cb) => {
|
||||
const doRun = tests => {
|
||||
const modulesToLoad = tests.map(test => {
|
||||
if (path.isAbsolute(test)) {
|
||||
test = path.relative(src, path.resolve(test));
|
||||
}
|
||||
|
||||
return test.replace(/(\.js)|(\.d\.ts)|(\.js\.map)$/, '');
|
||||
});
|
||||
define(modulesToLoad, () => cb(null), cb);
|
||||
};
|
||||
|
||||
glob(argv.runGlob, { cwd: src }, function (err, files) { doRun(files); });
|
||||
};
|
||||
} else if (argv.run) {
|
||||
const tests = (typeof argv.run === 'string') ? [argv.run] : argv.run;
|
||||
const modulesToLoad = tests.map(function (test) {
|
||||
test = test.replace(/^src/, 'out');
|
||||
test = test.replace(/\.ts$/, '.js');
|
||||
return path.relative(src, path.resolve(test)).replace(/(\.js)|(\.js\.map)$/, '').replace(/\\/g, '/');
|
||||
});
|
||||
loadFunc = (cb) => {
|
||||
define(modulesToLoad, () => cb(null), cb);
|
||||
};
|
||||
} else {
|
||||
loadFunc = (cb) => {
|
||||
glob(TEST_GLOB, { cwd: src }, function (err, files) {
|
||||
const modulesToLoad = files.map(function (file) {
|
||||
return file.replace(/\.js$/, '');
|
||||
});
|
||||
define(modulesToLoad, function () { cb(null); }, cb);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
loadFunc(function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return process.exit(1);
|
||||
}
|
||||
|
||||
process.stderr.write = write;
|
||||
|
||||
if (!argv.run && !argv.runGlob) {
|
||||
// set up last test
|
||||
suite('Loader', function () {
|
||||
test('should not explode while loading', function () {
|
||||
assert.ok(!didErr, 'should not explode while loading');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// report failing test for every unexpected error during any of the tests
|
||||
let 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
|
||||
loader(['vs/base/common/errors'], function (errors) {
|
||||
errors.setUnexpectedErrorHandler(function (err) {
|
||||
const stack = (err && err.stack) || (new Error().stack);
|
||||
unexpectedErrors.push((err && err.message ? err.message : err) + '\n' + stack);
|
||||
});
|
||||
|
||||
// fire up mocha
|
||||
run();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (process.argv.some(function (a) { return /^--browser/.test(a); })) {
|
||||
require('./browser');
|
||||
} else {
|
||||
main();
|
||||
}
|
49
test/unit/node/browser.js
Normal file
49
test/unit/node/browser.js
Normal file
@ -0,0 +1,49 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
const yaserver = require('yaserver');
|
||||
const http = require('http');
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const REPO_ROOT = path.join(__dirname, '../../../');
|
||||
const PORT = 8887;
|
||||
|
||||
function template(str, env) {
|
||||
return str.replace(/{{\s*([\w_\-]+)\s*}}/g, function (all, part) {
|
||||
return env[part];
|
||||
});
|
||||
}
|
||||
|
||||
yaserver.createServer({ rootDir: REPO_ROOT }).then((staticServer) => {
|
||||
const server = http.createServer((req, res) => {
|
||||
if (req.url === '' || req.url === '/') {
|
||||
glob('**/vs/{base,platform,editor}/**/test/{common,browser}/**/*.test.js', {
|
||||
cwd: path.join(REPO_ROOT, 'out'),
|
||||
// ignore: ['**/test/{node,electron*}/**/*.js']
|
||||
}, function (err, files) {
|
||||
if (err) { console.log(err); process.exit(0); }
|
||||
|
||||
var modules = files
|
||||
.map(function (file) { return file.replace(/\.js$/, ''); });
|
||||
|
||||
fs.readFile(path.join(__dirname, 'index.html'), 'utf8', function (err, templateString) {
|
||||
if (err) { console.log(err); process.exit(0); }
|
||||
|
||||
res.end(template(templateString, {
|
||||
modules: JSON.stringify(modules)
|
||||
}));
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return staticServer.handle(req, res);
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`http://localhost:${PORT}/`);
|
||||
});
|
||||
});
|
12
test/unit/node/css.mock.js
Normal file
12
test/unit/node/css.mock.js
Normal file
@ -0,0 +1,12 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
define([], function() {
|
||||
return {
|
||||
load: function(name, req, load) {
|
||||
load({});
|
||||
}
|
||||
};
|
||||
});
|
29
test/unit/node/index.html
Normal file
29
test/unit/node/index.html
Normal file
@ -0,0 +1,29 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>VSCode Tests</title>
|
||||
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
|
||||
<script src="/out/vs/loader.js"></script>
|
||||
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
|
||||
|
||||
<script>
|
||||
mocha.setup('tdd');
|
||||
|
||||
require.config({
|
||||
baseUrl: '/out',
|
||||
paths: {
|
||||
assert: '/test/unit/assert.js',
|
||||
sinon: '/node_modules/sinon/pkg/sinon-1.17.7.js'
|
||||
}
|
||||
});
|
||||
|
||||
require({{ modules }}, function () {
|
||||
mocha.run();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user