Skip to content

Commit d5a6c8e

Browse files
authored
Test harness fixes (#54556)
1 parent a6d9858 commit d5a6c8e

File tree

106 files changed

+380
-371
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+380
-371
lines changed

src/harness/harnessIO.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ export namespace Compiler {
403403
compilerOptions: ts.CompilerOptions | undefined,
404404
// Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file
405405
currentDirectory: string | undefined,
406+
rootDir?: string,
406407
symlinks?: vfs.FileSet
407408
): compiler.CompilationResult {
408409
const options: ts.CompilerOptions & HarnessOptions = compilerOptions ? ts.cloneCompilerOptions(compilerOptions) : { noResolve: false };
@@ -424,12 +425,12 @@ export namespace Compiler {
424425
typeScriptVersion = harnessSettings.typeScriptVersion;
425426
}
426427
}
427-
if (options.rootDirs) {
428-
options.rootDirs = ts.map(options.rootDirs, d => ts.getNormalizedAbsolutePath(d, currentDirectory));
429-
}
430428

431429
const useCaseSensitiveFileNames = options.useCaseSensitiveFileNames !== undefined ? options.useCaseSensitiveFileNames : true;
432-
const programFileNames = inputFiles.map(file => file.unitName).filter(fileName => !ts.fileExtensionIs(fileName, ts.Extension.Json));
430+
// When a tsconfig is present, root names passed to createProgram should already be absolute
431+
const programFileNames = inputFiles
432+
.map(file => options.configFile ? ts.getNormalizedAbsolutePath(file.unitName, currentDirectory) : file.unitName)
433+
.filter(fileName => !ts.fileExtensionIs(fileName, ts.Extension.Json));
433434

434435
// Files from built\local that are requested by test "@includeBuiltFiles" to be in the context.
435436
// Treat them as library files, so include them in build, but not in baselines.
@@ -449,6 +450,8 @@ export namespace Compiler {
449450
if (symlinks) {
450451
fs.apply(symlinks);
451452
}
453+
454+
ts.assign(options, ts.convertToOptionsWithAbsolutePaths(options, path => ts.getNormalizedAbsolutePath(ts.getNormalizedAbsolutePath(path, rootDir), currentDirectory)));
452455
const host = new fakes.CompilerHost(fs, options);
453456
const result = compiler.compileFiles(host, programFileNames, options, typeScriptVersion);
454457
result.symlinks = symlinks;
@@ -499,7 +502,10 @@ export namespace Compiler {
499502
else if (vpath.isTypeScript(file.unitName) || (vpath.isJavaScript(file.unitName) && ts.getAllowJSCompilerOption(options))) {
500503
const declFile = findResultCodeFile(file.unitName);
501504
if (declFile && !findUnit(declFile.file, declInputFiles) && !findUnit(declFile.file, declOtherFiles)) {
502-
dtsFiles.push({ unitName: declFile.file, content: Utils.removeByteOrderMark(declFile.text) });
505+
dtsFiles.push({
506+
unitName: declFile.file,
507+
content: Utils.removeByteOrderMark(declFile.text)
508+
});
503509
}
504510
}
505511
}
@@ -539,7 +545,7 @@ export namespace Compiler {
539545
return;
540546
}
541547
const { declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory } = context;
542-
const output = compileFiles(declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory, symlinks);
548+
const output = compileFiles(declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory, /*rootDir*/ undefined, symlinks);
543549
return { declInputFiles, declOtherFiles, declResult: output };
544550
}
545551

@@ -1172,13 +1178,13 @@ export namespace TestCaseParser {
11721178
const optionRegex = /^[\/]{2}\s*@(\w+)\s*:\s*([^\r\n]*)/gm; // multiple matches on multiple lines
11731179
const linkRegex = /^[\/]{2}\s*@link\s*:\s*([^\r\n]*)\s*->\s*([^\r\n]*)/gm; // multiple matches on multiple lines
11741180

1175-
export function parseSymlinkFromTest(line: string, symlinks: vfs.FileSet | undefined) {
1181+
export function parseSymlinkFromTest(line: string, symlinks: vfs.FileSet | undefined, absoluteRootDir?: string) {
11761182
const linkMetaData = linkRegex.exec(line);
11771183
linkRegex.lastIndex = 0;
11781184
if (!linkMetaData) return undefined;
11791185

11801186
if (!symlinks) symlinks = {};
1181-
symlinks[linkMetaData[2].trim()] = new vfs.Symlink(linkMetaData[1].trim());
1187+
symlinks[ts.getNormalizedAbsolutePath(linkMetaData[2].trim(), absoluteRootDir)] = new vfs.Symlink(ts.getNormalizedAbsolutePath(linkMetaData[1].trim(), absoluteRootDir));
11821188
return symlinks;
11831189
}
11841190

@@ -1202,7 +1208,7 @@ export namespace TestCaseParser {
12021208
}
12031209

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

@@ -1217,7 +1223,7 @@ export namespace TestCaseParser {
12171223

12181224
for (const line of lines) {
12191225
let testMetaData: RegExpExecArray | null;
1220-
const possiblySymlinks = parseSymlinkFromTest(line, symlinks);
1226+
const possiblySymlinks = parseSymlinkFromTest(line, symlinks, ts.getNormalizedAbsolutePath(rootDir, vfs.srcFolder));
12211227
if (possiblySymlinks) {
12221228
symlinks = possiblySymlinks;
12231229
}
@@ -1288,9 +1294,9 @@ export namespace TestCaseParser {
12881294
const files: string[] = [];
12891295
const directories = new Set<string>();
12901296
for (const unit of testUnitData) {
1291-
const unitName = ts.getNormalizedAbsolutePath(unit.name, rootDir);
1292-
if (unitName.toLowerCase().startsWith(dir.toLowerCase())) {
1293-
let path = unitName.substring(dir.length);
1297+
const fileName = ts.getNormalizedAbsolutePath(ts.getNormalizedAbsolutePath(unit.name, rootDir), vfs.srcFolder);
1298+
if (fileName.toLowerCase().startsWith(dir.toLowerCase())) {
1299+
let path = fileName.substring(dir.length);
12941300
if (path.startsWith("/")) {
12951301
path = path.substring(1);
12961302
}
@@ -1319,11 +1325,9 @@ export namespace TestCaseParser {
13191325
if (getConfigNameFromFileName(data.name)) {
13201326
const configJson = ts.parseJsonText(data.name, data.content);
13211327
assert.isTrue(configJson.endOfFileToken !== undefined);
1322-
let baseDir = ts.normalizePath(ts.getDirectoryPath(data.name));
1323-
if (rootDir) {
1324-
baseDir = ts.getNormalizedAbsolutePath(baseDir, rootDir);
1325-
}
1326-
tsConfig = ts.parseJsonSourceFileConfigFileContent(configJson, parseConfigHost, baseDir, /*existingOptions*/ undefined, ts.getNormalizedAbsolutePath(data.name, rootDir));
1328+
const configFileName = ts.getNormalizedAbsolutePath(ts.getNormalizedAbsolutePath(data.name, rootDir), vfs.srcFolder);
1329+
const configDir = ts.getDirectoryPath(configFileName);
1330+
tsConfig = ts.parseJsonSourceFileConfigFileContent(configJson, parseConfigHost, configDir, /*existingOptions*/ undefined, configFileName);
13271331
tsConfigFileUnitData = data;
13281332

13291333
// delete entry from the list

src/testRunner/compilerRunner.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ export class CompilerBaselineRunner extends RunnerBase {
9292
let compilerTest!: CompilerTest;
9393
before(() => {
9494
let payload;
95-
let rootDir = ts.combinePaths(vfs.srcFolder, fileName.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(fileName) + "/");
95+
let rootDir = fileName.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(fileName) + "/";
9696
if (test && test.content) {
97-
rootDir = ts.combinePaths(vfs.srcFolder, test.file.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(test.file) + "/");
97+
rootDir = test.file.indexOf("conformance") === -1 ? "tests/cases/compiler/" : ts.getDirectoryPath(test.file) + "/";
9898
payload = TestCaseParser.makeUnitsFromTest(test.content, test.file, rootDir);
9999
}
100100
compilerTest = new CompilerTest(fileName, rootDir, payload, configuration);
@@ -179,7 +179,8 @@ class CompilerTest {
179179
// equivalent to other files on the file system not directly passed to the compiler (ie things that are referenced by other files)
180180
private otherFiles: Compiler.TestFile[];
181181

182-
constructor(fileName: string, rootDir: string, testCaseContent?: TestCaseParser.TestCaseContent, configurationOverrides?: TestCaseParser.CompilerSettings) {
182+
constructor(fileName: string, private rootDir: string, testCaseContent?: TestCaseParser.TestCaseContent, configurationOverrides?: TestCaseParser.CompilerSettings) {
183+
const absoluteRootDir = ts.getNormalizedAbsolutePath(rootDir, vfs.srcFolder);
183184
this.fileName = fileName;
184185
this.justName = vpath.basename(fileName);
185186
this.configuredName = this.justName;
@@ -218,20 +219,20 @@ class CompilerTest {
218219
this.tsConfigFiles = [];
219220
if (testCaseContent.tsConfig) {
220221
tsConfigOptions = ts.cloneCompilerOptions(testCaseContent.tsConfig.options);
221-
this.tsConfigFiles.push(this.createHarnessTestFile(testCaseContent.tsConfigFileUnitData!, rootDir, tsConfigOptions.configFilePath));
222+
this.tsConfigFiles.push(this.createHarnessTestFile(testCaseContent.tsConfigFileUnitData!));
222223
for (const unit of units) {
223-
if (testCaseContent.tsConfig.fileNames.includes(ts.getNormalizedAbsolutePath(unit.name, rootDir))) {
224-
this.toBeCompiled.push(this.createHarnessTestFile(unit, rootDir));
224+
if (testCaseContent.tsConfig.fileNames.includes(ts.getNormalizedAbsolutePath(unit.name, absoluteRootDir))) {
225+
this.toBeCompiled.push(this.createHarnessTestFile(unit));
225226
}
226227
else {
227-
this.otherFiles.push(this.createHarnessTestFile(unit, rootDir));
228+
this.otherFiles.push(this.createHarnessTestFile(unit));
228229
}
229230
}
230231
}
231232
else {
232233
const baseUrl = this.harnessSettings.baseUrl;
233234
if (baseUrl !== undefined && !ts.isRootedDiskPath(baseUrl)) {
234-
this.harnessSettings.baseUrl = ts.getNormalizedAbsolutePath(baseUrl, rootDir);
235+
this.harnessSettings.baseUrl = ts.getNormalizedAbsolutePath(baseUrl, absoluteRootDir);
235236
}
236237

237238
const lastUnit = units[units.length - 1];
@@ -240,22 +241,21 @@ class CompilerTest {
240241
// otherwise, assume all files are just meant to be in the same compilation session without explicit references to one another.
241242

242243
if (testCaseContent.settings.noImplicitReferences || /require\(/.test(lastUnit.content) || /reference\spath/.test(lastUnit.content)) {
243-
this.toBeCompiled.push(this.createHarnessTestFile(lastUnit, rootDir));
244+
this.toBeCompiled.push(this.createHarnessTestFile(lastUnit));
244245
units.forEach(unit => {
245246
if (unit.name !== lastUnit.name) {
246-
this.otherFiles.push(this.createHarnessTestFile(unit, rootDir));
247+
this.otherFiles.push(this.createHarnessTestFile(unit));
247248
}
248249
});
249250
}
250251
else {
251252
this.toBeCompiled = units.map(unit => {
252-
return this.createHarnessTestFile(unit, rootDir);
253+
return this.createHarnessTestFile(unit);
253254
});
254255
}
255256
}
256257

257258
if (tsConfigOptions && tsConfigOptions.configFilePath !== undefined) {
258-
tsConfigOptions.configFilePath = ts.combinePaths(rootDir, tsConfigOptions.configFilePath);
259259
tsConfigOptions.configFile!.fileName = tsConfigOptions.configFilePath;
260260
}
261261

@@ -265,6 +265,7 @@ class CompilerTest {
265265
this.harnessSettings,
266266
/*options*/ tsConfigOptions,
267267
/*currentDirectory*/ this.harnessSettings.currentDirectory,
268+
this.rootDir,
268269
testCaseContent.symlinks
269270
);
270271

@@ -352,13 +353,11 @@ class CompilerTest {
352353
);
353354
}
354355

355-
private makeUnitName(name: string, root: string) {
356-
const path = ts.toPath(name, root, ts.identity);
357-
const pathStart = ts.toPath(IO.getCurrentDirectory(), "", ts.identity);
358-
return pathStart ? path.replace(pathStart, "/") : path;
359-
}
360-
361-
private createHarnessTestFile(lastUnit: TestCaseParser.TestUnitData, rootDir: string, unitName?: string): Compiler.TestFile {
362-
return { unitName: unitName || this.makeUnitName(lastUnit.name, rootDir), content: lastUnit.content, fileOptions: lastUnit.fileOptions };
356+
private createHarnessTestFile(unit: TestCaseParser.TestUnitData): Compiler.TestFile {
357+
return {
358+
unitName: ts.getNormalizedAbsolutePath(unit.name, this.rootDir),
359+
content: unit.content,
360+
fileOptions: unit.fileOptions
361+
};
363362
}
364363
}

tests/baselines/reference/amdLikeInputDeclarationEmit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ define("lib/ExtendedClass", ["deps/BaseClass"],
3333

3434

3535
//// [ExtendedClass.d.ts]
36-
/// <reference path="../tests/cases/compiler/deps/BaseClass.d.ts" />
36+
/// <reference path="../deps/BaseClass.d.ts" />
3737
export = ExtendedClass;
3838
declare const ExtendedClass: new () => {
3939
f: () => "something";

tests/baselines/reference/bundledDtsLateExportRenaming.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ declare module "index" {
5454
//// [DtsFileErrors]
5555

5656

57-
dist/out.d.ts(10,33): error TS2307: Cannot find module 'nested' or its corresponding type declarations.
57+
tests/cases/compiler/dist/out.d.ts(10,33): error TS2307: Cannot find module 'nested' or its corresponding type declarations.
5858

5959

60-
==== ./dist/out.d.ts (1 errors) ====
60+
==== tests/cases/compiler/dist/out.d.ts (1 errors) ====
6161
declare module "nested/shared" {
6262
export class B {
6363
}

tests/baselines/reference/bundlerImportTsExtensions(allowimportingtsextensions=false,noemit=false).errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error TS5056: Cannot write file 'out/b.js' because it would be overwritten by multiple input files.
2-
error TS5056: Cannot write file 'out/c.js' because it would be overwritten by multiple input files.
1+
error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/b.js' because it would be overwritten by multiple input files.
2+
error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/c.js' because it would be overwritten by multiple input files.
33
error TS6054: File '/project/e.txt' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx', '.cts', '.d.cts', '.cjs', '.mts', '.d.mts', '.mjs'.
44
The file is in the program because:
55
Root file specified for compilation
@@ -13,8 +13,8 @@ error TS6054: File '/project/e.txt' has an unsupported extension. The only suppo
1313
/project/types.d.ts(2,16): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.js' instead?
1414

1515

16-
!!! error TS5056: Cannot write file 'out/b.js' because it would be overwritten by multiple input files.
17-
!!! error TS5056: Cannot write file 'out/c.js' because it would be overwritten by multiple input files.
16+
!!! error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/b.js' because it would be overwritten by multiple input files.
17+
!!! error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/c.js' because it would be overwritten by multiple input files.
1818
!!! error TS6054: File '/project/e.txt' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx', '.cts', '.d.cts', '.cjs', '.mts', '.d.mts', '.mjs'.
1919
!!! error TS6054: The file is in the program because:
2020
!!! error TS6054: Root file specified for compilation

tests/baselines/reference/bundlerImportTsExtensions(allowimportingtsextensions=true,noemit=false).errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error TS5056: Cannot write file 'out/b.js' because it would be overwritten by multiple input files.
2-
error TS5056: Cannot write file 'out/c.js' because it would be overwritten by multiple input files.
1+
error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/b.js' because it would be overwritten by multiple input files.
2+
error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/c.js' because it would be overwritten by multiple input files.
33
error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.
44
error TS6054: File '/project/e.txt' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx', '.cts', '.d.cts', '.cjs', '.mts', '.d.mts', '.mjs'.
55
The file is in the program because:
@@ -9,8 +9,8 @@ error TS6054: File '/project/e.txt' has an unsupported extension. The only suppo
99
/project/types.d.ts(2,16): error TS2846: A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file './a.ts' instead?
1010

1111

12-
!!! error TS5056: Cannot write file 'out/b.js' because it would be overwritten by multiple input files.
13-
!!! error TS5056: Cannot write file 'out/c.js' because it would be overwritten by multiple input files.
12+
!!! error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/b.js' because it would be overwritten by multiple input files.
13+
!!! error TS5056: Cannot write file 'tests/cases/conformance/moduleResolution/bundler/out/c.js' because it would be overwritten by multiple input files.
1414
!!! error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.
1515
!!! error TS6054: File '/project/e.txt' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx', '.cts', '.d.cts', '.cjs', '.mts', '.d.mts', '.mjs'.
1616
!!! error TS6054: The file is in the program because:

tests/baselines/reference/declarationEmitCommonSourceDirectoryDoesNotContainAllFiles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ declare module "index" {
2828
//// [DtsFileErrors]
2929

3030

31-
dist/index.d.ts(2,23): error TS2307: Cannot find module 'b' or its corresponding type declarations.
31+
/a/tests/cases/compiler/dist/index.d.ts(2,23): error TS2307: Cannot find module 'b' or its corresponding type declarations.
3232

3333

34-
==== dist/index.d.ts (1 errors) ====
34+
==== /a/tests/cases/compiler/dist/index.d.ts (1 errors) ====
3535
declare module "src/index" {
3636
import { B } from "b";
3737
~~~
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error TS5055: Cannot write file 'tests/cases/compiler/out.d.ts' because it would overwrite input file.
1+
error TS5055: Cannot write file '/out.d.ts' because it would overwrite input file.
22
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
33
error TS5101: Option 'out' is deprecated and will stop functioning in TypeScript 5.5. Specify compilerOption '"ignoreDeprecations": "5.0"' to silence this error.
44
Use 'outFile' instead.
55

66

7-
!!! error TS5055: Cannot write file 'tests/cases/compiler/out.d.ts' because it would overwrite input file.
7+
!!! error TS5055: Cannot write file '/out.d.ts' because it would overwrite input file.
88
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
99
!!! error TS5101: Option 'out' is deprecated and will stop functioning in TypeScript 5.5. Specify compilerOption '"ignoreDeprecations": "5.0"' to silence this error.
1010
!!! error TS5101: Use 'outFile' instead.
11-
==== tests/cases/compiler/out.d.ts (0 errors) ====
11+
==== /out.d.ts (0 errors) ====
1212
declare class c {
1313
}
1414

15-
==== tests/cases/compiler/a.ts (0 errors) ====
15+
==== /a.ts (0 errors) ====
1616
class d {
1717
}
1818

0 commit comments

Comments
 (0)