diff --git a/src/harness/unittests/tscWatchMode.ts b/src/harness/unittests/tscWatchMode.ts index 47116cd5b01..b10c73d4a06 100644 --- a/src/harness/unittests/tscWatchMode.ts +++ b/src/harness/unittests/tscWatchMode.ts @@ -72,22 +72,31 @@ namespace ts.tscWatch { checkOutputDoesNotContain(host, expectedNonAffectedFiles); } + const elapsedRegex = /^Elapsed:: [0-9]+ms/; function checkOutputErrors( host: WatchedSystem, + logsBeforeWatchDiagnostic: string[] | undefined, preErrorsWatchDiagnostic: DiagnosticMessage | undefined, + logsBeforeErrors: string[] | undefined, errors: ReadonlyArray, + disableConsoleClears?: boolean | undefined, ...postErrorsWatchDiagnostics: DiagnosticMessage[] ) { + let screenClears = 0; const outputs = host.getOutput(); - const expectedOutputCount = (preErrorsWatchDiagnostic ? 1 : 0) + errors.length + postErrorsWatchDiagnostics.length; - assert.equal(outputs.length, expectedOutputCount); + const expectedOutputCount = (preErrorsWatchDiagnostic ? 1 : 0) + errors.length + postErrorsWatchDiagnostics.length + + (logsBeforeWatchDiagnostic ? logsBeforeWatchDiagnostic.length : 0) + (logsBeforeErrors ? logsBeforeErrors.length : 0); + assert.equal(outputs.length, expectedOutputCount, JSON.stringify(outputs)); let index = 0; + forEach(logsBeforeWatchDiagnostic, log => assertLog("logsBeforeWatchDiagnostic", log)); if (preErrorsWatchDiagnostic) { assertWatchDiagnostic(preErrorsWatchDiagnostic); } + forEach(logsBeforeErrors, log => assertLog("logBeforeError", log)); // Verify errors forEach(errors, assertDiagnostic); forEach(postErrorsWatchDiagnostics, assertWatchDiagnostic); + assert.equal(host.screenClears.length, screenClears, "Expected number of screen clears"); host.clearOutput(); function assertDiagnostic(diagnostic: Diagnostic) { @@ -96,8 +105,18 @@ namespace ts.tscWatch { index++; } + function assertLog(caption: string, expected: string) { + const actual = outputs[index]; + assert.equal(actual.replace(elapsedRegex, ""), expected.replace(elapsedRegex, ""), getOutputAtFailedMessage(caption, expected)); + index++; + } + function assertWatchDiagnostic(diagnosticMessage: DiagnosticMessage) { const expected = getWatchDiagnosticWithoutDate(diagnosticMessage); + if (!disableConsoleClears && diagnosticMessage.code !== Diagnostics.Compilation_complete_Watching_for_file_changes.code) { + assert.equal(host.screenClears[screenClears], index, `Expected screen clear at this diagnostic: ${expected}`); + screenClears++; + } assert.isTrue(endsWith(outputs[index], expected), getOutputAtFailedMessage("Watch diagnostic", expected)); index++; } @@ -111,20 +130,20 @@ namespace ts.tscWatch { } } - function checkOutputErrorsInitial(host: WatchedSystem, errors: ReadonlyArray) { - checkOutputErrors(host, Diagnostics.Starting_compilation_in_watch_mode, errors, Diagnostics.Compilation_complete_Watching_for_file_changes); + function checkOutputErrorsInitial(host: WatchedSystem, errors: ReadonlyArray, disableConsoleClears?: boolean, logsBeforeWatchDiagnostic?: string[], logsBeforeErrors?: string[]) { + checkOutputErrors(host, logsBeforeWatchDiagnostic, Diagnostics.Starting_compilation_in_watch_mode, logsBeforeErrors, errors, disableConsoleClears, Diagnostics.Compilation_complete_Watching_for_file_changes); } function checkOutputErrorsInitialWithConfigErrors(host: WatchedSystem, errors: ReadonlyArray) { - checkOutputErrors(host, /*preErrorsWatchDiagnostic*/ undefined, errors, Diagnostics.Starting_compilation_in_watch_mode, Diagnostics.Compilation_complete_Watching_for_file_changes); + checkOutputErrors(host, /*logsBeforeWatchDiagnostic*/ undefined, /*preErrorsWatchDiagnostic*/ undefined, /*logsBeforeErrors*/ undefined, errors, /*disableConsoleClears*/ undefined, Diagnostics.Starting_compilation_in_watch_mode, Diagnostics.Compilation_complete_Watching_for_file_changes); } - function checkOutputErrorsIncremental(host: WatchedSystem, errors: ReadonlyArray) { - checkOutputErrors(host, Diagnostics.File_change_detected_Starting_incremental_compilation, errors, Diagnostics.Compilation_complete_Watching_for_file_changes); + function checkOutputErrorsIncremental(host: WatchedSystem, errors: ReadonlyArray, disableConsoleClears?: boolean, logsBeforeWatchDiagnostic?: string[], logsBeforeErrors?: string[]) { + checkOutputErrors(host, logsBeforeWatchDiagnostic, Diagnostics.File_change_detected_Starting_incremental_compilation, logsBeforeErrors, errors, disableConsoleClears, Diagnostics.Compilation_complete_Watching_for_file_changes); } function checkOutputErrorsIncrementalWithExit(host: WatchedSystem, errors: ReadonlyArray, expectedExitCode: ExitStatus) { - checkOutputErrors(host, Diagnostics.File_change_detected_Starting_incremental_compilation, errors); + checkOutputErrors(host, /*logsBeforeWatchDiagnostic*/ undefined, Diagnostics.File_change_detected_Starting_incremental_compilation, /*logsBeforeErrors*/ undefined, errors, /*disableConsoleClears*/ undefined); assert.equal(host.exitCode, expectedExitCode); } @@ -2161,30 +2180,33 @@ declare module "fs" { path: "f.ts", content: "" }; - const files = [file]; + const files = [file, libFile]; + const disableConsoleClear = options.diagnostics || options.extendedDiagnostics || options.preserveWatchOutput; const host = createWatchedSystem(files); - let clearCount: number | undefined; - checkConsoleClears(); - createWatchOfFilesAndCompilerOptions([file.path], host, options); - checkConsoleClears(); + checkOutputErrorsInitial(host, emptyArray, disableConsoleClear, options.extendedDiagnostics && [ + "Current directory: / CaseSensitiveFileNames: false\n" + ], options.extendedDiagnostics && [ + "Synchronizing program\n", + "CreatingProgramWith::\n", + " roots: [\"f.ts\"]\n", + " options: {\"extendedDiagnostics\":true}\n", + "FileWatcher:: Added:: WatchInfo: f.ts 250 \n", + "FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 \n" + ]); file.content = "//"; host.reloadFS(files); host.runQueuedTimeoutCallbacks(); - - checkConsoleClears(); - - function checkConsoleClears() { - if (clearCount === undefined || options.preserveWatchOutput) { - clearCount = 0; - } - else if (!options.diagnostics && !options.extendedDiagnostics) { - clearCount++; - } - host.checkScreenClears(clearCount); - return clearCount; - } + checkOutputErrorsIncremental(host, emptyArray, disableConsoleClear, options.extendedDiagnostics && [ + "FileWatcher:: Triggered with /f.ts1:: WatchInfo: f.ts 250 \n", + "Elapsed:: 0ms FileWatcher:: Triggered with /f.ts1:: WatchInfo: f.ts 250 \n" + ], options.extendedDiagnostics && [ + "Synchronizing program\n", + "CreatingProgramWith::\n", + " roots: [\"f.ts\"]\n", + " options: {\"extendedDiagnostics\":true}\n" + ]); } it("without --diagnostics or --extendedDiagnostics", () => { diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index 8b62c210abb..e74eed39afe 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -288,7 +288,7 @@ interface Array {}` private toPath: (f: string) => Path; private timeoutCallbacks = new Callbacks(); private immediateCallbacks = new Callbacks(); - private screenClears = 0; + readonly screenClears: number[] = []; readonly watchedDirectories = createMultiMap(); readonly watchedDirectoriesRecursive = createMultiMap(); @@ -762,7 +762,7 @@ interface Array {}` } clearScreen(): void { - this.screenClears += 1; + this.screenClears.push(this.output.length); } checkTimeoutQueueLengthAndRun(expected: number) { @@ -802,10 +802,6 @@ interface Array {}` this.immediateCallbacks.unregister(timeoutId); } - checkScreenClears(expected: number): void { - assert.equal(this.screenClears, expected); - } - createDirectory(directoryName: string): void { const folder = this.toFolder(directoryName); @@ -839,6 +835,7 @@ interface Array {}` clearOutput() { clear(this.output); + this.screenClears.length = 0; } realpath(s: string): string {