Skip to content

Commit d2d2213

Browse files
committed
Merge pull request microsoft#5655 from Microsoft/harnessCleanup
cleanup test harness code
2 parents 531e096 + 50aab7a commit d2d2213

6 files changed

Lines changed: 141 additions & 165 deletions

File tree

src/harness/compilerRunner.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CompilerBaselineRunner extends RunnerBase {
4949
let testCaseContent: { settings: Harness.TestCaseParser.CompilerSettings; testUnitData: Harness.TestCaseParser.TestUnitData[]; };
5050

5151
let units: Harness.TestCaseParser.TestUnitData[];
52-
let tcSettings: Harness.TestCaseParser.CompilerSettings;
52+
let harnessSettings: Harness.TestCaseParser.CompilerSettings;
5353

5454
let lastUnit: Harness.TestCaseParser.TestUnitData;
5555
let rootDir: string;
@@ -58,46 +58,43 @@ class CompilerBaselineRunner extends RunnerBase {
5858
let program: ts.Program;
5959
let options: ts.CompilerOptions;
6060
// equivalent to the files that will be passed on the command line
61-
let toBeCompiled: { unitName: string; content: string }[];
61+
let toBeCompiled: Harness.Compiler.TestFile[];
6262
// equivalent to other files on the file system not directly passed to the compiler (ie things that are referenced by other files)
63-
let otherFiles: { unitName: string; content: string }[];
64-
let harnessCompiler: Harness.Compiler.HarnessCompiler;
63+
let otherFiles: Harness.Compiler.TestFile[];
6564

6665
before(() => {
6766
justName = fileName.replace(/^.*[\\\/]/, ""); // strips the fileName from the path.
6867
content = Harness.IO.readFile(fileName);
6968
testCaseContent = Harness.TestCaseParser.makeUnitsFromTest(content, fileName);
7069
units = testCaseContent.testUnitData;
71-
tcSettings = testCaseContent.settings;
70+
harnessSettings = testCaseContent.settings;
7271
lastUnit = units[units.length - 1];
7372
rootDir = lastUnit.originalFilePath.indexOf("conformance") === -1 ? "tests/cases/compiler/" : lastUnit.originalFilePath.substring(0, lastUnit.originalFilePath.lastIndexOf("/")) + "/";
74-
harnessCompiler = Harness.Compiler.getCompiler();
7573
// We need to assemble the list of input files for the compiler and other related files on the 'filesystem' (ie in a multi-file test)
7674
// If the last file in a test uses require or a triple slash reference we'll assume all other files will be brought in via references,
7775
// otherwise, assume all files are just meant to be in the same compilation session without explicit references to one another.
7876
toBeCompiled = [];
7977
otherFiles = [];
8078
if (/require\(/.test(lastUnit.content) || /reference\spath/.test(lastUnit.content)) {
81-
toBeCompiled.push({ unitName: rootDir + lastUnit.name, content: lastUnit.content });
79+
toBeCompiled.push({ unitName: ts.combinePaths(rootDir, lastUnit.name), content: lastUnit.content });
8280
units.forEach(unit => {
8381
if (unit.name !== lastUnit.name) {
84-
otherFiles.push({ unitName: rootDir + unit.name, content: unit.content });
82+
otherFiles.push({ unitName: ts.combinePaths(rootDir, unit.name), content: unit.content });
8583
}
8684
});
8785
}
8886
else {
8987
toBeCompiled = units.map(unit => {
90-
return { unitName: rootDir + unit.name, content: unit.content };
88+
return { unitName: ts.combinePaths(rootDir, unit.name), content: unit.content };
9189
});
9290
}
9391

94-
options = harnessCompiler.compileFiles(toBeCompiled, otherFiles, function (compileResult, _program) {
95-
result = compileResult;
96-
// The program will be used by typeWriter
97-
program = _program;
98-
}, function (settings) {
99-
harnessCompiler.setCompilerSettings(tcSettings);
100-
});
92+
const output = Harness.Compiler.HarnessCompiler.compileFiles(
93+
toBeCompiled, otherFiles, harnessSettings, /* options */ undefined, /* currentDirectory */ undefined);
94+
95+
options = output.options;
96+
result = output.result;
97+
program = output.program;
10198
});
10299

103100
after(() => {
@@ -107,22 +104,21 @@ class CompilerBaselineRunner extends RunnerBase {
107104
content = undefined;
108105
testCaseContent = undefined;
109106
units = undefined;
110-
tcSettings = undefined;
107+
harnessSettings = undefined;
111108
lastUnit = undefined;
112109
rootDir = undefined;
113110
result = undefined;
114111
program = undefined;
115112
options = undefined;
116113
toBeCompiled = undefined;
117114
otherFiles = undefined;
118-
harnessCompiler = undefined;
119115
});
120116

121117
function getByteOrderMarkText(file: Harness.Compiler.GeneratedFile): string {
122118
return file.writeByteOrderMark ? "\u00EF\u00BB\u00BF" : "";
123119
}
124120

125-
function getErrorBaseline(toBeCompiled: { unitName: string; content: string }[], otherFiles: { unitName: string; content: string }[], result: Harness.Compiler.CompilerResult) {
121+
function getErrorBaseline(toBeCompiled: Harness.Compiler.TestFile[], otherFiles: Harness.Compiler.TestFile[], result: Harness.Compiler.CompilerResult) {
126122
return Harness.Compiler.getErrorBaseline(toBeCompiled.concat(otherFiles), result.errors);
127123
}
128124

@@ -184,9 +180,9 @@ class CompilerBaselineRunner extends RunnerBase {
184180
}
185181
}
186182

187-
const declFileCompilationResult = harnessCompiler.compileDeclarationFiles(toBeCompiled, otherFiles, result, function (settings) {
188-
harnessCompiler.setCompilerSettings(tcSettings);
189-
}, options);
183+
const declFileCompilationResult =
184+
Harness.Compiler.HarnessCompiler.compileDeclarationFiles(
185+
toBeCompiled, otherFiles, result, harnessSettings, options, /* currentDirectory */ undefined);
190186

191187
if (declFileCompilationResult && declFileCompilationResult.declResult.errors.length) {
192188
jsCode += "\r\n\r\n//// [DtsFileErrors]\r\n";
@@ -367,7 +363,6 @@ class CompilerBaselineRunner extends RunnerBase {
367363
public initializeTests() {
368364
describe(this.testSuiteName + " tests", () => {
369365
describe("Setup compiler for compiler baselines", () => {
370-
const harnessCompiler = Harness.Compiler.getCompiler();
371366
this.parseOptions();
372367
});
373368

src/harness/fourslash.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,10 +2383,16 @@ namespace FourSlash {
23832383
// here we cache the JS output and reuse it for every test.
23842384
let fourslashJsOutput: string;
23852385
{
2386-
const host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFileName, content: undefined }],
2386+
const fourslashFile: Harness.Compiler.TestFileWithPath = {
2387+
unitName: Harness.Compiler.fourslashFileName,
2388+
content: undefined,
2389+
path: ts.toPath(Harness.Compiler.fourslashFileName, Harness.IO.getCurrentDirectory(), Harness.Compiler.getCanonicalFileName)
2390+
};
2391+
const host = Harness.Compiler.createCompilerHost([fourslashFile],
23872392
(fn, contents) => fourslashJsOutput = contents,
23882393
ts.ScriptTarget.Latest,
2389-
Harness.IO.useCaseSensitiveFileNames());
2394+
Harness.IO.useCaseSensitiveFileNames(),
2395+
Harness.IO.getCurrentDirectory());
23902396

23912397
const program = ts.createProgram([Harness.Compiler.fourslashFileName], { noResolve: true, target: ts.ScriptTarget.ES3 }, host);
23922398

@@ -2400,15 +2406,28 @@ namespace FourSlash {
24002406

24012407
currentTestState = new TestState(basePath, testType, testData);
24022408

2409+
const currentDirectory = Harness.IO.getCurrentDirectory();
2410+
const useCaseSensitiveFileNames = Harness.IO.useCaseSensitiveFileNames();
2411+
const getCanonicalFileName = ts.createGetCanonicalFileName(useCaseSensitiveFileNames);
2412+
24032413
let result = "";
2414+
const fourslashFile: Harness.Compiler.TestFileWithPath = {
2415+
unitName: Harness.Compiler.fourslashFileName,
2416+
content: undefined,
2417+
path: ts.toPath(Harness.Compiler.fourslashFileName, currentDirectory, getCanonicalFileName)
2418+
};
2419+
const testFile: Harness.Compiler.TestFileWithPath = {
2420+
unitName: fileName,
2421+
content: content,
2422+
path: ts.toPath(fileName, currentDirectory, getCanonicalFileName)
2423+
};
2424+
24042425
const host = Harness.Compiler.createCompilerHost(
2405-
[
2406-
{ unitName: Harness.Compiler.fourslashFileName, content: undefined },
2407-
{ unitName: fileName, content: content }
2408-
],
2426+
[ fourslashFile, testFile ],
24092427
(fn, contents) => result = contents,
24102428
ts.ScriptTarget.Latest,
2411-
Harness.IO.useCaseSensitiveFileNames());
2429+
useCaseSensitiveFileNames,
2430+
currentDirectory);
24122431

24132432
const program = ts.createProgram([Harness.Compiler.fourslashFileName, fileName], { outFile: "fourslashTestOutput.js", noResolve: true, target: ts.ScriptTarget.ES3 }, host);
24142433

0 commit comments

Comments
 (0)