Skip to content

Commit c0c5fbc

Browse files
committed
Tests
1 parent 0279cde commit c0c5fbc

24 files changed

+10392
-0
lines changed

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,10 @@ interface Array<T> { length: number; [n: number]: T; }`
10151015
this.modifyFile(path, this.readFile(path) + content, options);
10161016
}
10171017

1018+
prependFile(path: string, content: string, options?: Partial<ReloadWatchInvokeOptions>): void {
1019+
this.modifyFile(path, content + this.readFile(path), options);
1020+
}
1021+
10181022
write(message: string) {
10191023
this.output.push(message);
10201024
}

src/testRunner/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"unittests/tsbuild/moduleSpecifiers.ts",
125125
"unittests/tsbuild/noEmitOnError.ts",
126126
"unittests/tsbuild/outFile.ts",
127+
"unittests/tsbuild/persistResolutions.ts",
127128
"unittests/tsbuild/referencesWithRootDirInParent.ts",
128129
"unittests/tsbuild/resolveJsonModule.ts",
129130
"unittests/tsbuild/sample.ts",
@@ -134,13 +135,15 @@
134135
"unittests/tsc/declarationEmit.ts",
135136
"unittests/tsc/incremental.ts",
136137
"unittests/tsc/listFilesOnly.ts",
138+
"unittests/tsc/persistResolutions.ts",
137139
"unittests/tsc/projectReferences.ts",
138140
"unittests/tsc/runWithoutArgs.ts",
139141
"unittests/tscWatch/consoleClearing.ts",
140142
"unittests/tscWatch/emit.ts",
141143
"unittests/tscWatch/emitAndErrorUpdates.ts",
142144
"unittests/tscWatch/forceConsistentCasingInFileNames.ts",
143145
"unittests/tscWatch/incremental.ts",
146+
"unittests/tscWatch/persistResolutions.ts",
144147
"unittests/tscWatch/programUpdates.ts",
145148
"unittests/tscWatch/resolutionCache.ts",
146149
"unittests/tscWatch/sourceOfProjectReferenceRedirect.ts",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace ts {
2+
export function verifyTscPersistsResolutions(input: "--p" | "--b", outFile?: string) {
3+
verifyTscSerializedIncrementalEdits({
4+
scenario: "persistResolutions",
5+
subScenario: `saves resolution and uses it for new program${outFile ? " with outFile" : ""}`,
6+
fs: () => loadProjectFromFiles({
7+
"/src/project/src/main.ts": Utils.dedent`
8+
import { something } from "./filePresent";
9+
import { something2 } from "./fileNotFound";`,
10+
"/src/project/src/filePresent.ts": `export function something() { return 10; }`,
11+
"/src/project/tsconfig.json": JSON.stringify({
12+
compilerOptions: {
13+
module: "amd",
14+
composite: true,
15+
persistResolutions: true,
16+
traceResolution: true,
17+
outFile
18+
},
19+
include: ["src/**/*.ts"]
20+
}),
21+
}),
22+
commandLineArgs: [input, "src/project"],
23+
incrementalScenarios: [
24+
noChangeRun,
25+
{
26+
subScenario: "Modify main file",
27+
buildKind: BuildKind.IncrementalDtsChange,
28+
modifyFs: fs => appendText(fs, `/src/project/src/main.ts`, `something();`),
29+
},
30+
{
31+
subScenario: "Add new module and update main file",
32+
buildKind: BuildKind.IncrementalDtsChange,
33+
modifyFs: fs => {
34+
fs.writeFileSync(`/src/project/src/newFile.ts`, "export function foo() { return 20; }");
35+
prependText(fs, `/src/project/src/main.ts`, `import { foo } from "./newFile";`);
36+
},
37+
},
38+
{
39+
subScenario: "Write file that could not be resolved",
40+
buildKind: BuildKind.IncrementalDtsChange,
41+
modifyFs: fs => fs.writeFileSync(`/src/project/src/fileNotFound.ts`, "export function something2() { return 20; }"),
42+
},
43+
{
44+
subScenario: "Clean resolutions",
45+
buildKind: BuildKind.IncrementalDtsChange,
46+
modifyFs: noop,
47+
commandLineArgs: [input, "src/project", "--cleanResolutions"]
48+
},
49+
noChangeRun,
50+
{
51+
subScenario: "Modify main file",
52+
buildKind: BuildKind.IncrementalDtsChange,
53+
modifyFs: fs => appendText(fs, `/src/project/src/main.ts`, `something();`),
54+
},
55+
],
56+
baselinePrograms: true,
57+
});
58+
}
59+
60+
describe("unittests:: tsbuild:: persistResolutions::", () => {
61+
verifyTscPersistsResolutions("--b");
62+
verifyTscPersistsResolutions("--b", "outFile.js");
63+
});
64+
}

src/testRunner/unittests/tsbuild/sample.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,35 @@ class someClass2 { }`),
503503
modifyFs: fs => replaceText(fs, "/src/tests/tsconfig.json", `"esModuleInterop": false`, `"esModuleInterop": true`),
504504
}],
505505
});
506+
507+
verifyTscSerializedIncrementalEdits({
508+
scenario: "sample1",
509+
subScenario: "persistResolutions",
510+
baselinePrograms: true,
511+
fs: () => projFs,
512+
modifyFs: fs => fs.writeFileSync("/src/core/tsconfig.json", JSON.stringify({
513+
compilerOptions: {
514+
composite: true,
515+
skipDefaultLibCheck: true,
516+
persistResolutions: true,
517+
}
518+
})),
519+
commandLineArgs: ["--b", "/src/tests"],
520+
incrementalScenarios: [
521+
...coreChanges,
522+
{
523+
subScenario: "Clean resolutions",
524+
buildKind: BuildKind.IncrementalDtsChange,
525+
modifyFs: noop,
526+
commandLineArgs: ["--b", "/src/tests", "--cleanResolutions"],
527+
},
528+
{
529+
subScenario: "Modify core",
530+
buildKind: BuildKind.IncrementalDtsChange,
531+
modifyFs: fs => replaceText(fs, "/src/core/index.ts", "export class someClass {", "export class someClassNew {"),
532+
}
533+
]
534+
});
506535
});
507536
});
508537
}

src/testRunner/unittests/tsbuild/watchMode.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,4 +1451,94 @@ const a: string = "hello";`),
14511451
]
14521452
});
14531453
});
1454+
1455+
function getPersistentResolutionsFiles(outFile?: string) {
1456+
return createWatchedSystem([
1457+
{
1458+
path: `${projectRoot}/src/main.ts`,
1459+
content: Utils.dedent`
1460+
import { something } from "./filePresent";
1461+
import { something2 } from "./fileNotFound";`,
1462+
},
1463+
{
1464+
path: `${projectRoot}/src/filePresent.ts`,
1465+
content: `export function something() { return 10; }`,
1466+
},
1467+
{
1468+
path: `${projectRoot}/tsconfig.json`,
1469+
content: JSON.stringify({
1470+
compilerOptions: {
1471+
module: "amd",
1472+
composite: true,
1473+
persistResolutions: true,
1474+
traceResolution: true,
1475+
outFile
1476+
},
1477+
include: ["src/**/*.ts"]
1478+
}),
1479+
},
1480+
libFile
1481+
], { currentDirectory: projectRoot });
1482+
}
1483+
1484+
function getPersistentResoluitionsFilesWith(modify: (sys: WatchedSystem) => void, outFile?: string) {
1485+
const sys = getPersistentResolutionsFiles(outFile);
1486+
const exit = sys.exit;
1487+
const readFile = sys.readFile;
1488+
const writeFile = sys.writeFile;
1489+
fakes.patchHostForBuildInfoReadWrite(sys);
1490+
sys.exit = noop;
1491+
modify(sys);
1492+
sys.exit = exit;
1493+
sys.readFile = readFile;
1494+
sys.writeFile = writeFile;
1495+
sys.clearOutput();
1496+
return sys;
1497+
}
1498+
1499+
function verifyTscWatchPersistentResolutionsWorker(subScenario: string, input: "--p" | "--b", sys: () => WatchedSystem) {
1500+
verifyTscWatch({
1501+
scenario: "persistResolutions",
1502+
subScenario,
1503+
sys,
1504+
commandLineArgs: [input, ".", "-w", "--extendedDiagnostics"],
1505+
changes: [
1506+
{
1507+
caption: "Modify main file",
1508+
change: sys => sys.appendFile(`${projectRoot}/src/main.ts`, `something();`),
1509+
timeouts: runQueuedTimeoutCallbacks,
1510+
},
1511+
{
1512+
caption: "Add new module and update main file",
1513+
change: sys => {
1514+
sys.writeFile(`${projectRoot}/src/newFile.ts`, "export function foo() { return 20; }");
1515+
sys.prependFile(`${projectRoot}/src/main.ts`, `import { foo } from "./newFile";`);
1516+
},
1517+
timeouts: runQueuedTimeoutCallbacks,
1518+
},
1519+
{
1520+
caption: "Write file that could not be resolved",
1521+
change: sys => sys.writeFile(`${projectRoot}/src/fileNotFound.ts`, "export function something2() { return 20; }"),
1522+
timeouts: sys => {
1523+
sys.runQueuedTimeoutCallbacks(); // Invalidate resolutions
1524+
sys.runQueuedTimeoutCallbacks(); // Actual update
1525+
}
1526+
}
1527+
]
1528+
});
1529+
}
1530+
1531+
export function verifyTscWatchPersistentResolutions(input: "--p" | "--b", outFile?: string) {
1532+
verifyTscWatchPersistentResolutionsWorker(`saves resolution and uses it for new program${outFile ? " with outFile" : ""}`, input, () => getPersistentResolutionsFiles(outFile));
1533+
verifyTscWatchPersistentResolutionsWorker(`can build after resolutions have been saved in tsbuildinfo file${outFile ? " with outFile" : ""}`, input, () => getPersistentResoluitionsFilesWith(sys => executeCommandLine(sys, noop, [input, "."]), outFile));
1534+
verifyTscWatchPersistentResolutionsWorker(`can build after resolutions are cleaned${outFile ? " with outFile" : ""}`, input, () => getPersistentResoluitionsFilesWith(sys => {
1535+
executeCommandLine(sys, noop, [input, "."]);
1536+
executeCommandLine(sys, noop, [input, ".", "--cleanResolutions"]);
1537+
}, outFile));
1538+
}
1539+
1540+
describe("unittests:: tsbuild:: watchMode:: persistentResolutions", () => {
1541+
verifyTscWatchPersistentResolutions("--b");
1542+
verifyTscWatchPersistentResolutions("--b", "outFile.js");
1543+
});
14541544
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ts {
2+
describe("unittests:: tsc:: persistResolutions::", () => {
3+
verifyTscPersistsResolutions("--p");
4+
verifyTscPersistsResolutions("--p", "outFile.js");
5+
});
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ts.tscWatch {
2+
describe("unittests:: tsc-watch:: persistResolutions", () => {
3+
verifyTscWatchPersistentResolutions("--p");
4+
verifyTscWatchPersistentResolutions("--p", "outFile.js");
5+
});
6+
}

0 commit comments

Comments
 (0)