From 552777d70e81c8e07b4c891dc9d68a47266c561d Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 28 Sep 2018 15:27:35 -0700 Subject: [PATCH] When there is parse error do not verify actual errors --- .../convertCompilerOptionsFromJson.ts | 132 ++++++------------ 1 file changed, 43 insertions(+), 89 deletions(-) diff --git a/src/testRunner/unittests/convertCompilerOptionsFromJson.ts b/src/testRunner/unittests/convertCompilerOptionsFromJson.ts index fbbf68e9dc..6140ab72ee 100644 --- a/src/testRunner/unittests/convertCompilerOptionsFromJson.ts +++ b/src/testRunner/unittests/convertCompilerOptionsFromJson.ts @@ -5,36 +5,46 @@ namespace ts { getCanonicalFileName: createGetCanonicalFileName(/*useCaseSensitiveFileNames*/ true), getNewLine: () => "\n" }; - function assertCompilerOptions(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) { + + interface ExpectedResultWithParsingSuccess { + compilerOptions: CompilerOptions; + errors: ReadonlyArray; + } + + interface ExpectedResultWithParsingFailure { + compilerOptions: CompilerOptions; + hasParseErrors: true; + } + + type ExpectedResult = ExpectedResultWithParsingSuccess | ExpectedResultWithParsingFailure; + + function isExpectedResultWithParsingFailure(expectedResult: ExpectedResult): expectedResult is ExpectedResultWithParsingFailure { + return !!(expectedResult as ExpectedResultWithParsingFailure).hasParseErrors; + } + + function assertCompilerOptions(json: any, configFileName: string, expectedResult: ExpectedResultWithParsingSuccess) { assertCompilerOptionsWithJson(json, configFileName, expectedResult); assertCompilerOptionsWithJsonNode(json, configFileName, expectedResult); } - function assertCompilerOptionsWithJson(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) { - const { options: actualCompilerOptions, errors: actualErrors} = convertCompilerOptionsFromJson(json.compilerOptions, "/apath/", configFileName); + function assertCompilerOptionsWithJson(json: any, configFileName: string, expectedResult: ExpectedResultWithParsingSuccess) { + const { options: actualCompilerOptions, errors: actualErrors } = convertCompilerOptionsFromJson(json.compilerOptions, "/apath/", configFileName); const parsedCompilerOptions = JSON.stringify(actualCompilerOptions); const expectedCompilerOptions = JSON.stringify({ ...expectedResult.compilerOptions, configFilePath: configFileName }); assert.equal(parsedCompilerOptions, expectedCompilerOptions); - const expectedErrors = expectedResult.errors; - assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`); - for (let i = 0; i < actualErrors.length; i++) { - const actualError = actualErrors[i]; - const expectedError = expectedErrors[i]; - assert.equal(actualError.code, expectedError.code); - assert.equal(actualError.category, expectedError.category); - assert.equal(actualError.messageText, expectedError.messageText); - } + verifyErrors(actualErrors, expectedResult.errors, /*ignoreLocation*/ true); } - function assertCompilerOptionsWithJsonNode(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) { + function assertCompilerOptionsWithJsonNode(json: any, configFileName: string, expectedResult: ExpectedResultWithParsingSuccess) { assertCompilerOptionsWithJsonText(JSON.stringify(json), configFileName, expectedResult); } - function assertCompilerOptionsWithJsonText(fileText: string, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: (Diagnostic | string)[] }) { + function assertCompilerOptionsWithJsonText(fileText: string, configFileName: string, expectedResult: ExpectedResult) { const result = parseJsonText(configFileName, fileText); assert(!!result.endOfFileToken); + assert.equal(!!result.parseDiagnostics.length, isExpectedResultWithParsingFailure(expectedResult)); const host: ParseConfigHost = new fakes.ParseConfigHost(new vfs.FileSystem(/*ignoreCase*/ false, { cwd: "/apath/" })); const { options: actualCompilerOptions, errors: actualParseErrors } = parseJsonSourceFileConfigFileContent(result, host, "/apath/", /*existingOptions*/ undefined, configFileName); expectedResult.compilerOptions.configFilePath = configFileName; @@ -44,27 +54,33 @@ namespace ts { assert.equal(parsedCompilerOptions, expectedCompilerOptions); assert.equal(actualCompilerOptions.configFile, result); - 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)); - const expectedErrors = expectedResult.errors; - 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, " ")}.`); + if (!isExpectedResultWithParsingFailure(expectedResult)) { + 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); + } + } + + function verifyErrors(actualErrors: Diagnostic[], expectedErrors: ReadonlyArray, ignoreLocation?: boolean) { + assert.isTrue(expectedErrors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedErrors.map(getDiagnosticString), undefined, " ")}. Actual error: ${JSON.stringify(actualErrors.map(getDiagnosticString), undefined, " ")}.`); for (let i = 0; i < actualErrors.length; i++) { const actualError = actualErrors[i]; const expectedError = expectedErrors[i]; - if (isString(expectedError)) { - assert.equal(getDiagnosticString(actualError), expectedError); - } - else { - assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`); - assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`); + + assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`); + assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`); + if (!ignoreLocation) { assert(actualError.file); assert(actualError.start); assert(actualError.length); } } - } - function getDiagnosticString(diagnostic: Diagnostic) { - return formatDiagnostic(diagnostic, formatDiagnosticHost); + function getDiagnosticString(diagnostic: Diagnostic) { + if (ignoreLocation) { + const { file, ...rest } = diagnostic; + diagnostic = { file: undefined, ...rest }; + } + return formatDiagnostic(diagnostic, formatDiagnosticHost); + } } // tsconfig.json tests @@ -583,69 +599,7 @@ namespace ts { module: ModuleKind.ESNext, types: [] }, - errors: [ - "tsconfig.json(5,5): error TS1136: Property assignment expected.\n", - "tsconfig.json(5,6): error TS1136: Property assignment expected.\n", - "tsconfig.json(5,9): error TS1005: ':' expected.\n", - "tsconfig.json(5,20): error TS1005: ',' expected.\n", - "tsconfig.json(5,41): error TS1109: Expression expected.\n", - "tsconfig.json(6,29): error TS1005: ';' expected.\n", - "tsconfig.json(7,5): error TS1109: Expression expected.\n", - "tsconfig.json(7,6): error TS1109: Expression expected.\n", - "tsconfig.json(7,11): error TS1005: ',' expected.\n", - "tsconfig.json(7,12): error TS1005: ':' expected.\n", - "tsconfig.json(7,13): error TS1109: Expression expected.\n", - "tsconfig.json(8,16): error TS1005: ',' expected.\n", - "tsconfig.json(8,22): error TS1005: ':' expected.\n", - "tsconfig.json(10,21): error TS1109: Expression expected.\n", - "tsconfig.json(10,23): error TS1109: Expression expected.\n", - "tsconfig.json(10,36): error TS1005: ',' expected.\n", - "tsconfig.json(10,50): error TS1109: Expression expected.\n", - "tsconfig.json(10,51): error TS1109: Expression expected.\n", - "tsconfig.json(10,52): error TS1109: Expression expected.\n", - "tsconfig.json(10,53): error TS1109: Expression expected.\n", - "tsconfig.json(10,54): error TS1109: Expression expected.\n", - "tsconfig.json(10,56): error TS1109: Expression expected.\n", - "tsconfig.json(10,58): error TS1005: ',' expected.\n", - "tsconfig.json(10,59): error TS1136: Property assignment expected.\n", - "tsconfig.json(11,7): error TS1136: Property assignment expected.\n", - "tsconfig.json(11,8): error TS1136: Property assignment expected.\n", - "tsconfig.json(11,11): error TS1005: ':' expected.\n", - "tsconfig.json(11,29): error TS1109: Expression expected.\n", - "tsconfig.json(14,8): error TS1109: Expression expected.\n", - "tsconfig.json(14,13): error TS1005: ',' expected.\n", - "tsconfig.json(14,18): error TS1005: ':' expected.\n", - "tsconfig.json(14,35): error TS1109: Expression expected.\n", - "tsconfig.json(16,8): error TS1109: Expression expected.\n", - "tsconfig.json(16,13): error TS1005: ',' expected.\n", - "tsconfig.json(16,14): error TS1005: ':' expected.\n", - "tsconfig.json(16,15): error TS1109: Expression expected.\n", - "tsconfig.json(17,5): error TS1109: Expression expected.\n", - "tsconfig.json(3,15): error TS6046: Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'esnext'.\n", - "tsconfig.json(5,7): error TS1327: String literal with double quotes expected.\n", - "tsconfig.json(5,7): error TS5023: Unknown compiler option '_'.\n", - "tsconfig.json(5,8): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n", - "tsconfig.json(5,9): error TS1136: Property assignment expected.\n", - "tsconfig.json(7,11): error TS1327: String literal with double quotes expected.\n", - "tsconfig.json(7,11): error TS5023: Unknown compiler option '_'.\n", - "tsconfig.json(7,12): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n", - "tsconfig.json(8,18): error TS1327: String literal with double quotes expected.\n", - "tsconfig.json(8,18): error TS5023: Unknown compiler option 'true'.\n", - "tsconfig.json(8,22): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n", - "tsconfig.json(10,7): error TS5024: Compiler option 'types' requires a value of type string.\n", - "tsconfig.json(10,23): error TS1136: Property assignment expected.\n", - "tsconfig.json(11,9): error TS1327: String literal with double quotes expected.\n", - "tsconfig.json(11,9): error TS5023: Unknown compiler option '_'.\n", - "tsconfig.json(11,10): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n", - "tsconfig.json(11,11): error TS1136: Property assignment expected.\n", - "tsconfig.json(14,13): error TS1327: String literal with double quotes expected.\n", - "tsconfig.json(14,13): error TS5023: Unknown compiler option 'else'.\n", - "tsconfig.json(14,17): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n", - "tsconfig.json(14,18): error TS1136: Property assignment expected.\n", - "tsconfig.json(16,13): error TS1327: String literal with double quotes expected.\n", - "tsconfig.json(16,13): error TS5023: Unknown compiler option '_'.\n", - "tsconfig.json(16,14): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n" - ] + hasParseErrors: true } ); });