Skip to content

Commit 552777d

Browse files
committed
When there is parse error do not verify actual errors
1 parent 0245c2d commit 552777d

File tree

1 file changed

+43
-89
lines changed

1 file changed

+43
-89
lines changed

src/testRunner/unittests/convertCompilerOptionsFromJson.ts

Lines changed: 43 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,46 @@ namespace ts {
55
getCanonicalFileName: createGetCanonicalFileName(/*useCaseSensitiveFileNames*/ true),
66
getNewLine: () => "\n"
77
};
8-
function assertCompilerOptions(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
8+
9+
interface ExpectedResultWithParsingSuccess {
10+
compilerOptions: CompilerOptions;
11+
errors: ReadonlyArray<Diagnostic>;
12+
}
13+
14+
interface ExpectedResultWithParsingFailure {
15+
compilerOptions: CompilerOptions;
16+
hasParseErrors: true;
17+
}
18+
19+
type ExpectedResult = ExpectedResultWithParsingSuccess | ExpectedResultWithParsingFailure;
20+
21+
function isExpectedResultWithParsingFailure(expectedResult: ExpectedResult): expectedResult is ExpectedResultWithParsingFailure {
22+
return !!(expectedResult as ExpectedResultWithParsingFailure).hasParseErrors;
23+
}
24+
25+
function assertCompilerOptions(json: any, configFileName: string, expectedResult: ExpectedResultWithParsingSuccess) {
926
assertCompilerOptionsWithJson(json, configFileName, expectedResult);
1027
assertCompilerOptionsWithJsonNode(json, configFileName, expectedResult);
1128
}
1229

13-
function assertCompilerOptionsWithJson(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
14-
const { options: actualCompilerOptions, errors: actualErrors} = convertCompilerOptionsFromJson(json.compilerOptions, "/apath/", configFileName);
30+
function assertCompilerOptionsWithJson(json: any, configFileName: string, expectedResult: ExpectedResultWithParsingSuccess) {
31+
const { options: actualCompilerOptions, errors: actualErrors } = convertCompilerOptionsFromJson(json.compilerOptions, "/apath/", configFileName);
1532

1633
const parsedCompilerOptions = JSON.stringify(actualCompilerOptions);
1734
const expectedCompilerOptions = JSON.stringify({ ...expectedResult.compilerOptions, configFilePath: configFileName });
1835
assert.equal(parsedCompilerOptions, expectedCompilerOptions);
1936

20-
const expectedErrors = expectedResult.errors;
21-
assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`);
22-
for (let i = 0; i < actualErrors.length; i++) {
23-
const actualError = actualErrors[i];
24-
const expectedError = expectedErrors[i];
25-
assert.equal(actualError.code, expectedError.code);
26-
assert.equal(actualError.category, expectedError.category);
27-
assert.equal(actualError.messageText, expectedError.messageText);
28-
}
37+
verifyErrors(actualErrors, expectedResult.errors, /*ignoreLocation*/ true);
2938
}
3039

31-
function assertCompilerOptionsWithJsonNode(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
40+
function assertCompilerOptionsWithJsonNode(json: any, configFileName: string, expectedResult: ExpectedResultWithParsingSuccess) {
3241
assertCompilerOptionsWithJsonText(JSON.stringify(json), configFileName, expectedResult);
3342
}
3443

35-
function assertCompilerOptionsWithJsonText(fileText: string, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: (Diagnostic | string)[] }) {
44+
function assertCompilerOptionsWithJsonText(fileText: string, configFileName: string, expectedResult: ExpectedResult) {
3645
const result = parseJsonText(configFileName, fileText);
3746
assert(!!result.endOfFileToken);
47+
assert.equal(!!result.parseDiagnostics.length, isExpectedResultWithParsingFailure(expectedResult));
3848
const host: ParseConfigHost = new fakes.ParseConfigHost(new vfs.FileSystem(/*ignoreCase*/ false, { cwd: "/apath/" }));
3949
const { options: actualCompilerOptions, errors: actualParseErrors } = parseJsonSourceFileConfigFileContent(result, host, "/apath/", /*existingOptions*/ undefined, configFileName);
4050
expectedResult.compilerOptions.configFilePath = configFileName;
@@ -44,27 +54,33 @@ namespace ts {
4454
assert.equal(parsedCompilerOptions, expectedCompilerOptions);
4555
assert.equal(actualCompilerOptions.configFile, result);
4656

47-
const actualErrors = (result.parseDiagnostics as Diagnostic[]).concat(actualParseErrors.filter(error => error.code !== Diagnostics.No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2.code));
48-
const expectedErrors = expectedResult.errors;
49-
assert.isTrue(expectedErrors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedErrors.map(e => isString(e) ? e : getDiagnosticString(e)), undefined, " ")}. Actual error: ${JSON.stringify(actualErrors.map(getDiagnosticString), undefined, " ")}.`);
57+
if (!isExpectedResultWithParsingFailure(expectedResult)) {
58+
verifyErrors(actualParseErrors.filter(error => error.code !== Diagnostics.No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2.code), expectedResult.errors);
59+
}
60+
}
61+
62+
function verifyErrors(actualErrors: Diagnostic[], expectedErrors: ReadonlyArray<Diagnostic>, ignoreLocation?: boolean) {
63+
assert.isTrue(expectedErrors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedErrors.map(getDiagnosticString), undefined, " ")}. Actual error: ${JSON.stringify(actualErrors.map(getDiagnosticString), undefined, " ")}.`);
5064
for (let i = 0; i < actualErrors.length; i++) {
5165
const actualError = actualErrors[i];
5266
const expectedError = expectedErrors[i];
53-
if (isString(expectedError)) {
54-
assert.equal(getDiagnosticString(actualError), expectedError);
55-
}
56-
else {
57-
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
58-
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
67+
68+
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
69+
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
70+
if (!ignoreLocation) {
5971
assert(actualError.file);
6072
assert(actualError.start);
6173
assert(actualError.length);
6274
}
6375
}
64-
}
6576

66-
function getDiagnosticString(diagnostic: Diagnostic) {
67-
return formatDiagnostic(diagnostic, formatDiagnosticHost);
77+
function getDiagnosticString(diagnostic: Diagnostic) {
78+
if (ignoreLocation) {
79+
const { file, ...rest } = diagnostic;
80+
diagnostic = { file: undefined, ...rest };
81+
}
82+
return formatDiagnostic(diagnostic, formatDiagnosticHost);
83+
}
6884
}
6985

7086
// tsconfig.json tests
@@ -583,69 +599,7 @@ namespace ts {
583599
module: ModuleKind.ESNext,
584600
types: []
585601
},
586-
errors: [
587-
"tsconfig.json(5,5): error TS1136: Property assignment expected.\n",
588-
"tsconfig.json(5,6): error TS1136: Property assignment expected.\n",
589-
"tsconfig.json(5,9): error TS1005: ':' expected.\n",
590-
"tsconfig.json(5,20): error TS1005: ',' expected.\n",
591-
"tsconfig.json(5,41): error TS1109: Expression expected.\n",
592-
"tsconfig.json(6,29): error TS1005: ';' expected.\n",
593-
"tsconfig.json(7,5): error TS1109: Expression expected.\n",
594-
"tsconfig.json(7,6): error TS1109: Expression expected.\n",
595-
"tsconfig.json(7,11): error TS1005: ',' expected.\n",
596-
"tsconfig.json(7,12): error TS1005: ':' expected.\n",
597-
"tsconfig.json(7,13): error TS1109: Expression expected.\n",
598-
"tsconfig.json(8,16): error TS1005: ',' expected.\n",
599-
"tsconfig.json(8,22): error TS1005: ':' expected.\n",
600-
"tsconfig.json(10,21): error TS1109: Expression expected.\n",
601-
"tsconfig.json(10,23): error TS1109: Expression expected.\n",
602-
"tsconfig.json(10,36): error TS1005: ',' expected.\n",
603-
"tsconfig.json(10,50): error TS1109: Expression expected.\n",
604-
"tsconfig.json(10,51): error TS1109: Expression expected.\n",
605-
"tsconfig.json(10,52): error TS1109: Expression expected.\n",
606-
"tsconfig.json(10,53): error TS1109: Expression expected.\n",
607-
"tsconfig.json(10,54): error TS1109: Expression expected.\n",
608-
"tsconfig.json(10,56): error TS1109: Expression expected.\n",
609-
"tsconfig.json(10,58): error TS1005: ',' expected.\n",
610-
"tsconfig.json(10,59): error TS1136: Property assignment expected.\n",
611-
"tsconfig.json(11,7): error TS1136: Property assignment expected.\n",
612-
"tsconfig.json(11,8): error TS1136: Property assignment expected.\n",
613-
"tsconfig.json(11,11): error TS1005: ':' expected.\n",
614-
"tsconfig.json(11,29): error TS1109: Expression expected.\n",
615-
"tsconfig.json(14,8): error TS1109: Expression expected.\n",
616-
"tsconfig.json(14,13): error TS1005: ',' expected.\n",
617-
"tsconfig.json(14,18): error TS1005: ':' expected.\n",
618-
"tsconfig.json(14,35): error TS1109: Expression expected.\n",
619-
"tsconfig.json(16,8): error TS1109: Expression expected.\n",
620-
"tsconfig.json(16,13): error TS1005: ',' expected.\n",
621-
"tsconfig.json(16,14): error TS1005: ':' expected.\n",
622-
"tsconfig.json(16,15): error TS1109: Expression expected.\n",
623-
"tsconfig.json(17,5): error TS1109: Expression expected.\n",
624-
"tsconfig.json(3,15): error TS6046: Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'esnext'.\n",
625-
"tsconfig.json(5,7): error TS1327: String literal with double quotes expected.\n",
626-
"tsconfig.json(5,7): error TS5023: Unknown compiler option '_'.\n",
627-
"tsconfig.json(5,8): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
628-
"tsconfig.json(5,9): error TS1136: Property assignment expected.\n",
629-
"tsconfig.json(7,11): error TS1327: String literal with double quotes expected.\n",
630-
"tsconfig.json(7,11): error TS5023: Unknown compiler option '_'.\n",
631-
"tsconfig.json(7,12): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
632-
"tsconfig.json(8,18): error TS1327: String literal with double quotes expected.\n",
633-
"tsconfig.json(8,18): error TS5023: Unknown compiler option 'true'.\n",
634-
"tsconfig.json(8,22): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
635-
"tsconfig.json(10,7): error TS5024: Compiler option 'types' requires a value of type string.\n",
636-
"tsconfig.json(10,23): error TS1136: Property assignment expected.\n",
637-
"tsconfig.json(11,9): error TS1327: String literal with double quotes expected.\n",
638-
"tsconfig.json(11,9): error TS5023: Unknown compiler option '_'.\n",
639-
"tsconfig.json(11,10): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
640-
"tsconfig.json(11,11): error TS1136: Property assignment expected.\n",
641-
"tsconfig.json(14,13): error TS1327: String literal with double quotes expected.\n",
642-
"tsconfig.json(14,13): error TS5023: Unknown compiler option 'else'.\n",
643-
"tsconfig.json(14,17): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n",
644-
"tsconfig.json(14,18): error TS1136: Property assignment expected.\n",
645-
"tsconfig.json(16,13): error TS1327: String literal with double quotes expected.\n",
646-
"tsconfig.json(16,13): error TS5023: Unknown compiler option '_'.\n",
647-
"tsconfig.json(16,14): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n"
648-
]
602+
hasParseErrors: true
649603
}
650604
);
651605
});

0 commit comments

Comments
 (0)