Skip to content

Commit c117c26

Browse files
authored
Refactor to simplify project references tsc-watch and tsserver tests (#42926)
1 parent 2a01f92 commit c117c26

11 files changed

+4022
-605
lines changed

src/testRunner/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
"unittests/tscWatch/forceConsistentCasingInFileNames.ts",
148148
"unittests/tscWatch/incremental.ts",
149149
"unittests/tscWatch/programUpdates.ts",
150+
"unittests/tscWatch/projectsWithReferences.ts",
150151
"unittests/tscWatch/resolutionCache.ts",
151152
"unittests/tscWatch/sourceOfProjectReferenceRedirect.ts",
152153
"unittests/tscWatch/watchApi.ts",
@@ -192,6 +193,7 @@
192193
"unittests/tsserver/projectReferences.ts",
193194
"unittests/tsserver/projectReferencesSourcemap.ts",
194195
"unittests/tsserver/projects.ts",
196+
"unittests/tsserver/projectsWithReferences.ts",
195197
"unittests/tsserver/refactors.ts",
196198
"unittests/tsserver/reload.ts",
197199
"unittests/tsserver/reloadProjects.ts",

src/testRunner/unittests/tsbuild/watchMode.ts

Lines changed: 0 additions & 594 deletions
Large diffs are not rendered by default.

src/testRunner/unittests/tsc/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace ts {
2929
baselineSourceMap?: boolean;
3030
baselineReadFileCalls?: boolean;
3131
baselinePrograms?: boolean;
32+
baselineDependencies?: boolean;
3233
}
3334

3435
export type CommandLineProgram = [Program, EmitAndSemanticDiagnosticsBuilderProgram?];
@@ -73,7 +74,7 @@ namespace ts {
7374
const {
7475
scenario, subScenario, buildKind,
7576
commandLineArgs, modifyFs,
76-
baselineSourceMap, baselineReadFileCalls, baselinePrograms
77+
baselineSourceMap, baselineReadFileCalls, baselinePrograms, baselineDependencies
7778
} = input;
7879
if (modifyFs) modifyFs(inputFs);
7980
inputFs.makeReadonly();
@@ -110,7 +111,7 @@ namespace ts {
110111
sys.write(`exitCode:: ExitStatus.${ExitStatus[sys.exitCode as ExitStatus]}\n`);
111112
if (baselinePrograms) {
112113
const baseline: string[] = [];
113-
tscWatch.baselinePrograms(baseline, getPrograms);
114+
tscWatch.baselinePrograms(baseline, getPrograms, baselineDependencies);
114115
sys.write(baseline.join("\n"));
115116
}
116117
if (baselineReadFileCalls) {

src/testRunner/unittests/tscWatch/helpers.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ namespace ts.tscWatch {
299299
}
300300
export interface TscWatchCheckOptions {
301301
baselineSourceMap?: boolean;
302+
baselineDependencies?: boolean;
302303
}
303304
export interface TscWatchCompileBase extends TscWatchCheckOptions {
304305
scenario: string;
@@ -323,7 +324,7 @@ namespace ts.tscWatch {
323324
const {
324325
scenario, subScenario,
325326
commandLineArgs, changes,
326-
baselineSourceMap
327+
baselineSourceMap, baselineDependencies
327328
} = input;
328329

329330
if (!isWatch(commandLineArgs)) sys.exit = exitCode => sys.exitCode = exitCode;
@@ -342,6 +343,7 @@ namespace ts.tscWatch {
342343
oldSnap,
343344
getPrograms,
344345
baselineSourceMap,
346+
baselineDependencies,
345347
changes,
346348
watchOrSolution
347349
});
@@ -381,7 +383,7 @@ namespace ts.tscWatch {
381383
export function runWatchBaseline({
382384
scenario, subScenario, commandLineArgs,
383385
getPrograms, sys, baseline, oldSnap,
384-
baselineSourceMap,
386+
baselineSourceMap, baselineDependencies,
385387
changes, watchOrSolution
386388
}: RunWatchBaseline) {
387389
baseline.push(`${sys.getExecutingFilePath()} ${commandLineArgs.join(" ")}`);
@@ -390,7 +392,8 @@ namespace ts.tscWatch {
390392
getPrograms,
391393
sys,
392394
oldSnap,
393-
baselineSourceMap
395+
baselineSourceMap,
396+
baselineDependencies,
394397
});
395398

396399
for (const { caption, change, timeouts } of changes) {
@@ -401,7 +404,8 @@ namespace ts.tscWatch {
401404
getPrograms,
402405
sys,
403406
oldSnap,
404-
baselineSourceMap
407+
baselineSourceMap,
408+
baselineDependencies,
405409
});
406410
}
407411
Harness.Baseline.runBaseline(`${isBuild(commandLineArgs) ?
@@ -420,10 +424,10 @@ namespace ts.tscWatch {
420424
export interface WatchBaseline extends Baseline, TscWatchCheckOptions {
421425
getPrograms: () => readonly CommandLineProgram[];
422426
}
423-
export function watchBaseline({ baseline, getPrograms, sys, oldSnap, baselineSourceMap }: WatchBaseline) {
427+
export function watchBaseline({ baseline, getPrograms, sys, oldSnap, baselineSourceMap, baselineDependencies }: WatchBaseline) {
424428
if (baselineSourceMap) generateSourceMapBaselineFiles(sys);
425429
sys.serializeOutput(baseline);
426-
const programs = baselinePrograms(baseline, getPrograms);
430+
const programs = baselinePrograms(baseline, getPrograms, baselineDependencies);
427431
sys.serializeWatches(baseline);
428432
baseline.push(`exitCode:: ExitStatus.${ExitStatus[sys.exitCode as ExitStatus]}`, "");
429433
sys.diff(baseline, oldSnap);
@@ -434,15 +438,15 @@ namespace ts.tscWatch {
434438
return programs;
435439
}
436440

437-
export function baselinePrograms(baseline: string[], getPrograms: () => readonly CommandLineProgram[]) {
441+
export function baselinePrograms(baseline: string[], getPrograms: () => readonly CommandLineProgram[], baselineDependencies: boolean | undefined) {
438442
const programs = getPrograms();
439443
for (const program of programs) {
440-
baselineProgram(baseline, program);
444+
baselineProgram(baseline, program, baselineDependencies);
441445
}
442446
return programs;
443447
}
444448

445-
function baselineProgram(baseline: string[], [program, builderProgram]: CommandLineProgram) {
449+
function baselineProgram(baseline: string[], [program, builderProgram]: CommandLineProgram, baselineDependencies: boolean | undefined) {
446450
const options = program.getCompilerOptions();
447451
baseline.push(`Program root files: ${JSON.stringify(program.getRootFileNames())}`);
448452
baseline.push(`Program options: ${JSON.stringify(options)}`);
@@ -466,6 +470,15 @@ namespace ts.tscWatch {
466470
baseline.push("No cached semantic diagnostics in the builder::");
467471
}
468472
baseline.push("");
473+
if (!baselineDependencies) return;
474+
baseline.push("Dependencies for::");
475+
for (const file of builderProgram.getSourceFiles()) {
476+
baseline.push(`${file.fileName}:`);
477+
for (const depenedency of builderProgram.getAllDependencies(file)) {
478+
baseline.push(` ${depenedency}`);
479+
}
480+
}
481+
baseline.push("");
469482
}
470483

471484
export interface VerifyTscWatch extends TscWatchCompile {
@@ -492,4 +505,31 @@ namespace ts.tscWatch {
492505
const content = Debug.checkDefined(sys.readFile(file));
493506
sys.writeFile(file, content.replace(searchValue, replaceValue));
494507
}
508+
509+
export function createSolutionBuilder(system: WatchedSystem, rootNames: readonly string[], defaultOptions?: BuildOptions) {
510+
const host = createSolutionBuilderHost(system);
511+
return ts.createSolutionBuilder(host, rootNames, defaultOptions || {});
512+
}
513+
514+
export function ensureErrorFreeBuild(host: WatchedSystem, rootNames: readonly string[]) {
515+
// ts build should succeed
516+
const solutionBuilder = createSolutionBuilder(host, rootNames, {});
517+
solutionBuilder.build();
518+
assert.equal(host.getOutput().length, 0, JSON.stringify(host.getOutput(), /*replacer*/ undefined, " "));
519+
}
520+
521+
export function createSystemWithSolutionBuild(solutionRoots: readonly string[], files: readonly TestFSWithWatch.FileOrFolderOrSymLink[], params?: TestFSWithWatch.TestServerHostCreationParameters) {
522+
const sys = createWatchedSystem(files, params);
523+
const originalReadFile = sys.readFile;
524+
const originalWrite = sys.write;
525+
const originalWriteFile = sys.writeFile;
526+
const solutionBuilder = createSolutionBuilder(TestFSWithWatch.changeToHostTrackingWrittenFiles(
527+
fakes.patchHostForBuildInfoReadWrite(sys)
528+
), solutionRoots, {});
529+
solutionBuilder.build();
530+
sys.readFile = originalReadFile;
531+
sys.write = originalWrite;
532+
sys.writeFile = originalWriteFile;
533+
return sys;
534+
}
495535
}

0 commit comments

Comments
 (0)