Test harness fixes (#54556)

This commit is contained in:
Andrew Branch
2023-06-06 17:22:33 -07:00
committed by GitHub
parent a6d9858383
commit d5a6c8edc6
106 changed files with 380 additions and 371 deletions

View File

@@ -403,6 +403,7 @@ export namespace Compiler {
compilerOptions: ts.CompilerOptions | undefined,
// Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file
currentDirectory: string | undefined,
rootDir?: string,
symlinks?: vfs.FileSet
): compiler.CompilationResult {
const options: ts.CompilerOptions & HarnessOptions = compilerOptions ? ts.cloneCompilerOptions(compilerOptions) : { noResolve: false };
@@ -424,12 +425,12 @@ export namespace Compiler {
typeScriptVersion = harnessSettings.typeScriptVersion;
}
}
if (options.rootDirs) {
options.rootDirs = ts.map(options.rootDirs, d => ts.getNormalizedAbsolutePath(d, currentDirectory));
}
const useCaseSensitiveFileNames = options.useCaseSensitiveFileNames !== undefined ? options.useCaseSensitiveFileNames : true;
const programFileNames = inputFiles.map(file => file.unitName).filter(fileName => !ts.fileExtensionIs(fileName, ts.Extension.Json));
// When a tsconfig is present, root names passed to createProgram should already be absolute
const programFileNames = inputFiles
.map(file => options.configFile ? ts.getNormalizedAbsolutePath(file.unitName, currentDirectory) : file.unitName)
.filter(fileName => !ts.fileExtensionIs(fileName, ts.Extension.Json));
// Files from built\local that are requested by test "@includeBuiltFiles" to be in the context.
// Treat them as library files, so include them in build, but not in baselines.
@@ -449,6 +450,8 @@ export namespace Compiler {
if (symlinks) {
fs.apply(symlinks);
}
ts.assign(options, ts.convertToOptionsWithAbsolutePaths(options, path => ts.getNormalizedAbsolutePath(ts.getNormalizedAbsolutePath(path, rootDir), currentDirectory)));
const host = new fakes.CompilerHost(fs, options);
const result = compiler.compileFiles(host, programFileNames, options, typeScriptVersion);
result.symlinks = symlinks;
@@ -499,7 +502,10 @@ export namespace Compiler {
else if (vpath.isTypeScript(file.unitName) || (vpath.isJavaScript(file.unitName) && ts.getAllowJSCompilerOption(options))) {
const declFile = findResultCodeFile(file.unitName);
if (declFile && !findUnit(declFile.file, declInputFiles) && !findUnit(declFile.file, declOtherFiles)) {
dtsFiles.push({ unitName: declFile.file, content: Utils.removeByteOrderMark(declFile.text) });
dtsFiles.push({
unitName: declFile.file,
content: Utils.removeByteOrderMark(declFile.text)
});
}
}
}
@@ -539,7 +545,7 @@ export namespace Compiler {
return;
}
const { declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory } = context;
const output = compileFiles(declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory, symlinks);
const output = compileFiles(declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory, /*rootDir*/ undefined, symlinks);
return { declInputFiles, declOtherFiles, declResult: output };
}
@@ -1172,13 +1178,13 @@ export namespace TestCaseParser {
const optionRegex = /^[\/]{2}\s*@(\w+)\s*:\s*([^\r\n]*)/gm; // multiple matches on multiple lines
const linkRegex = /^[\/]{2}\s*@link\s*:\s*([^\r\n]*)\s*->\s*([^\r\n]*)/gm; // multiple matches on multiple lines
export function parseSymlinkFromTest(line: string, symlinks: vfs.FileSet | undefined) {
export function parseSymlinkFromTest(line: string, symlinks: vfs.FileSet | undefined, absoluteRootDir?: string) {
const linkMetaData = linkRegex.exec(line);
linkRegex.lastIndex = 0;
if (!linkMetaData) return undefined;
if (!symlinks) symlinks = {};
symlinks[linkMetaData[2].trim()] = new vfs.Symlink(linkMetaData[1].trim());
symlinks[ts.getNormalizedAbsolutePath(linkMetaData[2].trim(), absoluteRootDir)] = new vfs.Symlink(ts.getNormalizedAbsolutePath(linkMetaData[1].trim(), absoluteRootDir));
return symlinks;
}
@@ -1202,7 +1208,7 @@ export namespace TestCaseParser {
}
/** Given a test file containing // @FileName directives, return an array of named units of code to be added to an existing compiler instance */
export function makeUnitsFromTest(code: string, fileName: string, rootDir?: string, settings = extractCompilerSettings(code)): TestCaseContent {
export function makeUnitsFromTest(code: string, fileName: string, rootDir: string, settings = extractCompilerSettings(code)): TestCaseContent {
// List of all the subfiles we've parsed out
const testUnitData: TestUnitData[] = [];
@@ -1217,7 +1223,7 @@ export namespace TestCaseParser {
for (const line of lines) {
let testMetaData: RegExpExecArray | null;
const possiblySymlinks = parseSymlinkFromTest(line, symlinks);
const possiblySymlinks = parseSymlinkFromTest(line, symlinks, ts.getNormalizedAbsolutePath(rootDir, vfs.srcFolder));
if (possiblySymlinks) {
symlinks = possiblySymlinks;
}
@@ -1288,9 +1294,9 @@ export namespace TestCaseParser {
const files: string[] = [];
const directories = new Set<string>();
for (const unit of testUnitData) {
const unitName = ts.getNormalizedAbsolutePath(unit.name, rootDir);
if (unitName.toLowerCase().startsWith(dir.toLowerCase())) {
let path = unitName.substring(dir.length);
const fileName = ts.getNormalizedAbsolutePath(ts.getNormalizedAbsolutePath(unit.name, rootDir), vfs.srcFolder);
if (fileName.toLowerCase().startsWith(dir.toLowerCase())) {
let path = fileName.substring(dir.length);
if (path.startsWith("/")) {
path = path.substring(1);
}
@@ -1319,11 +1325,9 @@ export namespace TestCaseParser {
if (getConfigNameFromFileName(data.name)) {
const configJson = ts.parseJsonText(data.name, data.content);
assert.isTrue(configJson.endOfFileToken !== undefined);
let baseDir = ts.normalizePath(ts.getDirectoryPath(data.name));
if (rootDir) {
baseDir = ts.getNormalizedAbsolutePath(baseDir, rootDir);
}
tsConfig = ts.parseJsonSourceFileConfigFileContent(configJson, parseConfigHost, baseDir, /*existingOptions*/ undefined, ts.getNormalizedAbsolutePath(data.name, rootDir));
const configFileName = ts.getNormalizedAbsolutePath(ts.getNormalizedAbsolutePath(data.name, rootDir), vfs.srcFolder);
const configDir = ts.getDirectoryPath(configFileName);
tsConfig = ts.parseJsonSourceFileConfigFileContent(configJson, parseConfigHost, configDir, /*existingOptions*/ undefined, configFileName);
tsConfigFileUnitData = data;
// delete entry from the list

View File

@@ -92,9 +92,9 @@ export class CompilerBaselineRunner extends RunnerBase {
let compilerTest!: CompilerTest;
before(() => {
let payload;
let rootDir = ts.combinePaths(vfs.srcFolder, fileName.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(fileName) + "/");
let rootDir = fileName.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(fileName) + "/";
if (test && test.content) {
rootDir = ts.combinePaths(vfs.srcFolder, test.file.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(test.file) + "/");
rootDir = test.file.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(test.file) + "/";
payload = TestCaseParser.makeUnitsFromTest(test.content, test.file, rootDir);
}
compilerTest = new CompilerTest(fileName, rootDir, payload, configuration);
@@ -179,7 +179,8 @@ class CompilerTest {
// equivalent to other files on the file system not directly passed to the compiler (ie things that are referenced by other files)
private otherFiles: Compiler.TestFile[];
constructor(fileName: string, rootDir: string, testCaseContent?: TestCaseParser.TestCaseContent, configurationOverrides?: TestCaseParser.CompilerSettings) {
constructor(fileName: string, private rootDir: string, testCaseContent?: TestCaseParser.TestCaseContent, configurationOverrides?: TestCaseParser.CompilerSettings) {
const absoluteRootDir = ts.getNormalizedAbsolutePath(rootDir, vfs.srcFolder);
this.fileName = fileName;
this.justName = vpath.basename(fileName);
this.configuredName = this.justName;
@@ -218,20 +219,20 @@ class CompilerTest {
this.tsConfigFiles = [];
if (testCaseContent.tsConfig) {
tsConfigOptions = ts.cloneCompilerOptions(testCaseContent.tsConfig.options);
this.tsConfigFiles.push(this.createHarnessTestFile(testCaseContent.tsConfigFileUnitData!, rootDir, tsConfigOptions.configFilePath));
this.tsConfigFiles.push(this.createHarnessTestFile(testCaseContent.tsConfigFileUnitData!));
for (const unit of units) {
if (testCaseContent.tsConfig.fileNames.includes(ts.getNormalizedAbsolutePath(unit.name, rootDir))) {
this.toBeCompiled.push(this.createHarnessTestFile(unit, rootDir));
if (testCaseContent.tsConfig.fileNames.includes(ts.getNormalizedAbsolutePath(unit.name, absoluteRootDir))) {
this.toBeCompiled.push(this.createHarnessTestFile(unit));
}
else {
this.otherFiles.push(this.createHarnessTestFile(unit, rootDir));
this.otherFiles.push(this.createHarnessTestFile(unit));
}
}
}
else {
const baseUrl = this.harnessSettings.baseUrl;
if (baseUrl !== undefined && !ts.isRootedDiskPath(baseUrl)) {
this.harnessSettings.baseUrl = ts.getNormalizedAbsolutePath(baseUrl, rootDir);
this.harnessSettings.baseUrl = ts.getNormalizedAbsolutePath(baseUrl, absoluteRootDir);
}
const lastUnit = units[units.length - 1];
@@ -240,22 +241,21 @@ class CompilerTest {
// otherwise, assume all files are just meant to be in the same compilation session without explicit references to one another.
if (testCaseContent.settings.noImplicitReferences || /require\(/.test(lastUnit.content) || /reference\spath/.test(lastUnit.content)) {
this.toBeCompiled.push(this.createHarnessTestFile(lastUnit, rootDir));
this.toBeCompiled.push(this.createHarnessTestFile(lastUnit));
units.forEach(unit => {
if (unit.name !== lastUnit.name) {
this.otherFiles.push(this.createHarnessTestFile(unit, rootDir));
this.otherFiles.push(this.createHarnessTestFile(unit));
}
});
}
else {
this.toBeCompiled = units.map(unit => {
return this.createHarnessTestFile(unit, rootDir);
return this.createHarnessTestFile(unit);
});
}
}
if (tsConfigOptions && tsConfigOptions.configFilePath !== undefined) {
tsConfigOptions.configFilePath = ts.combinePaths(rootDir, tsConfigOptions.configFilePath);
tsConfigOptions.configFile!.fileName = tsConfigOptions.configFilePath;
}
@@ -265,6 +265,7 @@ class CompilerTest {
this.harnessSettings,
/*options*/ tsConfigOptions,
/*currentDirectory*/ this.harnessSettings.currentDirectory,
this.rootDir,
testCaseContent.symlinks
);
@@ -352,13 +353,11 @@ class CompilerTest {
);
}
private makeUnitName(name: string, root: string) {
const path = ts.toPath(name, root, ts.identity);
const pathStart = ts.toPath(IO.getCurrentDirectory(), "", ts.identity);
return pathStart ? path.replace(pathStart, "/") : path;
}
private createHarnessTestFile(lastUnit: TestCaseParser.TestUnitData, rootDir: string, unitName?: string): Compiler.TestFile {
return { unitName: unitName || this.makeUnitName(lastUnit.name, rootDir), content: lastUnit.content, fileOptions: lastUnit.fileOptions };
private createHarnessTestFile(unit: TestCaseParser.TestUnitData): Compiler.TestFile {
return {
unitName: ts.getNormalizedAbsolutePath(unit.name, this.rootDir),
content: unit.content,
fileOptions: unit.fileOptions
};
}
}