Skip to content

Commit efc7160

Browse files
committed
Refactor tsc-watch tests
1 parent f117184 commit efc7160

File tree

4 files changed

+125
-121
lines changed

4 files changed

+125
-121
lines changed

src/testRunner/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"unittests/transform.ts",
6161
"unittests/tsbuild.ts",
6262
"unittests/tsbuildWatchMode.ts",
63-
"unittests/tscWatchMode.ts",
6463
"unittests/config/commandLineParsing.ts",
6564
"unittests/config/configurationExtension.ts",
6665
"unittests/config/convertCompilerOptionsFromJson.ts",
@@ -88,7 +87,9 @@
8887
"unittests/services/preProcessFile.ts",
8988
"unittests/services/textChanges.ts",
9089
"unittests/services/transpile.ts",
90+
"unittests/tscWatch/consoleClearing.ts",
9191
"unittests/tscWatch/emit.ts",
92+
"unittests/tscWatch/programUpdates.ts",
9293
"unittests/tscWatch/resolutionCache.ts",
9394
"unittests/tscWatch/watchEnvironment.ts",
9495
"unittests/tscWatch/watchApi.ts",
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
namespace ts.tscWatch {
2+
describe("unittests:: tsc-watch:: console clearing", () => {
3+
const currentDirectoryLog = "Current directory: / CaseSensitiveFileNames: false\n";
4+
const fileWatcherAddedLog = [
5+
"FileWatcher:: Added:: WatchInfo: /f.ts 250 Source file\n",
6+
"FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 Source file\n"
7+
];
8+
9+
const file: File = {
10+
path: "/f.ts",
11+
content: ""
12+
};
13+
14+
function getProgramSynchronizingLog(options: CompilerOptions) {
15+
return [
16+
"Synchronizing program\n",
17+
"CreatingProgramWith::\n",
18+
" roots: [\"/f.ts\"]\n",
19+
` options: ${JSON.stringify(options)}\n`
20+
];
21+
}
22+
23+
function isConsoleClearDisabled(options: CompilerOptions) {
24+
return options.diagnostics || options.extendedDiagnostics || options.preserveWatchOutput;
25+
}
26+
27+
function verifyCompilation(host: WatchedSystem, options: CompilerOptions, initialDisableOptions?: CompilerOptions) {
28+
const disableConsoleClear = isConsoleClearDisabled(options);
29+
const hasLog = options.extendedDiagnostics || options.diagnostics;
30+
checkOutputErrorsInitial(host, emptyArray, initialDisableOptions ? isConsoleClearDisabled(initialDisableOptions) : disableConsoleClear, hasLog ? [
31+
currentDirectoryLog,
32+
...getProgramSynchronizingLog(options),
33+
...(options.extendedDiagnostics ? fileWatcherAddedLog : emptyArray)
34+
] : undefined);
35+
host.modifyFile(file.path, "//");
36+
host.runQueuedTimeoutCallbacks();
37+
checkOutputErrorsIncremental(host, emptyArray, disableConsoleClear, hasLog ? [
38+
"FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n",
39+
"Scheduling update\n",
40+
"Elapsed:: 0ms FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n"
41+
] : undefined, hasLog ? getProgramSynchronizingLog(options) : undefined);
42+
}
43+
44+
function checkConsoleClearingUsingCommandLineOptions(options: CompilerOptions = {}) {
45+
const files = [file, libFile];
46+
const host = createWatchedSystem(files);
47+
createWatchOfFilesAndCompilerOptions([file.path], host, options);
48+
verifyCompilation(host, options);
49+
}
50+
51+
it("without --diagnostics or --extendedDiagnostics", () => {
52+
checkConsoleClearingUsingCommandLineOptions();
53+
});
54+
it("with --diagnostics", () => {
55+
checkConsoleClearingUsingCommandLineOptions({
56+
diagnostics: true,
57+
});
58+
});
59+
it("with --extendedDiagnostics", () => {
60+
checkConsoleClearingUsingCommandLineOptions({
61+
extendedDiagnostics: true,
62+
});
63+
});
64+
it("with --preserveWatchOutput", () => {
65+
checkConsoleClearingUsingCommandLineOptions({
66+
preserveWatchOutput: true,
67+
});
68+
});
69+
70+
describe("when preserveWatchOutput is true in config file", () => {
71+
const compilerOptions: CompilerOptions = {
72+
preserveWatchOutput: true
73+
};
74+
const configFile: File = {
75+
path: "/tsconfig.json",
76+
content: JSON.stringify({ compilerOptions })
77+
};
78+
const files = [file, configFile, libFile];
79+
it("using createWatchOfConfigFile ", () => {
80+
const host = createWatchedSystem(files);
81+
createWatchOfConfigFile(configFile.path, host);
82+
// Initially console is cleared if --preserveOutput is not provided since the config file is yet to be parsed
83+
verifyCompilation(host, compilerOptions, {});
84+
});
85+
it("when createWatchProgram is invoked with configFileParseResult on WatchCompilerHostOfConfigFile", () => {
86+
const host = createWatchedSystem(files);
87+
const reportDiagnostic = createDiagnosticReporter(host);
88+
const optionsToExtend: CompilerOptions = {};
89+
const configParseResult = parseConfigFileWithSystem(configFile.path, optionsToExtend, host, reportDiagnostic)!;
90+
const watchCompilerHost = createWatchCompilerHostOfConfigFile(configParseResult.options.configFilePath!, optionsToExtend, host, /*createProgram*/ undefined, reportDiagnostic, createWatchStatusReporter(host));
91+
watchCompilerHost.configFileParsingResult = configParseResult;
92+
createWatchProgram(watchCompilerHost);
93+
verifyCompilation(host, compilerOptions);
94+
});
95+
});
96+
});
97+
}

src/testRunner/unittests/tscWatch/helpers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ namespace ts.tscWatch {
164164
};
165165
}
166166

167+
export function getDiagnosticWithoutFile(message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
168+
let text = getLocaleSpecificMessage(message);
169+
170+
if (arguments.length > 1) {
171+
text = formatStringFromArgs(text, arguments, 1);
172+
}
173+
174+
return getDiagnosticOfFileFrom(/*file*/ undefined, text, /*start*/ undefined, /*length*/ undefined, message);
175+
}
176+
177+
export function getDiagnosticOfFile(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
178+
let text = getLocaleSpecificMessage(message);
179+
180+
if (arguments.length > 4) {
181+
text = formatStringFromArgs(text, arguments, 4);
182+
}
183+
184+
return getDiagnosticOfFileFrom(file, text, start, length, message);
185+
}
186+
167187
export function getDiagnosticOfFileFromProgram(program: Program, filePath: string, start: number, length: number, message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
168188
let text = getLocaleSpecificMessage(message);
169189

@@ -175,6 +195,11 @@ namespace ts.tscWatch {
175195
text, start, length, message);
176196
}
177197

198+
export function getUnknownCompilerOption(program: Program, configFile: File, option: string) {
199+
const quotedOption = `"${option}"`;
200+
return getDiagnosticOfFile(program.getCompilerOptions().configFile!, configFile.content.indexOf(quotedOption), quotedOption.length, Diagnostics.Unknown_compiler_option_0, option);
201+
}
202+
178203
export function getDiagnosticModuleNotFoundOfFile(program: Program, file: File, moduleName: string) {
179204
const quotedModuleName = `"${moduleName}"`;
180205
return getDiagnosticOfFileFromProgram(program, file.path, file.content.indexOf(quotedModuleName), quotedModuleName.length, Diagnostics.Cannot_find_module_0, moduleName);

src/testRunner/unittests/tscWatchMode.ts renamed to src/testRunner/unittests/tscWatch/programUpdates.ts

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
namespace ts.tscWatch {
2-
function getDiagnosticWithoutFile(message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
3-
let text = getLocaleSpecificMessage(message);
4-
5-
if (arguments.length > 1) {
6-
text = formatStringFromArgs(text, arguments, 1);
7-
}
8-
9-
return getDiagnosticOfFileFrom(/*file*/ undefined, text, /*start*/ undefined, /*length*/ undefined, message);
10-
}
11-
12-
function getDiagnosticOfFile(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ..._args: (string | number)[]): Diagnostic {
13-
let text = getLocaleSpecificMessage(message);
14-
15-
if (arguments.length > 4) {
16-
text = formatStringFromArgs(text, arguments, 4);
17-
}
18-
19-
return getDiagnosticOfFileFrom(file, text, start, length, message);
20-
}
21-
22-
function getUnknownCompilerOption(program: Program, configFile: File, option: string) {
23-
const quotedOption = `"${option}"`;
24-
return getDiagnosticOfFile(program.getCompilerOptions().configFile!, configFile.content.indexOf(quotedOption), quotedOption.length, Diagnostics.Unknown_compiler_option_0, option);
25-
}
26-
27-
describe("tsc-watch program updates", () => {
2+
describe("unittests:: tsc-watch:: program updates", () => {
283
it("create watch without config file", () => {
294
const appFile: File = {
305
path: "/a/b/c/app.ts",
@@ -1486,99 +1461,5 @@ interface Document {
14861461
});
14871462
});
14881463

1489-
describe("tsc-watch console clearing", () => {
1490-
const currentDirectoryLog = "Current directory: / CaseSensitiveFileNames: false\n";
1491-
const fileWatcherAddedLog = [
1492-
"FileWatcher:: Added:: WatchInfo: /f.ts 250 Source file\n",
1493-
"FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 Source file\n"
1494-
];
1495-
1496-
const file: File = {
1497-
path: "/f.ts",
1498-
content: ""
1499-
};
1500-
1501-
function getProgramSynchronizingLog(options: CompilerOptions) {
1502-
return [
1503-
"Synchronizing program\n",
1504-
"CreatingProgramWith::\n",
1505-
" roots: [\"/f.ts\"]\n",
1506-
` options: ${JSON.stringify(options)}\n`
1507-
];
1508-
}
1509-
1510-
function isConsoleClearDisabled(options: CompilerOptions) {
1511-
return options.diagnostics || options.extendedDiagnostics || options.preserveWatchOutput;
1512-
}
1513-
1514-
function verifyCompilation(host: WatchedSystem, options: CompilerOptions, initialDisableOptions?: CompilerOptions) {
1515-
const disableConsoleClear = isConsoleClearDisabled(options);
1516-
const hasLog = options.extendedDiagnostics || options.diagnostics;
1517-
checkOutputErrorsInitial(host, emptyArray, initialDisableOptions ? isConsoleClearDisabled(initialDisableOptions) : disableConsoleClear, hasLog ? [
1518-
currentDirectoryLog,
1519-
...getProgramSynchronizingLog(options),
1520-
...(options.extendedDiagnostics ? fileWatcherAddedLog : emptyArray)
1521-
] : undefined);
1522-
host.modifyFile(file.path, "//");
1523-
host.runQueuedTimeoutCallbacks();
1524-
checkOutputErrorsIncremental(host, emptyArray, disableConsoleClear, hasLog ? [
1525-
"FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n",
1526-
"Scheduling update\n",
1527-
"Elapsed:: 0ms FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n"
1528-
] : undefined, hasLog ? getProgramSynchronizingLog(options) : undefined);
1529-
}
15301464

1531-
function checkConsoleClearingUsingCommandLineOptions(options: CompilerOptions = {}) {
1532-
const files = [file, libFile];
1533-
const host = createWatchedSystem(files);
1534-
createWatchOfFilesAndCompilerOptions([file.path], host, options);
1535-
verifyCompilation(host, options);
1536-
}
1537-
1538-
it("without --diagnostics or --extendedDiagnostics", () => {
1539-
checkConsoleClearingUsingCommandLineOptions();
1540-
});
1541-
it("with --diagnostics", () => {
1542-
checkConsoleClearingUsingCommandLineOptions({
1543-
diagnostics: true,
1544-
});
1545-
});
1546-
it("with --extendedDiagnostics", () => {
1547-
checkConsoleClearingUsingCommandLineOptions({
1548-
extendedDiagnostics: true,
1549-
});
1550-
});
1551-
it("with --preserveWatchOutput", () => {
1552-
checkConsoleClearingUsingCommandLineOptions({
1553-
preserveWatchOutput: true,
1554-
});
1555-
});
1556-
1557-
describe("when preserveWatchOutput is true in config file", () => {
1558-
const compilerOptions: CompilerOptions = {
1559-
preserveWatchOutput: true
1560-
};
1561-
const configFile: File = {
1562-
path: "/tsconfig.json",
1563-
content: JSON.stringify({ compilerOptions })
1564-
};
1565-
const files = [file, configFile, libFile];
1566-
it("using createWatchOfConfigFile ", () => {
1567-
const host = createWatchedSystem(files);
1568-
createWatchOfConfigFile(configFile.path, host);
1569-
// Initially console is cleared if --preserveOutput is not provided since the config file is yet to be parsed
1570-
verifyCompilation(host, compilerOptions, {});
1571-
});
1572-
it("when createWatchProgram is invoked with configFileParseResult on WatchCompilerHostOfConfigFile", () => {
1573-
const host = createWatchedSystem(files);
1574-
const reportDiagnostic = createDiagnosticReporter(host);
1575-
const optionsToExtend: CompilerOptions = {};
1576-
const configParseResult = parseConfigFileWithSystem(configFile.path, optionsToExtend, host, reportDiagnostic)!;
1577-
const watchCompilerHost = createWatchCompilerHostOfConfigFile(configParseResult.options.configFilePath!, optionsToExtend, host, /*createProgram*/ undefined, reportDiagnostic, createWatchStatusReporter(host));
1578-
watchCompilerHost.configFileParsingResult = configParseResult;
1579-
createWatchProgram(watchCompilerHost);
1580-
verifyCompilation(host, compilerOptions);
1581-
});
1582-
});
1583-
});
15841465
}

0 commit comments

Comments
 (0)