mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-11 19:27:35 -06:00
Refactor tsc-watch tests
This commit is contained in:
parent
f117184562
commit
efc71602cd
@ -60,7 +60,6 @@
|
||||
"unittests/transform.ts",
|
||||
"unittests/tsbuild.ts",
|
||||
"unittests/tsbuildWatchMode.ts",
|
||||
"unittests/tscWatchMode.ts",
|
||||
"unittests/config/commandLineParsing.ts",
|
||||
"unittests/config/configurationExtension.ts",
|
||||
"unittests/config/convertCompilerOptionsFromJson.ts",
|
||||
@ -88,7 +87,9 @@
|
||||
"unittests/services/preProcessFile.ts",
|
||||
"unittests/services/textChanges.ts",
|
||||
"unittests/services/transpile.ts",
|
||||
"unittests/tscWatch/consoleClearing.ts",
|
||||
"unittests/tscWatch/emit.ts",
|
||||
"unittests/tscWatch/programUpdates.ts",
|
||||
"unittests/tscWatch/resolutionCache.ts",
|
||||
"unittests/tscWatch/watchEnvironment.ts",
|
||||
"unittests/tscWatch/watchApi.ts",
|
||||
|
||||
97
src/testRunner/unittests/tscWatch/consoleClearing.ts
Normal file
97
src/testRunner/unittests/tscWatch/consoleClearing.ts
Normal file
@ -0,0 +1,97 @@
|
||||
namespace ts.tscWatch {
|
||||
describe("unittests:: tsc-watch:: console clearing", () => {
|
||||
const currentDirectoryLog = "Current directory: / CaseSensitiveFileNames: false\n";
|
||||
const fileWatcherAddedLog = [
|
||||
"FileWatcher:: Added:: WatchInfo: /f.ts 250 Source file\n",
|
||||
"FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 Source file\n"
|
||||
];
|
||||
|
||||
const file: File = {
|
||||
path: "/f.ts",
|
||||
content: ""
|
||||
};
|
||||
|
||||
function getProgramSynchronizingLog(options: CompilerOptions) {
|
||||
return [
|
||||
"Synchronizing program\n",
|
||||
"CreatingProgramWith::\n",
|
||||
" roots: [\"/f.ts\"]\n",
|
||||
` options: ${JSON.stringify(options)}\n`
|
||||
];
|
||||
}
|
||||
|
||||
function isConsoleClearDisabled(options: CompilerOptions) {
|
||||
return options.diagnostics || options.extendedDiagnostics || options.preserveWatchOutput;
|
||||
}
|
||||
|
||||
function verifyCompilation(host: WatchedSystem, options: CompilerOptions, initialDisableOptions?: CompilerOptions) {
|
||||
const disableConsoleClear = isConsoleClearDisabled(options);
|
||||
const hasLog = options.extendedDiagnostics || options.diagnostics;
|
||||
checkOutputErrorsInitial(host, emptyArray, initialDisableOptions ? isConsoleClearDisabled(initialDisableOptions) : disableConsoleClear, hasLog ? [
|
||||
currentDirectoryLog,
|
||||
...getProgramSynchronizingLog(options),
|
||||
...(options.extendedDiagnostics ? fileWatcherAddedLog : emptyArray)
|
||||
] : undefined);
|
||||
host.modifyFile(file.path, "//");
|
||||
host.runQueuedTimeoutCallbacks();
|
||||
checkOutputErrorsIncremental(host, emptyArray, disableConsoleClear, hasLog ? [
|
||||
"FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n",
|
||||
"Scheduling update\n",
|
||||
"Elapsed:: 0ms FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n"
|
||||
] : undefined, hasLog ? getProgramSynchronizingLog(options) : undefined);
|
||||
}
|
||||
|
||||
function checkConsoleClearingUsingCommandLineOptions(options: CompilerOptions = {}) {
|
||||
const files = [file, libFile];
|
||||
const host = createWatchedSystem(files);
|
||||
createWatchOfFilesAndCompilerOptions([file.path], host, options);
|
||||
verifyCompilation(host, options);
|
||||
}
|
||||
|
||||
it("without --diagnostics or --extendedDiagnostics", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions();
|
||||
});
|
||||
it("with --diagnostics", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions({
|
||||
diagnostics: true,
|
||||
});
|
||||
});
|
||||
it("with --extendedDiagnostics", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions({
|
||||
extendedDiagnostics: true,
|
||||
});
|
||||
});
|
||||
it("with --preserveWatchOutput", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions({
|
||||
preserveWatchOutput: true,
|
||||
});
|
||||
});
|
||||
|
||||
describe("when preserveWatchOutput is true in config file", () => {
|
||||
const compilerOptions: CompilerOptions = {
|
||||
preserveWatchOutput: true
|
||||
};
|
||||
const configFile: File = {
|
||||
path: "/tsconfig.json",
|
||||
content: JSON.stringify({ compilerOptions })
|
||||
};
|
||||
const files = [file, configFile, libFile];
|
||||
it("using createWatchOfConfigFile ", () => {
|
||||
const host = createWatchedSystem(files);
|
||||
createWatchOfConfigFile(configFile.path, host);
|
||||
// Initially console is cleared if --preserveOutput is not provided since the config file is yet to be parsed
|
||||
verifyCompilation(host, compilerOptions, {});
|
||||
});
|
||||
it("when createWatchProgram is invoked with configFileParseResult on WatchCompilerHostOfConfigFile", () => {
|
||||
const host = createWatchedSystem(files);
|
||||
const reportDiagnostic = createDiagnosticReporter(host);
|
||||
const optionsToExtend: CompilerOptions = {};
|
||||
const configParseResult = parseConfigFileWithSystem(configFile.path, optionsToExtend, host, reportDiagnostic)!;
|
||||
const watchCompilerHost = createWatchCompilerHostOfConfigFile(configParseResult.options.configFilePath!, optionsToExtend, host, /*createProgram*/ undefined, reportDiagnostic, createWatchStatusReporter(host));
|
||||
watchCompilerHost.configFileParsingResult = configParseResult;
|
||||
createWatchProgram(watchCompilerHost);
|
||||
verifyCompilation(host, compilerOptions);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -164,6 +164,26 @@ namespace ts.tscWatch {
|
||||
};
|
||||
}
|
||||
|
||||
export function getDiagnosticWithoutFile(message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
|
||||
let text = getLocaleSpecificMessage(message);
|
||||
|
||||
if (arguments.length > 1) {
|
||||
text = formatStringFromArgs(text, arguments, 1);
|
||||
}
|
||||
|
||||
return getDiagnosticOfFileFrom(/*file*/ undefined, text, /*start*/ undefined, /*length*/ undefined, message);
|
||||
}
|
||||
|
||||
export function getDiagnosticOfFile(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
|
||||
let text = getLocaleSpecificMessage(message);
|
||||
|
||||
if (arguments.length > 4) {
|
||||
text = formatStringFromArgs(text, arguments, 4);
|
||||
}
|
||||
|
||||
return getDiagnosticOfFileFrom(file, text, start, length, message);
|
||||
}
|
||||
|
||||
export function getDiagnosticOfFileFromProgram(program: Program, filePath: string, start: number, length: number, message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
|
||||
let text = getLocaleSpecificMessage(message);
|
||||
|
||||
@ -175,6 +195,11 @@ namespace ts.tscWatch {
|
||||
text, start, length, message);
|
||||
}
|
||||
|
||||
export function getUnknownCompilerOption(program: Program, configFile: File, option: string) {
|
||||
const quotedOption = `"${option}"`;
|
||||
return getDiagnosticOfFile(program.getCompilerOptions().configFile!, configFile.content.indexOf(quotedOption), quotedOption.length, Diagnostics.Unknown_compiler_option_0, option);
|
||||
}
|
||||
|
||||
export function getDiagnosticModuleNotFoundOfFile(program: Program, file: File, moduleName: string) {
|
||||
const quotedModuleName = `"${moduleName}"`;
|
||||
return getDiagnosticOfFileFromProgram(program, file.path, file.content.indexOf(quotedModuleName), quotedModuleName.length, Diagnostics.Cannot_find_module_0, moduleName);
|
||||
|
||||
@ -1,30 +1,5 @@
|
||||
namespace ts.tscWatch {
|
||||
function getDiagnosticWithoutFile(message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
|
||||
let text = getLocaleSpecificMessage(message);
|
||||
|
||||
if (arguments.length > 1) {
|
||||
text = formatStringFromArgs(text, arguments, 1);
|
||||
}
|
||||
|
||||
return getDiagnosticOfFileFrom(/*file*/ undefined, text, /*start*/ undefined, /*length*/ undefined, message);
|
||||
}
|
||||
|
||||
function getDiagnosticOfFile(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
|
||||
let text = getLocaleSpecificMessage(message);
|
||||
|
||||
if (arguments.length > 4) {
|
||||
text = formatStringFromArgs(text, arguments, 4);
|
||||
}
|
||||
|
||||
return getDiagnosticOfFileFrom(file, text, start, length, message);
|
||||
}
|
||||
|
||||
function getUnknownCompilerOption(program: Program, configFile: File, option: string) {
|
||||
const quotedOption = `"${option}"`;
|
||||
return getDiagnosticOfFile(program.getCompilerOptions().configFile!, configFile.content.indexOf(quotedOption), quotedOption.length, Diagnostics.Unknown_compiler_option_0, option);
|
||||
}
|
||||
|
||||
describe("tsc-watch program updates", () => {
|
||||
describe("unittests:: tsc-watch:: program updates", () => {
|
||||
it("create watch without config file", () => {
|
||||
const appFile: File = {
|
||||
path: "/a/b/c/app.ts",
|
||||
@ -1486,99 +1461,5 @@ interface Document {
|
||||
});
|
||||
});
|
||||
|
||||
describe("tsc-watch console clearing", () => {
|
||||
const currentDirectoryLog = "Current directory: / CaseSensitiveFileNames: false\n";
|
||||
const fileWatcherAddedLog = [
|
||||
"FileWatcher:: Added:: WatchInfo: /f.ts 250 Source file\n",
|
||||
"FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 Source file\n"
|
||||
];
|
||||
|
||||
const file: File = {
|
||||
path: "/f.ts",
|
||||
content: ""
|
||||
};
|
||||
|
||||
function getProgramSynchronizingLog(options: CompilerOptions) {
|
||||
return [
|
||||
"Synchronizing program\n",
|
||||
"CreatingProgramWith::\n",
|
||||
" roots: [\"/f.ts\"]\n",
|
||||
` options: ${JSON.stringify(options)}\n`
|
||||
];
|
||||
}
|
||||
|
||||
function isConsoleClearDisabled(options: CompilerOptions) {
|
||||
return options.diagnostics || options.extendedDiagnostics || options.preserveWatchOutput;
|
||||
}
|
||||
|
||||
function verifyCompilation(host: WatchedSystem, options: CompilerOptions, initialDisableOptions?: CompilerOptions) {
|
||||
const disableConsoleClear = isConsoleClearDisabled(options);
|
||||
const hasLog = options.extendedDiagnostics || options.diagnostics;
|
||||
checkOutputErrorsInitial(host, emptyArray, initialDisableOptions ? isConsoleClearDisabled(initialDisableOptions) : disableConsoleClear, hasLog ? [
|
||||
currentDirectoryLog,
|
||||
...getProgramSynchronizingLog(options),
|
||||
...(options.extendedDiagnostics ? fileWatcherAddedLog : emptyArray)
|
||||
] : undefined);
|
||||
host.modifyFile(file.path, "//");
|
||||
host.runQueuedTimeoutCallbacks();
|
||||
checkOutputErrorsIncremental(host, emptyArray, disableConsoleClear, hasLog ? [
|
||||
"FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n",
|
||||
"Scheduling update\n",
|
||||
"Elapsed:: 0ms FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n"
|
||||
] : undefined, hasLog ? getProgramSynchronizingLog(options) : undefined);
|
||||
}
|
||||
|
||||
function checkConsoleClearingUsingCommandLineOptions(options: CompilerOptions = {}) {
|
||||
const files = [file, libFile];
|
||||
const host = createWatchedSystem(files);
|
||||
createWatchOfFilesAndCompilerOptions([file.path], host, options);
|
||||
verifyCompilation(host, options);
|
||||
}
|
||||
|
||||
it("without --diagnostics or --extendedDiagnostics", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions();
|
||||
});
|
||||
it("with --diagnostics", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions({
|
||||
diagnostics: true,
|
||||
});
|
||||
});
|
||||
it("with --extendedDiagnostics", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions({
|
||||
extendedDiagnostics: true,
|
||||
});
|
||||
});
|
||||
it("with --preserveWatchOutput", () => {
|
||||
checkConsoleClearingUsingCommandLineOptions({
|
||||
preserveWatchOutput: true,
|
||||
});
|
||||
});
|
||||
|
||||
describe("when preserveWatchOutput is true in config file", () => {
|
||||
const compilerOptions: CompilerOptions = {
|
||||
preserveWatchOutput: true
|
||||
};
|
||||
const configFile: File = {
|
||||
path: "/tsconfig.json",
|
||||
content: JSON.stringify({ compilerOptions })
|
||||
};
|
||||
const files = [file, configFile, libFile];
|
||||
it("using createWatchOfConfigFile ", () => {
|
||||
const host = createWatchedSystem(files);
|
||||
createWatchOfConfigFile(configFile.path, host);
|
||||
// Initially console is cleared if --preserveOutput is not provided since the config file is yet to be parsed
|
||||
verifyCompilation(host, compilerOptions, {});
|
||||
});
|
||||
it("when createWatchProgram is invoked with configFileParseResult on WatchCompilerHostOfConfigFile", () => {
|
||||
const host = createWatchedSystem(files);
|
||||
const reportDiagnostic = createDiagnosticReporter(host);
|
||||
const optionsToExtend: CompilerOptions = {};
|
||||
const configParseResult = parseConfigFileWithSystem(configFile.path, optionsToExtend, host, reportDiagnostic)!;
|
||||
const watchCompilerHost = createWatchCompilerHostOfConfigFile(configParseResult.options.configFilePath!, optionsToExtend, host, /*createProgram*/ undefined, reportDiagnostic, createWatchStatusReporter(host));
|
||||
watchCompilerHost.configFileParsingResult = configParseResult;
|
||||
createWatchProgram(watchCompilerHost);
|
||||
verifyCompilation(host, compilerOptions);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user