Skip to content

Commit bfbc388

Browse files
committed
Input and output in watch mode
1 parent 25d7928 commit bfbc388

File tree

208 files changed

+8479
-7357
lines changed

Some content is hidden

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

208 files changed

+8479
-7357
lines changed

src/testRunner/unittests/tsbuild/watchMode.ts

Lines changed: 182 additions & 169 deletions
Large diffs are not rendered by default.

src/testRunner/unittests/tscWatch/consoleClearing.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@ namespace ts.tscWatch {
66
content: ""
77
};
88

9-
function makeChangeToFile(sys: WatchedSystem) {
10-
sys.modifyFile(file.path, "//");
11-
sys.runQueuedTimeoutCallbacks();
12-
return "Comment added to file f";
13-
}
9+
const makeChangeToFile: TscWatchCompileChange[] = [{
10+
caption: "Comment added to file f",
11+
change: sys => sys.modifyFile(file.path, "//"),
12+
timeouts: runQueuedTimeoutCallbacks,
13+
}];
1414

1515
function checkConsoleClearingUsingCommandLineOptions(subScenario: string, commandLineOptions?: string[]) {
1616
verifyTscWatch({
1717
scenario,
1818
subScenario,
1919
commandLineArgs: ["--w", file.path, ...commandLineOptions || emptyArray],
2020
sys: () => createWatchedSystem([file, libFile]),
21-
changes: [
22-
makeChangeToFile
23-
],
21+
changes: makeChangeToFile,
2422
});
2523
}
2624

@@ -39,20 +37,16 @@ namespace ts.tscWatch {
3937
};
4038
const files = [file, configFile, libFile];
4139
it("using createWatchOfConfigFile ", () => {
42-
const sys = TestFSWithWatch.changeToHostTrackingWrittenFiles(
43-
createWatchedSystem(files)
44-
);
45-
const watch = createWatchOfConfigFile(configFile.path, sys);
40+
const baseline = createBaseline(createWatchedSystem(files));
41+
const watch = createWatchOfConfigFile(configFile.path, baseline.sys);
4642
// Initially console is cleared if --preserveOutput is not provided since the config file is yet to be parsed
4743
runWatchBaseline({
4844
scenario,
4945
subScenario: "when preserveWatchOutput is true in config file/createWatchOfConfigFile",
5046
commandLineArgs: ["--w", "-p", configFile.path],
51-
sys,
47+
...baseline,
5248
getPrograms: () => [[watch.getCurrentProgram().getProgram(), watch.getCurrentProgram()]],
53-
changes: [
54-
makeChangeToFile
55-
],
49+
changes: makeChangeToFile,
5650
watchOrSolution: watch
5751
});
5852
});
@@ -61,9 +55,7 @@ namespace ts.tscWatch {
6155
subScenario: "when preserveWatchOutput is true in config file/when createWatchProgram is invoked with configFileParseResult on WatchCompilerHostOfConfigFile",
6256
commandLineArgs: ["--w", "-p", configFile.path],
6357
sys: () => createWatchedSystem(files),
64-
changes: [
65-
makeChangeToFile
66-
],
58+
changes: makeChangeToFile,
6759
});
6860
});
6961
});

src/testRunner/unittests/tscWatch/emit.ts

Lines changed: 102 additions & 94 deletions
Large diffs are not rendered by default.

src/testRunner/unittests/tscWatch/emitAndErrorUpdates.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ console.log(b.c.d);`
9797
subScenario: `deepImportChanges/${subScenario}`,
9898
files: () => [aFile, bFile, cFile],
9999
changes: [
100-
sys => {
101-
sys.writeFile(cFile.path, cFile.content.replace("d", "d2"));
102-
sys.runQueuedTimeoutCallbacks();
103-
return "Rename property d to d2 of class C";
100+
{
101+
caption: "Rename property d to d2 of class C",
102+
change: sys => sys.writeFile(cFile.path, cFile.content.replace("d", "d2")),
103+
timeouts: runQueuedTimeoutCallbacks,
104104
}
105105
],
106106
});
@@ -197,10 +197,10 @@ getPoint().c.x;`
197197
subScenario: "file not exporting a deep multilevel import that changes",
198198
files: () => [aFile, bFile, cFile, dFile, eFile],
199199
changes: [
200-
sys => {
201-
sys.writeFile(aFile.path, aFile.content.replace("x2", "x"));
202-
sys.runQueuedTimeoutCallbacks();
203-
return "Rename property x2 to x of interface Coords";
200+
{
201+
caption: "Rename property x2 to x of interface Coords",
202+
change: sys => sys.writeFile(aFile.path, aFile.content.replace("x2", "x")),
203+
timeouts: runQueuedTimeoutCallbacks,
204204
}
205205
]
206206
});
@@ -260,10 +260,10 @@ export class Data {
260260
files: () => [lib1ToolsInterface, lib1ToolsPublic, app, lib2Public, lib1Public, ...files],
261261
configFile: () => config,
262262
changes: [
263-
sys => {
264-
sys.writeFile(lib1ToolsInterface.path, lib1ToolsInterface.content.replace("title", "title2"));
265-
sys.runQueuedTimeoutCallbacks();
266-
return "Rename property title to title2 of interface ITest";
263+
{
264+
caption: "Rename property title to title2 of interface ITest",
265+
change: sys => sys.writeFile(lib1ToolsInterface.path, lib1ToolsInterface.content.replace("title", "title2")),
266+
timeouts: runQueuedTimeoutCallbacks,
267267
}
268268
]
269269
});
@@ -303,6 +303,20 @@ export class Data2 {
303303
});
304304

305305
describe("with noEmitOnError", () => {
306+
function change(caption: string, content: string): TscWatchCompileChange {
307+
return {
308+
caption,
309+
change: sys => sys.writeFile(`${TestFSWithWatch.tsbuildProjectsLocation}/noEmitOnError/src/main.ts`, content),
310+
// build project
311+
timeouts: checkSingleTimeoutQueueLengthAndRun
312+
};
313+
}
314+
const noChange: TscWatchCompileChange = {
315+
caption: "No change",
316+
change: sys => sys.writeFile(`${TestFSWithWatch.tsbuildProjectsLocation}/noEmitOnError/src/main.ts`, sys.readFile(`${TestFSWithWatch.tsbuildProjectsLocation}/noEmitOnError/src/main.ts`)!),
317+
// build project
318+
timeouts: checkSingleTimeoutQueueLengthAndRun,
319+
};
306320
verifyEmitAndErrorUpdates({
307321
subScenario: "with noEmitOnError",
308322
currentDirectory: `${TestFSWithWatch.tsbuildProjectsLocation}/noEmitOnError`,
@@ -312,29 +326,19 @@ export class Data2 {
312326
configFile: () => TestFSWithWatch.getTsBuildProjectFile("noEmitOnError", "tsconfig.json"),
313327
changes: [
314328
noChange,
315-
sys => change(sys, "Fix Syntax error", `import { A } from "../shared/types/db";
329+
change("Fix Syntax error", `import { A } from "../shared/types/db";
316330
const a = {
317331
lastName: 'sdsd'
318332
};`),
319-
sys => change(sys, "Semantic Error", `import { A } from "../shared/types/db";
333+
change("Semantic Error", `import { A } from "../shared/types/db";
320334
const a: string = 10;`),
321335
noChange,
322-
sys => change(sys, "Fix Semantic Error", `import { A } from "../shared/types/db";
336+
change("Fix Semantic Error", `import { A } from "../shared/types/db";
323337
const a: string = "hello";`),
324338
noChange,
325339
],
326340
baselineIncremental: true
327341
});
328-
function change(sys: WatchedSystem, caption: string, content: string) {
329-
sys.writeFile(`${TestFSWithWatch.tsbuildProjectsLocation}/noEmitOnError/src/main.ts`, content);
330-
sys.checkTimeoutQueueLengthAndRun(1); // build project
331-
return caption;
332-
}
333-
function noChange(sys: WatchedSystem) {
334-
sys.writeFile(`${TestFSWithWatch.tsbuildProjectsLocation}/noEmitOnError/src/main.ts`, sys.readFile(`${TestFSWithWatch.tsbuildProjectsLocation}/noEmitOnError/src/main.ts`)!);
335-
sys.checkTimeoutQueueLengthAndRun(1); // build project
336-
return "No change";
337-
}
338342
});
339343
});
340344
}

src/testRunner/unittests/tscWatch/forceConsistentCasingInFileNames.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ namespace ts.tscWatch {
2828
verifyConsistentFileNames({
2929
subScenario: "when changing module name with different casing",
3030
changes: [
31-
sys => {
32-
sys.writeFile(anotherFile.path, anotherFile.content.replace("./logger", "./Logger"));
33-
sys.runQueuedTimeoutCallbacks();
34-
return "Change module name from logger to Logger";
31+
{
32+
caption: "Change module name from logger to Logger",
33+
change: sys => sys.writeFile(anotherFile.path, anotherFile.content.replace("./logger", "./Logger")),
34+
timeouts: runQueuedTimeoutCallbacks,
3535
}
3636
]
3737
});
3838

3939
verifyConsistentFileNames({
4040
subScenario: "when renaming file with different casing",
4141
changes: [
42-
sys => {
43-
sys.renameFile(loggerFile.path, `${projectRoot}/Logger.ts`);
44-
sys.runQueuedTimeoutCallbacks();
45-
return "Change name of file from logger to Logger";
42+
{
43+
caption: "Change name of file from logger to Logger",
44+
change: sys => sys.renameFile(loggerFile.path, `${projectRoot}/Logger.ts`),
45+
timeouts: runQueuedTimeoutCallbacks,
4646
}
4747
]
4848
});

src/testRunner/unittests/tscWatch/helpers.ts

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,28 @@ namespace ts.tscWatch {
270270
return getDiagnosticOfFileFromProgram(program, file.path, file.content.indexOf(quotedModuleName), quotedModuleName.length, Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations, moduleName);
271271
}
272272

273-
export type TscWatchCompileChange = (
274-
sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles,
275-
programs: readonly CommandLineProgram[],
276-
watchOrSolution: ReturnType<typeof executeCommandLine>
277-
) => string;
273+
export function runQueuedTimeoutCallbacks(sys: WatchedSystem) {
274+
sys.runQueuedTimeoutCallbacks();
275+
}
276+
277+
export function checkSingleTimeoutQueueLengthAndRun(sys: WatchedSystem) {
278+
sys.checkTimeoutQueueLengthAndRun(1);
279+
}
280+
281+
export function checkSingleTimeoutQueueLengthAndRunAndVerifyNoTimeout(sys: WatchedSystem) {
282+
sys.checkTimeoutQueueLengthAndRun(1);
283+
sys.checkTimeoutQueueLength(0);
284+
}
285+
286+
export interface TscWatchCompileChange {
287+
caption: string;
288+
change: (sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles) => void;
289+
timeouts: (
290+
sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles,
291+
programs: readonly CommandLineProgram[],
292+
watchOrSolution: ReturnType<typeof executeCommandLine>
293+
) => void;
294+
}
278295
export interface TscWatchCheckOptions {
279296
baselineSourceMap?: boolean;
280297
}
@@ -290,10 +307,8 @@ namespace ts.tscWatch {
290307

291308
export type SystemSnap = ReturnType<WatchedSystem["snap"]>;
292309
function tscWatchCompile(input: TscWatchCompile) {
293-
it("Generates files matching the baseline", () => {
294-
const sys = TestFSWithWatch.changeToHostTrackingWrittenFiles(
295-
fakes.patchHostForBuildInfoReadWrite(input.sys())
296-
);
310+
it("tsc-watch:: Generates files matching the baseline", () => {
311+
const { sys, baseline, oldSnap } = createBaseline(input.sys());
297312
const {
298313
scenario, subScenario,
299314
commandLineArgs, changes,
@@ -312,6 +327,8 @@ namespace ts.tscWatch {
312327
subScenario,
313328
commandLineArgs,
314329
sys,
330+
baseline,
331+
oldSnap,
315332
getPrograms,
316333
baselineSourceMap,
317334
changes,
@@ -320,31 +337,54 @@ namespace ts.tscWatch {
320337
});
321338
}
322339

323-
export interface RunWatchBaseline extends TscWatchCompileBase {
340+
export interface Baseline {
341+
baseline: string[];
342+
sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles;
343+
oldSnap: SystemSnap;
344+
}
345+
346+
export function createBaseline(system: WatchedSystem): Baseline {
347+
const sys = TestFSWithWatch.changeToHostTrackingWrittenFiles(
348+
fakes.patchHostForBuildInfoReadWrite(system)
349+
);
350+
const baseline: string[] = [];
351+
baseline.push("Input::");
352+
sys.diff(baseline);
353+
return { sys, baseline, oldSnap: sys.snap() };
354+
}
355+
356+
export function applyChange(sys: Baseline["sys"], baseline: Baseline["baseline"], change: TscWatchCompileChange["change"], caption?: TscWatchCompileChange["caption"]) {
357+
const oldSnap = sys.snap();
358+
baseline.push(`Change::${caption ? " " + caption : ""}`, "");
359+
change(sys);
360+
baseline.push("Input::");
361+
sys.diff(baseline, oldSnap);
362+
return sys.snap();
363+
}
364+
365+
export interface RunWatchBaseline extends Baseline, TscWatchCompileBase {
324366
sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles;
325367
getPrograms: () => readonly CommandLineProgram[];
326368
watchOrSolution: ReturnType<typeof executeCommandLine>;
327369
}
328370
export function runWatchBaseline({
329371
scenario, subScenario, commandLineArgs,
330-
getPrograms, sys,
372+
getPrograms, sys, baseline, oldSnap,
331373
baselineSourceMap,
332374
changes, watchOrSolution
333375
}: RunWatchBaseline) {
334-
const baseline: string[] = [];
335376
baseline.push(`${sys.getExecutingFilePath()} ${commandLineArgs.join(" ")}`);
336377
let programs = watchBaseline({
337378
baseline,
338379
getPrograms,
339380
sys,
340-
oldSnap: undefined,
381+
oldSnap,
341382
baselineSourceMap
342383
});
343384

344-
for (const change of changes) {
345-
const oldSnap = sys.snap();
346-
const caption = change(sys, programs, watchOrSolution);
347-
baseline.push(`Change:: ${caption}`, "");
385+
for (const { caption, change, timeouts } of changes) {
386+
oldSnap = applyChange(sys, baseline, change, caption);
387+
timeouts(sys, programs, watchOrSolution);
348388
programs = watchBaseline({
349389
baseline,
350390
getPrograms,
@@ -366,19 +406,16 @@ namespace ts.tscWatch {
366406
});
367407
}
368408

369-
export interface WatchBaseline extends TscWatchCheckOptions {
370-
baseline: string[];
371-
sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles;
409+
export interface WatchBaseline extends Baseline, TscWatchCheckOptions {
372410
getPrograms: () => readonly CommandLineProgram[];
373-
oldSnap: SystemSnap | undefined;
374411
}
375412
export function watchBaseline({ baseline, getPrograms, sys, oldSnap, baselineSourceMap }: WatchBaseline) {
376413
if (baselineSourceMap) generateSourceMapBaselineFiles(sys);
377-
sys.diff(baseline, oldSnap);
378414
sys.serializeOutput(baseline);
379415
const programs = baselinePrograms(baseline, getPrograms);
380416
sys.serializeWatches(baseline);
381417
baseline.push(`exitCode:: ExitStatus.${ExitStatus[sys.exitCode as ExitStatus]}`, "");
418+
sys.diff(baseline, oldSnap);
382419
sys.writtenFiles.forEach((value, key) => {
383420
assert.equal(value, 1, `Expected to write file ${key} only once`);
384421
});

src/testRunner/unittests/tscWatch/incremental.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,21 @@ namespace ts.tscWatch {
2828
{ subScenario, files, optionsToExtend, modifyFs }: VerifyIncrementalWatchEmitInput,
2929
incremental: boolean
3030
) {
31-
const sys = TestFSWithWatch.changeToHostTrackingWrittenFiles(
32-
fakes.patchHostForBuildInfoReadWrite(createWatchedSystem(files(), { currentDirectory: project }))
33-
);
31+
const { sys, baseline, oldSnap } = createBaseline(createWatchedSystem(files(), { currentDirectory: project }));
3432
if (incremental) sys.exit = exitCode => sys.exitCode = exitCode;
3533
const argsToPass = [incremental ? "-i" : "-w", ...(optionsToExtend || emptyArray)];
36-
const baseline: string[] = [];
3734
baseline.push(`${sys.getExecutingFilePath()} ${argsToPass.join(" ")}`);
3835
const { cb, getPrograms } = commandLineCallbacks(sys);
39-
build(/*oldSnap*/ undefined);
36+
build(oldSnap);
4037

4138
if (modifyFs) {
42-
const oldSnap = sys.snap();
43-
modifyFs(sys);
44-
baseline.push(`Change::`, "");
39+
const oldSnap = applyChange(sys, baseline, modifyFs);
4540
build(oldSnap);
4641
}
4742

4843
Harness.Baseline.runBaseline(`${isBuild(argsToPass) ? "tsbuild/watchMode" : "tscWatch"}/incremental/${subScenario.split(" ").join("-")}-${incremental ? "incremental" : "watch"}.js`, baseline.join("\r\n"));
4944

50-
function build(oldSnap: SystemSnap | undefined) {
45+
function build(oldSnap: SystemSnap) {
5146
const closer = executeCommandLine(
5247
sys,
5348
cb,

0 commit comments

Comments
 (0)