ci: fix symbol test to handle duplicate symbols (#28459)

In 7cb8396, we improved the symbol test to remove suffixes from
the output, so "CIRCULAR$1" became "CIRCULAR". However, the logic
that checked for extra symbols depended on each symbol being unique.
If multiple symbols with the same name were added (e.g. pulled in
from separate files), they would be added to the map as "extras",
even if they were marked as expected in the golden file.

This commit updates the symbol checking logic to take multiple
symbols with the same name into account.

Closes #28406

PR Close #28459
This commit is contained in:
Kara Erickson 2019-01-30 15:29:09 -08:00 committed by Matias Niemelä
parent 35e45dc894
commit c0b383590e
1 changed files with 15 additions and 16 deletions

View File

@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as fs from 'fs';
import * as ts from 'typescript';
@ -67,25 +66,31 @@ export class SymbolExtractor {
return symbols;
}
static diff(actual: Symbol[], expected: string|((Symbol | string)[])): {[name: string]: string} {
static diff(actual: Symbol[], expected: string|((Symbol | string)[])): {[name: string]: number} {
if (typeof expected == 'string') {
expected = JSON.parse(expected);
}
const diff: {[name: string]: ('missing' | 'extra')} = {};
const diff: {[name: string]: number} = {};
// All symbols in the golden file start out with a count corresponding to the number of symbols
// with that name. Once they are matched with symbols in the actual output, the count should
// even out to 0.
(expected as(Symbol | string)[]).forEach((nameOrSymbol) => {
diff[typeof nameOrSymbol == 'string' ? nameOrSymbol : nameOrSymbol.name] = 'missing';
const symbolName = typeof nameOrSymbol == 'string' ? nameOrSymbol : nameOrSymbol.name;
diff[symbolName] = (diff[symbolName] || 0) + 1;
});
actual.forEach((s) => {
if (diff[s.name] === 'missing') {
if (diff[s.name] === 1) {
delete diff[s.name];
} else {
diff[s.name] = 'extra';
diff[s.name] = (diff[s.name] || 0) - 1;
}
});
return diff;
}
constructor(private path: string, private contents: string) {
this.actual = SymbolExtractor.parse(path, contents);
}
@ -102,7 +107,9 @@ export class SymbolExtractor {
console.error(`Expected symbols in '${this.path}' did not match gold file.`);
passed = false;
}
console.error(` Symbol: ${key} => ${diff[key]}`);
const missingOrExtra = diff[key] > 0 ? 'extra' : 'missing';
const count = Math.abs(diff[key]);
console.error(` Symbol: ${key} => ${count} ${missingOrExtra} in golden file.`);
});
return passed;
@ -114,14 +121,6 @@ function stripSuffix(text: string): string {
return index > -1 ? text.substring(0, index) : text;
}
function toSymbol(v: string | Symbol): Symbol {
return typeof v == 'string' ? {'name': v} : v as Symbol;
}
function toName(symbol: Symbol): string {
return symbol.name;
}
/**
* Detects if VariableDeclarationList is format `var ..., bundle = function(){}()`;
*
@ -131,4 +130,4 @@ function toName(symbol: Symbol): string {
function isRollupExportSymbol(decl: ts.VariableDeclaration): boolean {
return !!(decl.initializer && decl.initializer.kind == ts.SyntaxKind.CallExpression) &&
ts.isIdentifier(decl.name) && decl.name.text === 'bundle';
}
}