2020-08-22 02:14:11 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export type Configuration = { name: string, value: string }[];
|
|
|
|
|
|
|
|
|
|
export class Test {
|
|
|
|
|
suite: Suite;
|
|
|
|
|
title: string;
|
|
|
|
|
file: string;
|
|
|
|
|
only = false;
|
|
|
|
|
pending = false;
|
2020-08-23 01:44:56 +02:00
|
|
|
slow = false;
|
2020-08-22 02:14:11 +02:00
|
|
|
duration = 0;
|
|
|
|
|
timeout = 0;
|
|
|
|
|
fn: Function;
|
2020-08-22 17:46:45 +02:00
|
|
|
error: any;
|
2020-08-25 20:00:05 +02:00
|
|
|
stdout: (string | Buffer)[] = [];
|
|
|
|
|
stderr: (string | Buffer)[] = [];
|
|
|
|
|
data: any = {};
|
2020-08-22 02:14:11 +02:00
|
|
|
|
|
|
|
|
_ordinal: number;
|
|
|
|
|
_overriddenFn: Function;
|
2020-08-22 09:05:24 +02:00
|
|
|
_startTime: number;
|
2020-08-22 02:14:11 +02:00
|
|
|
|
|
|
|
|
constructor(title: string, fn: Function) {
|
|
|
|
|
this.title = title;
|
|
|
|
|
this.fn = fn;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:22:14 +02:00
|
|
|
titlePath(): string[] {
|
|
|
|
|
return [...this.suite.titlePath(), this.title];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fullTitle(): string {
|
|
|
|
|
return this.titlePath().join(' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_clone(): Test {
|
2020-08-22 02:14:11 +02:00
|
|
|
const test = new Test(this.title, this.fn);
|
|
|
|
|
test.suite = this.suite;
|
|
|
|
|
test.only = this.only;
|
|
|
|
|
test.file = this.file;
|
|
|
|
|
test.pending = this.pending;
|
|
|
|
|
test.timeout = this.timeout;
|
|
|
|
|
test._overriddenFn = this._overriddenFn;
|
|
|
|
|
return test;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Suite {
|
|
|
|
|
title: string;
|
|
|
|
|
parent?: Suite;
|
|
|
|
|
suites: Suite[] = [];
|
|
|
|
|
tests: Test[] = [];
|
|
|
|
|
only = false;
|
|
|
|
|
pending = false;
|
|
|
|
|
file: string;
|
2020-08-23 01:44:56 +02:00
|
|
|
configuration: Configuration;
|
2020-08-23 20:44:41 +02:00
|
|
|
_configurationString: string;
|
2020-08-22 02:14:11 +02:00
|
|
|
|
2020-08-22 09:05:24 +02:00
|
|
|
_hooks: { type: string, fn: Function } [] = [];
|
|
|
|
|
_entries: (Suite | Test)[] = [];
|
2020-08-22 02:14:11 +02:00
|
|
|
|
|
|
|
|
constructor(title: string, parent?: Suite) {
|
|
|
|
|
this.title = title;
|
|
|
|
|
this.parent = parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
titlePath(): string[] {
|
|
|
|
|
if (!this.parent)
|
2020-08-22 17:46:45 +02:00
|
|
|
return [];
|
2020-08-22 02:14:11 +02:00
|
|
|
return [...this.parent.titlePath(), this.title];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
total(): number {
|
|
|
|
|
let count = 0;
|
2020-08-22 09:05:24 +02:00
|
|
|
this.eachTest(fn => {
|
|
|
|
|
++count;
|
|
|
|
|
});
|
2020-08-22 02:14:11 +02:00
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:22:14 +02:00
|
|
|
_isPending(): boolean {
|
|
|
|
|
return this.pending || (this.parent && this.parent._isPending());
|
2020-08-22 02:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:22:14 +02:00
|
|
|
_addTest(test: Test) {
|
2020-08-22 02:14:11 +02:00
|
|
|
test.suite = this;
|
|
|
|
|
this.tests.push(test);
|
2020-08-22 09:05:24 +02:00
|
|
|
this._entries.push(test);
|
2020-08-22 02:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:22:14 +02:00
|
|
|
_addSuite(suite: Suite) {
|
2020-08-22 02:14:11 +02:00
|
|
|
suite.parent = this;
|
|
|
|
|
this.suites.push(suite);
|
2020-08-22 09:05:24 +02:00
|
|
|
this._entries.push(suite);
|
2020-08-22 02:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:22:14 +02:00
|
|
|
eachSuite(fn: (suite: Suite) => boolean | void): boolean {
|
|
|
|
|
for (const suite of this.suites) {
|
|
|
|
|
if (suite.eachSuite(fn))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 09:05:24 +02:00
|
|
|
eachTest(fn: (test: Test) => boolean | void): boolean {
|
|
|
|
|
for (const suite of this.suites) {
|
|
|
|
|
if (suite.eachTest(fn))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
for (const test of this.tests) {
|
|
|
|
|
if (fn(test))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2020-08-22 02:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-23 17:22:14 +02:00
|
|
|
_clone(): Suite {
|
2020-08-22 02:14:11 +02:00
|
|
|
const suite = new Suite(this.title);
|
|
|
|
|
suite.only = this.only;
|
|
|
|
|
suite.file = this.file;
|
|
|
|
|
suite.pending = this.pending;
|
|
|
|
|
return suite;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 09:05:24 +02:00
|
|
|
_renumber() {
|
|
|
|
|
let ordinal = 0;
|
|
|
|
|
this.eachTest((test: Test) => {
|
|
|
|
|
// All tests are identified with their ordinals.
|
|
|
|
|
test._ordinal = ordinal++;
|
|
|
|
|
});
|
2020-08-22 02:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-22 09:05:24 +02:00
|
|
|
_addHook(type: string, fn: any) {
|
|
|
|
|
this._hooks.push({ type, fn });
|
2020-08-22 02:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-22 09:05:24 +02:00
|
|
|
_hasTestsToRun(): boolean {
|
|
|
|
|
let found = false;
|
|
|
|
|
this.eachTest(test => {
|
|
|
|
|
if (!test.pending) {
|
|
|
|
|
found = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-22 02:14:11 +02:00
|
|
|
});
|
2020-08-22 09:05:24 +02:00
|
|
|
return found;
|
2020-08-22 02:14:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-08-24 19:24:40 +02:00
|
|
|
|
|
|
|
|
export function serializeConfiguration(configuration: Configuration): string {
|
|
|
|
|
const tokens = [];
|
|
|
|
|
for (const { name, value } of configuration)
|
|
|
|
|
tokens.push(`${name}=${value}`);
|
|
|
|
|
return tokens.join(', ');
|
|
|
|
|
}
|
2020-08-24 23:55:48 +02:00
|
|
|
|
2020-08-25 03:58:43 +02:00
|
|
|
export function serializeError(error: Error | any): any {
|
2020-08-24 23:55:48 +02:00
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return {
|
|
|
|
|
message: error.message,
|
|
|
|
|
stack: error.stack
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return trimCycles(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function trimCycles(obj: any): any {
|
|
|
|
|
const cache = new Set();
|
|
|
|
|
return JSON.parse(
|
|
|
|
|
JSON.stringify(obj, function(key, value) {
|
|
|
|
|
if (typeof value === 'object' && value !== null) {
|
|
|
|
|
if (cache.has(value))
|
|
|
|
|
return '' + value;
|
|
|
|
|
cache.add(value);
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|