Pull out the incremental compilation into a function so we can test it

This commit is contained in:
Sheetal Nandi 2019-03-26 14:09:51 -07:00
parent 021444a248
commit 7457e5d9fe
4 changed files with 57 additions and 22 deletions

View file

@ -392,7 +392,7 @@ namespace ts {
return host;
}
export interface IncrementalProgramOptions<T extends BuilderProgram> {
interface IncrementalProgramOptions<T extends BuilderProgram> {
rootNames: ReadonlyArray<string>;
options: CompilerOptions;
configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>;
@ -400,7 +400,7 @@ namespace ts {
host?: CompilerHost;
createProgram?: CreateProgram<T>;
}
export function createIncrementalProgram<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({
function createIncrementalProgram<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({
rootNames, options, configFileParsingDiagnostics, projectReferences, host, createProgram
}: IncrementalProgramOptions<T>): 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;
}
}

View file

@ -57,20 +57,23 @@ namespace ts.tscWatch {
function checkOutputErrors(
host: WatchedSystem,
logsBeforeWatchDiagnostic: string[] | undefined,
preErrorsWatchDiagnostic: Diagnostic,
preErrorsWatchDiagnostic: Diagnostic | undefined,
logsBeforeErrors: string[] | undefined,
errors: ReadonlyArray<Diagnostic> | ReadonlyArray<string>,
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<Diagnostic> | ReadonlyArray<string>, 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;
}

View file

@ -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<string>, expected: ReadonlyArray<File>) {

View file

@ -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<T extends BuilderProgram>(host: { createProgram: CreateProgram<T>; }) {