From 7457e5d9fe0636ffd55f84ef1204c546ba5c93b5 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 26 Mar 2019 14:09:51 -0700 Subject: [PATCH] Pull out the incremental compilation into a function so we can test it --- src/compiler/watch.ts | 8 ++-- src/testRunner/unittests/tscWatch/helpers.ts | 42 ++++++++++++++----- .../unittests/tscWatch/incremental.ts | 24 +++++++++-- src/tsc/tsc.ts | 5 +-- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index d4147b6873e..9b08c54222f 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -392,7 +392,7 @@ namespace ts { return host; } - export interface IncrementalProgramOptions { + interface IncrementalProgramOptions { rootNames: ReadonlyArray; options: CompilerOptions; configFileParsingDiagnostics?: ReadonlyArray; @@ -400,7 +400,7 @@ namespace ts { host?: CompilerHost; createProgram?: CreateProgram; } - export function createIncrementalProgram({ + function createIncrementalProgram({ rootNames, options, configFileParsingDiagnostics, projectReferences, host, createProgram }: IncrementalProgramOptions): T { host = host || createIncrementalCompilerHost(options); @@ -427,11 +427,11 @@ namespace ts { const exitStatus = emitFilesAndReportErrors( builderProgram, input.reportDiagnostic || createDiagnosticReporter(system), - s => host!.trace && host!.trace!(s), + s => host.trace && host.trace(s), input.reportErrorSummary || input.options.pretty ? errorCount => system.write(getErrorSummaryText(errorCount, system.newLine)) : undefined ); if (input.afterProgramEmitAndDiagnostics) input.afterProgramEmitAndDiagnostics(builderProgram); - return exitStatus; + return exitStatus; } } diff --git a/src/testRunner/unittests/tscWatch/helpers.ts b/src/testRunner/unittests/tscWatch/helpers.ts index 98a2d4ffa4b..f8647393cb1 100644 --- a/src/testRunner/unittests/tscWatch/helpers.ts +++ b/src/testRunner/unittests/tscWatch/helpers.ts @@ -57,20 +57,23 @@ namespace ts.tscWatch { function checkOutputErrors( host: WatchedSystem, logsBeforeWatchDiagnostic: string[] | undefined, - preErrorsWatchDiagnostic: Diagnostic, + preErrorsWatchDiagnostic: Diagnostic | undefined, logsBeforeErrors: string[] | undefined, errors: ReadonlyArray | ReadonlyArray, disableConsoleClears?: boolean | undefined, - ...postErrorsWatchDiagnostics: Diagnostic[] + ...postErrorsWatchDiagnostics: Diagnostic[] | string[] ) { let screenClears = 0; const outputs = host.getOutput(); - const expectedOutputCount = 1 + errors.length + postErrorsWatchDiagnostics.length + - (logsBeforeWatchDiagnostic ? logsBeforeWatchDiagnostic.length : 0) + (logsBeforeErrors ? logsBeforeErrors.length : 0); + 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)); - assertWatchDiagnostic(preErrorsWatchDiagnostic); + if (preErrorsWatchDiagnostic) assertWatchDiagnostic(preErrorsWatchDiagnostic); forEach(logsBeforeErrors, log => assertLog("logBeforeError", log)); // Verify errors forEach(errors, assertDiagnostic); @@ -98,13 +101,18 @@ namespace ts.tscWatch { index++; } - function assertWatchDiagnostic(diagnostic: Diagnostic) { - const expected = getWatchDiagnosticWithoutDate(diagnostic); - if (!disableConsoleClears && contains(screenStartingMessageCodes, diagnostic.code)) { - assert.equal(host.screenClears[screenClears], index, `Expected screen clear at this diagnostic: ${expected}`); - screenClears++; + function assertWatchDiagnostic(diagnostic: Diagnostic | string) { + if (isString(diagnostic)) { + assert.equal(outputs[index], diagnostic, getOutputAtFailedMessage("Diagnostic", diagnostic)); + } + else { + const expected = getWatchDiagnosticWithoutDate(diagnostic); + if (!disableConsoleClears && contains(screenStartingMessageCodes, diagnostic.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)); } - assert.isTrue(endsWith(outputs[index], expected), getOutputAtFailedMessage("Watch diagnostic", expected)); index++; } @@ -159,6 +167,18 @@ namespace ts.tscWatch { assert.equal(host.exitCode, expectedExitCode); } + export function checkNormalBuildErrors(host: WatchedSystem, errors: ReadonlyArray | ReadonlyArray, reportErrorSummary?: boolean) { + checkOutputErrors( + host, + /*logsBeforeWatchDiagnostic*/ undefined, + /*preErrorsWatchDiagnostic*/ undefined, + /*logsBeforeErrors*/ undefined, + errors, + /*disableConsoleClears*/ undefined, + ...(reportErrorSummary ? [getErrorSummaryText(errors.length, host.newLine)] : emptyArray) + ); + } + function isDiagnosticMessageChain(message: DiagnosticMessage | DiagnosticMessageChain): message is DiagnosticMessageChain { return !!(message as DiagnosticMessageChain).messageText; } diff --git a/src/testRunner/unittests/tscWatch/incremental.ts b/src/testRunner/unittests/tscWatch/incremental.ts index c384f74ddce..35ec5bff555 100644 --- a/src/testRunner/unittests/tscWatch/incremental.ts +++ b/src/testRunner/unittests/tscWatch/incremental.ts @@ -26,12 +26,28 @@ namespace ts.tscWatch { it("with tsc", () => { verifyIncrementalWatchEmitWorker({ input, - emitAndReportErrors: , - verifyErrors + emitAndReportErrors: incrementalBuild, + verifyErrors: checkNormalBuildErrors }); }); } + function incrementalBuild(configFile: string, host: WatchedSystem) { + const reportDiagnostic = createDiagnosticReporter(host); + const config = parseConfigFileWithSystem(configFile, {}, host, reportDiagnostic); + if (config) { + performIncrementalCompilation({ + rootNames: config.fileNames, + options: config.options, + projectReferences: config.projectReferences, + configFileParsingDiagnostics: getConfigFileParsingDiagnostics(config), + reportDiagnostic, + system: host + }); + } + return { close: noop }; + } + interface VerifyIncrementalWatchEmitWorkerInput { input: VerifyIncrementalWatchEmitInput; emitAndReportErrors: (configFile: string, host: WatchedSystem) => { close(): void; }; @@ -83,10 +99,10 @@ namespace ts.tscWatch { } function verifyBuild({ host, writtenFiles, emitAndReportErrors, verifyErrors, expectedEmit, expectedErrors }: VerifyBuildWorker) { writtenFiles.clear(); - const watch = emitAndReportErrors("tsconfig.json", host); + const result = emitAndReportErrors("tsconfig.json", host); checkFileEmit(writtenFiles, expectedEmit); verifyErrors(host, expectedErrors); - watch.close(); + result.close(); } function checkFileEmit(actual: Map, expected: ReadonlyArray) { diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index 6a81cb09ab7..ccc04d38088 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -261,7 +261,7 @@ namespace ts { function performIncrementalCompilation(config: ParsedCommandLine) { const { options, fileNames, projectReferences } = config; enableStatistics(options); - const exitStatus = ts.performIncrementalCompilation({ + return sys.exit(ts.performIncrementalCompilation({ rootNames: fileNames, options, configFileParsingDiagnostics: getConfigFileParsingDiagnostics(config), @@ -269,8 +269,7 @@ namespace ts { reportDiagnostic, reportErrorSummary: createReportErrorSummary(options), afterProgramEmitAndDiagnostics: builderProgram => reportStatistics(builderProgram.getProgram()) - }); - return sys.exit(exitStatus); + })); } function updateCreateProgram(host: { createProgram: CreateProgram; }) {