X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=editors%2Fcode%2Ftests%2Funit%2Findex.ts;h=288bd60326c60e5c72790fca46dc20ffd6ecfc4f;hb=18d2fb81a78eb7ec75a5850f5c0c3d42a9bd01ec;hp=5165720b458b89ee1ba8dc5fc17da71873f39e47;hpb=f4f5fca10175b8d5fdfa36563c103f81b2b0acd3;p=rust.git diff --git a/editors/code/tests/unit/index.ts b/editors/code/tests/unit/index.ts index 5165720b458..288bd60326c 100644 --- a/editors/code/tests/unit/index.ts +++ b/editors/code/tests/unit/index.ts @@ -1,38 +1,82 @@ +import { readdir } from 'fs/promises'; import * as path from 'path'; -import Mocha from 'mocha'; -import glob from 'glob'; - -export function run(): Promise { - // Create the mocha test - const mocha = new Mocha({ - ui: 'tdd', - color: true - }); - - const testsRoot = __dirname; - - return new Promise((resolve, reject) => { - glob('**/**.test.js', { cwd: testsRoot }, (err, files) => { - if (err) { - return reject(err); - } - // Add files to the test suite - files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); +class Test { + readonly name: string; + readonly promise: Promise; + + constructor(name: string, promise: Promise) { + this.name = name; + this.promise = promise; + } +} + +class Suite { + tests: Test[]; + constructor() { + this.tests = []; + } + + public addTest(name: string, f: () => Promise): void { + const test = new Test(name, f()); + this.tests.push(test); + } + + public async run(): Promise { + let failed = 0; + for (const test of this.tests) { try { - // Run the mocha test - mocha.timeout(100000); - mocha.run(failures => { - if (failures > 0) { - reject(new Error(`${failures} tests failed.`)); - } else { - resolve(); - } - }); - } catch (err) { - reject(err); + await test.promise; + ok(` ✔ ${test.name}`); + } catch (e) { + error(` ✖︎ ${test.name}\n ${e.stack}`); + failed += 1; } - }); - }); + } + if (failed) { + const plural = failed > 1 ? "s" : ""; + throw new Error(`${failed} failed test${plural}`); + } + } +} + +export class Context { + public async suite(name: string, f: (ctx: Suite) => void): Promise { + const ctx = new Suite(); + f(ctx); + try { + ok(`⌛︎ ${name}`); + await ctx.run(); + ok(`✔ ${name}`); + } catch (e) { + error(`✖︎ ${name}\n ${e.stack}`); + throw e; + } + } +} + +export async function run(): Promise { + const context = new Context(); + + const testFiles = (await readdir(path.resolve(__dirname))).filter(name => name.endsWith('.test.js')); + for (const testFile of testFiles) { + try { + const testModule = require(path.resolve(__dirname, testFile)); + await testModule.getTests(context); + } catch (e) { + error(`${e}`); + throw e; + } + } +} + +function ok(message: string): void { + // eslint-disable-next-line no-console + console.log(`\x1b[32m${message}\x1b[0m`); +} + +function error(message: string): void { + // eslint-disable-next-line no-console + console.error(`\x1b[31m${message}\x1b[0m`); }