From df985dabd10d6e3bd3d00ee22c895a7849014c0b Mon Sep 17 00:00:00 2001 From: Kai Ninomiya Date: Mon, 8 Jul 2019 08:42:06 -0700 Subject: [PATCH] split TestSpecFile and ReadmeFile --- src/framework/loader.ts | 21 +++++++++++++-------- src/runtime/cmdline.ts | 6 +++--- src/runtime/standalone.ts | 2 +- src/runtime/wpt.ts | 2 +- src/suites/unittests/loading.spec.ts | 18 +++++++++--------- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/framework/loader.ts b/src/framework/loader.ts index df5d9a4b45f8..4e2449eb1336 100644 --- a/src/framework/loader.ts +++ b/src/framework/loader.ts @@ -6,19 +6,24 @@ import { TestSuiteListing } from './listing.js'; import { TestSpecID, TestCaseID } from './id.js'; // One of the following: -// - A shell object describing a directory (from its README.txt). // - An actual .spec.ts file, as imported. // - A *filtered* list of cases from a single .spec.ts file. export interface TestSpecFile { readonly description: string; - // undefined for README.txt, defined for a test module. - readonly g?: RunCaseIterable; + readonly g: RunCaseIterable; } +// A shell object describing a directory (from its README.txt). +export interface ReadmeFile { + readonly description: string; +} + +export type TestSpecOrReadme = TestSpecFile | ReadmeFile; + // A pending loaded spec (.spec.ts) file, plus identifying information. export interface TestQueryResult { readonly id: TestSpecID; - readonly spec: Promise; + readonly spec: Promise; } type TestQueryResults = IterableIterator; @@ -44,7 +49,7 @@ function filterTestGroup(group: RunCaseIterable, filter: TestGroupFilter): RunCa export interface TestFileLoader { listing(suite: string): Promise; - import(path: string): Promise; + import(path: string): Promise; } class DefaultTestFileLoader implements TestFileLoader { @@ -156,9 +161,9 @@ export class TestLoader { for (const { path, description } of specs) { if (path.startsWith(groupPrefix)) { const isReadme = path === '' || path.endsWith('/'); - const spec: Promise = isReadme - ? Promise.resolve({ description }) - : this.fileLoader.import(`${suite}/${path}.spec.js`); + const spec = isReadme + ? Promise.resolve({ description } as ReadmeFile) + : (this.fileLoader.import(`${suite}/${path}.spec.js`) as Promise); entries.push({ id: { suite, path }, spec }); } } diff --git a/src/runtime/cmdline.ts b/src/runtime/cmdline.ts index 9caee3e27b65..6d54bf56acf2 100644 --- a/src/runtime/cmdline.ts +++ b/src/runtime/cmdline.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import * as process from 'process'; -import { TestSpecFile, TestLoader } from '../framework/loader.js'; +import { TestLoader, TestSpecOrReadme } from '../framework/loader.js'; import { Logger, LiveTestCaseResult } from '../framework/logger.js'; import { TestSpecID } from '../framework/id.js'; import { makeQueryString } from '../framework/url_query.js'; @@ -45,7 +45,7 @@ for (const a of process.argv.slice(2)) { const log = new Logger(); const queryResults = await Promise.all( - Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecFile) => ({ id, spec: s }))) + Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecOrReadme) => ({ id, spec: s }))) ); const failed: Array<[TestSpecID, LiveTestCaseResult]> = []; @@ -54,7 +54,7 @@ for (const a of process.argv.slice(2)) { // TODO: don't run all tests all at once const running = []; for (const qr of queryResults) { - if (!qr.spec.g) { + if (!('g' in qr.spec)) { continue; } diff --git a/src/runtime/standalone.ts b/src/runtime/standalone.ts index cd757fd21d74..b447058d7e07 100644 --- a/src/runtime/standalone.ts +++ b/src/runtime/standalone.ts @@ -116,7 +116,7 @@ function mkCase(testcasesVis: HTMLElement, query: string, t: RunCase) { for (const qr of queryResults) { const testcasesVis = makeTest(qr.id, qr.spec.description); - if (!qr.spec.g) { + if (!('g' in qr.spec)) { continue; } diff --git a/src/runtime/wpt.ts b/src/runtime/wpt.ts index 5c9fb9652d84..5a4043978a5a 100644 --- a/src/runtime/wpt.ts +++ b/src/runtime/wpt.ts @@ -21,7 +21,7 @@ declare function async_test(f: (this: WptTestObject) => Promise, name: str ); for (const qr of queryResults) { - if (!qr.spec.g) { + if (!('g' in qr.spec)) { continue; } diff --git a/src/suites/unittests/loading.spec.ts b/src/suites/unittests/loading.spec.ts index 35169a173f72..592610f7aaeb 100644 --- a/src/suites/unittests/loading.spec.ts +++ b/src/suites/unittests/loading.spec.ts @@ -9,7 +9,7 @@ import { TestGroup, RunCase, } from '../../framework/index.js'; -import { TestSpecFile, TestLoader, TestFileLoader } from '../../framework/loader.js'; +import { TestLoader, TestFileLoader, TestSpecOrReadme } from '../../framework/loader.js'; import { TestSuiteListingEntry, TestSuiteListing } from '../../framework/listing.js'; import { Logger } from '../../framework/logger.js'; @@ -24,7 +24,7 @@ const listingData: { [k: string]: TestSuiteListingEntry[] } = { suite2: [{ path: '', description: 'desc 2a' }, { path: 'foof', description: 'desc 2b' }], }; -const specsData: { [k: string]: TestSpecFile } = { +const specsData: { [k: string]: TestSpecOrReadme } = { 'suite1/README.txt': { description: 'desc 1a' }, 'suite1/foo.spec.js': { description: 'desc 1b', @@ -77,7 +77,7 @@ class FakeTestFileLoader implements TestFileLoader { return listingData[suite]; } - async import(path: string): Promise { + async import(path: string): Promise { if (!specsData.hasOwnProperty(path)) { throw new Error('[test] mock file ' + path + ' does not exist'); } @@ -91,7 +91,7 @@ class LoadingTest extends DefaultFixture { async load(filters: string[]) { const listing = await this.loader.loadTests(filters); const entries = Promise.all( - Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecFile) => ({ id, spec: s }))) + Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecOrReadme) => ({ id, spec: s }))) ); return entries; } @@ -102,11 +102,11 @@ class LoadingTest extends DefaultFixture { if (a.length !== 1) { throw new Error('more than one group'); } - const g = a[0].spec.g; - if (g === undefined) { + const spec = a[0].spec; + if (!('g' in spec)) { throw new Error('group undefined'); } - return Array.from(g.iterate(rec)); + return Array.from(spec.g.iterate(rec)); } } @@ -139,7 +139,7 @@ g.test('whole group', async t => { const foo = (await t.load(['suite1:foo:']))[0]; t.expect(foo.id.suite === 'suite1'); t.expect(foo.id.path === 'foo'); - if (foo.spec.g === undefined) { + if (!('g' in foo.spec)) { throw new Error('foo group'); } const [rec] = new Logger().record({ suite: '', path: '' }); @@ -182,7 +182,7 @@ g.test('end2end', async t => { t.expect(l[0].id.suite === 'suite2'); t.expect(l[0].id.path === 'foof'); t.expect(l[0].spec.description === 'desc 2b'); - if (l[0].spec.g === undefined) { + if (!('g' in l[0].spec)) { throw new Error(); } t.expect(l[0].spec.g.iterate instanceof Function);