Skip to content

Commit f09c350

Browse files
committed
Batch enumerateFiles for multiple configurations into a single web request
1 parent 5d8f1d9 commit f09c350

10 files changed

+234
-63
lines changed

src/harness/compilerRunner.ts

+51-27
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const enum CompilerTestType {
1212
Test262
1313
}
1414

15+
interface CompilerFileBasedTest extends Harness.FileBasedTest {
16+
payload?: Harness.TestCaseParser.TestCaseContent;
17+
}
18+
1519
class CompilerBaselineRunner extends RunnerBase {
1620
private basePath = "tests/cases";
1721
private testSuiteName: TestRunnerKind;
@@ -42,7 +46,8 @@ class CompilerBaselineRunner extends RunnerBase {
4246
}
4347

4448
public enumerateTestFiles() {
45-
return this.enumerateFiles(this.basePath, /\.tsx?$/, { recursive: true });
49+
// see also: `enumerateTestFiles` in tests/webTestServer.ts
50+
return this.enumerateFiles(this.basePath, /\.tsx?$/, { recursive: true }).map(CompilerTest.getConfigurations);
4651
}
4752

4853
public initializeTests() {
@@ -52,24 +57,32 @@ class CompilerBaselineRunner extends RunnerBase {
5257
});
5358

5459
// this will set up a series of describe/it blocks to run between the setup and cleanup phases
55-
const files = this.tests.length > 0 ? this.tests : this.enumerateTestFiles();
56-
files.forEach(file => { this.checkTestCodeOutput(vpath.normalizeSeparators(file)); });
60+
const files = this.tests.length > 0 ? this.tests : Harness.IO.enumerateTestFiles(this);
61+
files.forEach(test => {
62+
const file = typeof test === "string" ? test : test.file;
63+
this.checkTestCodeOutput(vpath.normalizeSeparators(file), typeof test === "string" ? CompilerTest.getConfigurations(test) : test);
64+
});
5765
});
5866
}
5967

60-
public checkTestCodeOutput(fileName: string) {
61-
for (const { name, payload } of CompilerTest.getConfigurations(fileName)) {
62-
describe(`${this.testSuiteName} tests for ${fileName}${name ? ` (${name})` : ``}`, () => {
63-
this.runSuite(fileName, payload);
68+
public checkTestCodeOutput(fileName: string, test?: CompilerFileBasedTest) {
69+
if (test && test.configurations) {
70+
test.configurations.forEach(configuration => {
71+
describe(`${this.testSuiteName} tests for ${fileName}${configuration && configuration.name ? ` (${configuration.name})` : ``}`, () => {
72+
this.runSuite(fileName, test, configuration);
73+
});
6474
});
6575
}
76+
describe(`${this.testSuiteName} tests for ${fileName}}`, () => {
77+
this.runSuite(fileName, test);
78+
});
6679
}
6780

68-
private runSuite(fileName: string, testCaseContent: Harness.TestCaseParser.TestCaseContent) {
81+
private runSuite(fileName: string, test?: CompilerFileBasedTest, configuration?: Harness.FileBasedTestConfiguration) {
6982
// Mocha holds onto the closure environment of the describe callback even after the test is done.
7083
// Everything declared here should be cleared out in the "after" callback.
7184
let compilerTest: CompilerTest | undefined;
72-
before(() => { compilerTest = new CompilerTest(fileName, testCaseContent); });
85+
before(() => { compilerTest = new CompilerTest(fileName, test && test.payload, configuration && configuration.settings); });
7386
it(`Correct errors for ${fileName}`, () => { compilerTest.verifyDiagnostics(); });
7487
it(`Correct module resolution tracing for ${fileName}`, () => { compilerTest.verifyModuleResolution(); });
7588
it(`Correct sourcemap content for ${fileName}`, () => { compilerTest.verifySourceMapRecord(); });
@@ -97,11 +110,6 @@ class CompilerBaselineRunner extends RunnerBase {
97110
}
98111
}
99112

100-
interface CompilerTestConfiguration {
101-
name: string;
102-
payload: Harness.TestCaseParser.TestCaseContent;
103-
}
104-
105113
class CompilerTest {
106114
private fileName: string;
107115
private justName: string;
@@ -116,10 +124,20 @@ class CompilerTest {
116124
// equivalent to other files on the file system not directly passed to the compiler (ie things that are referenced by other files)
117125
private otherFiles: Harness.Compiler.TestFile[];
118126

119-
constructor(fileName: string, testCaseContent: Harness.TestCaseParser.TestCaseContent) {
127+
constructor(fileName: string, testCaseContent?: Harness.TestCaseParser.TestCaseContent, configurationOverrides?: Harness.TestCaseParser.CompilerSettings) {
120128
this.fileName = fileName;
121129
this.justName = vpath.basename(fileName);
130+
122131
const rootDir = fileName.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(fileName) + "/";
132+
133+
if (testCaseContent === undefined) {
134+
testCaseContent = Harness.TestCaseParser.makeUnitsFromTest(Harness.IO.readFile(fileName), fileName, rootDir);
135+
}
136+
137+
if (configurationOverrides) {
138+
testCaseContent = { ...testCaseContent, settings: { ...testCaseContent.settings, ...configurationOverrides } };
139+
}
140+
123141
const units = testCaseContent.testUnitData;
124142
this.harnessSettings = testCaseContent.settings;
125143
let tsConfigOptions: ts.CompilerOptions;
@@ -174,32 +192,38 @@ class CompilerTest {
174192
this.options = this.result.options;
175193
}
176194

177-
public static getConfigurations(fileName: string) {
178-
const content = Harness.IO.readFile(fileName);
179-
const rootDir = fileName.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(fileName) + "/";
180-
const testCaseContent = Harness.TestCaseParser.makeUnitsFromTest(content, fileName, rootDir);
181-
const configurations: CompilerTestConfiguration[] = [];
182-
const scriptTargets = this._split(testCaseContent.settings.target);
183-
const moduleKinds = this._split(testCaseContent.settings.module);
195+
public static getConfigurations(file: string): CompilerFileBasedTest {
196+
// also see `parseCompilerTestConfigurations` in tests/webTestServer.ts
197+
const content = Harness.IO.readFile(file);
198+
const rootDir = file.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(file) + "/";
199+
const payload = Harness.TestCaseParser.makeUnitsFromTest(content, file, rootDir);
200+
const settings = Harness.TestCaseParser.extractCompilerSettings(content);
201+
const scriptTargets = CompilerTest._split(settings.target);
202+
const moduleKinds = CompilerTest._split(settings.module);
203+
if (scriptTargets.length <= 1 && moduleKinds.length <= 1) {
204+
return { file, payload };
205+
}
206+
207+
const configurations: Harness.FileBasedTestConfiguration[] = [];
184208
for (const scriptTarget of scriptTargets) {
185209
for (const moduleKind of moduleKinds) {
210+
const settings: Record<string, any> = {};
186211
let name = "";
187212
if (moduleKinds.length > 1) {
213+
settings.module = moduleKind;
188214
name += `@module: ${moduleKind || "none"}`;
189215
}
190216
if (scriptTargets.length > 1) {
217+
settings.target = scriptTarget;
191218
if (name) name += ", ";
192219
name += `@target: ${scriptTarget || "none"}`;
193220
}
194221

195-
const settings = { ...testCaseContent.settings };
196-
if (scriptTarget) settings.target = scriptTarget;
197-
if (moduleKind) settings.module = moduleKind;
198-
configurations.push({ name, payload: { ...testCaseContent, settings } });
222+
configurations.push({ name, settings });
199223
}
200224
}
201225

202-
return configurations;
226+
return { file, payload, configurations };
203227
}
204228

205229
public verifyDiagnostics() {

src/harness/externalCompileRunner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract class ExternalCompileRunnerBase extends RunnerBase {
2828

2929
describe(`${this.kind()} code samples`, () => {
3030
for (const test of testList) {
31-
this.runTest(test);
31+
this.runTest(typeof test === "string" ? test : test.file);
3232
}
3333
});
3434
}

src/harness/fourslashRunner.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class FourSlashRunner extends RunnerBase {
3636
}
3737

3838
public enumerateTestFiles() {
39+
// see also: `enumerateTestFiles` in tests/webTestServer.ts
3940
return this.enumerateFiles(this.basePath, /\.ts/i, { recursive: false });
4041
}
4142

@@ -45,22 +46,23 @@ class FourSlashRunner extends RunnerBase {
4546

4647
public initializeTests() {
4748
if (this.tests.length === 0) {
48-
this.tests = this.enumerateTestFiles();
49+
this.tests = Harness.IO.enumerateTestFiles(this);
4950
}
5051

5152
describe(this.testSuiteName + " tests", () => {
52-
this.tests.forEach((fn: string) => {
53-
describe(fn, () => {
54-
fn = ts.normalizeSlashes(fn);
55-
const justName = fn.replace(/^.*[\\\/]/, "");
53+
this.tests.forEach(test => {
54+
const file = typeof test === "string" ? test : test.file;
55+
describe(file, () => {
56+
let fn = ts.normalizeSlashes(file);
57+
const justName = fn.replace(/^.*[\\\/]/, "");
5658

57-
// Convert to relative path
58-
const testIndex = fn.indexOf("tests/");
59-
if (testIndex >= 0) fn = fn.substr(testIndex);
59+
// Convert to relative path
60+
const testIndex = fn.indexOf("tests/");
61+
if (testIndex >= 0) fn = fn.substr(testIndex);
6062

61-
if (justName && !justName.match(/fourslash\.ts$/i) && !justName.match(/\.d\.ts$/i)) {
62-
it(this.testSuiteName + " test " + justName + " runs correctly", () => {
63-
FourSlash.runFourSlashTest(this.basePath, this.testType, fn);
63+
if (justName && !justName.match(/fourslash\.ts$/i) && !justName.match(/\.d\.ts$/i)) {
64+
it(this.testSuiteName + " test " + justName + " runs correctly", () => {
65+
FourSlash.runFourSlashTest(this.basePath, this.testType, fn);
6466
});
6567
}
6668
});

src/harness/harness.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,16 @@ namespace Utils {
499499
}
500500

501501
namespace Harness {
502+
export interface FileBasedTest {
503+
file: string;
504+
configurations?: FileBasedTestConfiguration[];
505+
}
506+
507+
export interface FileBasedTestConfiguration {
508+
name: string;
509+
settings?: Record<string, string>;
510+
}
511+
502512
// tslint:disable-next-line:interface-name
503513
export interface IO {
504514
newLine(): string;
@@ -514,6 +524,7 @@ namespace Harness {
514524
fileExists(fileName: string): boolean;
515525
directoryExists(path: string): boolean;
516526
deleteFile(fileName: string): void;
527+
enumerateTestFiles(runner: RunnerBase): (string | FileBasedTest)[];
517528
listFiles(path: string, filter?: RegExp, options?: { recursive?: boolean }): string[];
518529
log(text: string): void;
519530
args(): string[];
@@ -559,6 +570,10 @@ namespace Harness {
559570
return dirPath === path ? undefined : dirPath;
560571
}
561572

573+
function enumerateTestFiles(runner: RunnerBase) {
574+
return runner.enumerateTestFiles();
575+
}
576+
562577
function listFiles(path: string, spec: RegExp, options?: { recursive?: boolean }) {
563578
options = options || {};
564579

@@ -639,6 +654,7 @@ namespace Harness {
639654
directoryExists: path => ts.sys.directoryExists(path),
640655
deleteFile,
641656
listFiles,
657+
enumerateTestFiles,
642658
log: s => console.log(s),
643659
args: () => ts.sys.args,
644660
getExecutingFilePath: () => ts.sys.getExecutingFilePath(),
@@ -913,6 +929,11 @@ namespace Harness {
913929
return ts.getDirectoryPath(ts.normalizeSlashes(url.pathname || "/"));
914930
}
915931

932+
function enumerateTestFiles(runner: RunnerBase): (string | FileBasedTest)[] {
933+
const response = send(HttpRequestMessage.post(new URL("/api/enumerateTestFiles", serverRoot), HttpContent.text(runner.kind())));
934+
return hasJsonContent(response) ? JSON.parse(response.content.content) : [];
935+
}
936+
916937
function listFiles(dirname: string, spec?: RegExp, options?: { recursive?: boolean }): string[] {
917938
if (spec || (options && !options.recursive)) {
918939
let results = IO.listFiles(dirname);
@@ -959,6 +980,7 @@ namespace Harness {
959980
directoryExists,
960981
deleteFile,
961982
listFiles: Utils.memoize(listFiles, (path, spec, options) => `${path}|${spec}|${options ? options.recursive === true : true}`),
983+
enumerateTestFiles: Utils.memoize(enumerateTestFiles, runner => runner.kind()),
962984
log: s => console.log(s),
963985
args: () => [],
964986
getExecutingFilePath: () => "",
@@ -1779,7 +1801,7 @@ namespace Harness {
17791801
// Regex for parsing options in the format "@Alpha: Value of any sort"
17801802
const optionRegex = /^[\/]{2}\s*@(\w+)\s*:\s*([^\r\n]*)/gm; // multiple matches on multiple lines
17811803

1782-
function extractCompilerSettings(content: string): CompilerSettings {
1804+
export function extractCompilerSettings(content: string): CompilerSettings {
17831805
const opts: CompilerSettings = {};
17841806

17851807
let match: RegExpExecArray;
@@ -1800,9 +1822,7 @@ namespace Harness {
18001822
}
18011823

18021824
/** Given a test file containing // @FileName directives, return an array of named units of code to be added to an existing compiler instance */
1803-
export function makeUnitsFromTest(code: string, fileName: string, rootDir?: string): TestCaseContent {
1804-
const settings = extractCompilerSettings(code);
1805-
1825+
export function makeUnitsFromTest(code: string, fileName: string, rootDir?: string, settings = extractCompilerSettings(code)): TestCaseContent {
18061826
// List of all the subfiles we've parsed out
18071827
const testUnitData: TestUnitData[] = [];
18081828

src/harness/parallel/host.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ namespace Harness.Parallel.Host {
8181
const { statSync }: { statSync(path: string): { size: number }; } = require("fs");
8282
const path: { join: (...args: string[]) => string } = require("path");
8383
for (const runner of runners) {
84-
for (const file of runner.enumerateTestFiles()) {
84+
for (const test of runner.enumerateTestFiles()) {
85+
const file = typeof test === "string" ? test : test.file;
8586
let size: number;
8687
if (!perfData) {
8788
try {

src/harness/projectsRunner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace project {
5151
describe("projects tests", () => {
5252
const tests = this.tests.length === 0 ? this.enumerateTestFiles() : this.tests;
5353
for (const test of tests) {
54-
this.runProjectTestCase(test);
54+
this.runProjectTestCase(typeof test === "string" ? test : test.file);
5555
}
5656
});
5757
}

src/harness/runnerbase.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
/// <reference path="harness.ts" />
2-
3-
41
type TestRunnerKind = CompilerTestKind | FourslashTestKind | "project" | "rwc" | "test262" | "user" | "dt";
52
type CompilerTestKind = "conformance" | "compiler";
63
type FourslashTestKind = "fourslash" | "fourslash-shims" | "fourslash-shims-pp" | "fourslash-server";
74

85
abstract class RunnerBase {
96
// contains the tests to run
10-
public tests: string[] = [];
7+
public tests: (string | Harness.FileBasedTest)[] = [];
118

129
/** Add a source file to the runner's list of tests that need to be initialized with initializeTests */
1310
public addTest(fileName: string) {
@@ -20,7 +17,7 @@ abstract class RunnerBase {
2017

2118
abstract kind(): TestRunnerKind;
2219

23-
abstract enumerateTestFiles(): string[];
20+
abstract enumerateTestFiles(): (string | Harness.FileBasedTest)[];
2421

2522
/** The working directory where tests are found. Needed for batch testing where the input path will differ from the output path inside baselines */
2623
public workingDirectory = "";

src/harness/rwcRunner.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ namespace RWC {
232232

233233
class RWCRunner extends RunnerBase {
234234
public enumerateTestFiles() {
235+
// see also: `enumerateTestFiles` in tests/webTestServer.ts
235236
return Harness.IO.getDirectories("internal/cases/rwc/");
236237
}
237238

@@ -245,7 +246,7 @@ class RWCRunner extends RunnerBase {
245246
public initializeTests(): void {
246247
// Read in and evaluate the test list
247248
for (const test of this.tests && this.tests.length ? this.tests : this.enumerateTestFiles()) {
248-
this.runTest(test);
249+
this.runTest(typeof test === "string" ? test : test.file);
249250
}
250251
}
251252

src/harness/test262Runner.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class Test262BaselineRunner extends RunnerBase {
101101
}
102102

103103
public enumerateTestFiles() {
104+
// see also: `enumerateTestFiles` in tests/webTestServer.ts
104105
return ts.map(this.enumerateFiles(Test262BaselineRunner.basePath, Test262BaselineRunner.testFileExtensionRegex, { recursive: true }), ts.normalizePath);
105106
}
106107

@@ -113,7 +114,7 @@ class Test262BaselineRunner extends RunnerBase {
113114
});
114115
}
115116
else {
116-
this.tests.forEach(test => this.runTest(test));
117+
this.tests.forEach(test => this.runTest(typeof test === "string" ? test : test.file));
117118
}
118119
}
119120
}

0 commit comments

Comments
 (0)