-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Add bundle symbol extractor tool #22002
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/core/test/bundling/hello_world/bundle.golden_symbols.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
[ | ||
{ | ||
"name": "EMPTY$1" | ||
}, | ||
{ | ||
"name": "NO_CHANGE" | ||
}, | ||
{ | ||
"name": "Symbol$1" | ||
}, | ||
{ | ||
"name": "__global$1" | ||
}, | ||
{ | ||
"name": "__self$1" | ||
}, | ||
{ | ||
"name": "__window$1" | ||
}, | ||
{ | ||
"name": "_renderCompCount" | ||
}, | ||
{ | ||
"name": "_root" | ||
}, | ||
{ | ||
"name": "canInsertNativeNode" | ||
}, | ||
{ | ||
"name": "createLNode" | ||
}, | ||
{ | ||
"name": "createLView" | ||
}, | ||
{ | ||
"name": "domRendererFactory3" | ||
}, | ||
{ | ||
"name": "enterView" | ||
}, | ||
{ | ||
"name": "executeHooks" | ||
}, | ||
{ | ||
"name": "findFirstRNode" | ||
}, | ||
{ | ||
"name": "getDirectiveInstance" | ||
}, | ||
{ | ||
"name": "getNextLNodeWithProjection" | ||
}, | ||
{ | ||
"name": "getNextOrParentSiblingNode" | ||
}, | ||
{ | ||
"name": "invertObject" | ||
}, | ||
{ | ||
"name": "isProceduralRenderer" | ||
}, | ||
{ | ||
"name": "leaveView" | ||
}, | ||
{ | ||
"name": "locateHostElement" | ||
}, | ||
{ | ||
"name": "noop$2" | ||
}, | ||
{ | ||
"name": "refreshDynamicChildren" | ||
}, | ||
{ | ||
"name": "renderComponentOrTemplate" | ||
}, | ||
{ | ||
"name": "renderEmbeddedTemplate" | ||
}, | ||
{ | ||
"name": "stringify$1" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tools:defaults.bzl", "ts_library") | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") | ||
|
||
ts_library( | ||
name = "lib", | ||
testonly = 1, | ||
srcs = glob( | ||
["**/*.ts"], | ||
exclude = [ | ||
"**/*_spec.ts", | ||
"**/*_spec", | ||
], | ||
), | ||
deps = [ | ||
"//packages:types", | ||
], | ||
) | ||
|
||
ts_library( | ||
name = "test_lib", | ||
testonly = 1, | ||
srcs = glob( | ||
["**/*_spec.ts"], | ||
exclude = ["symbol_extractor_spec/**"], | ||
), | ||
deps = [ | ||
":lib", | ||
"//packages:types", | ||
], | ||
) | ||
|
||
jasmine_node_test( | ||
name = "test", | ||
data = glob(["symbol_extractor_spec/**"]), | ||
deps = [ | ||
":test_lib", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import {SymbolExtractor} from './symbol_extractor'; | ||
|
||
// These keys are arbitrary and local to this test. | ||
const update_var = 'UPDATE_GOLDEN'; | ||
const update_val = 1; | ||
|
||
if (require.main === module) { | ||
const doUpdate = process.env[update_var] == update_val; | ||
const args = process.argv.slice(2) as[string, string]; | ||
process.exitCode = main(args, doUpdate) ? 0 : 1; | ||
} | ||
|
||
/** | ||
* CLI main method. | ||
* | ||
* ``` | ||
* cli javascriptFilePath.js goldenFilePath.json | ||
* ``` | ||
*/ | ||
function main(argv: [string, string], doUpdate: boolean): boolean { | ||
const javascriptFilePath = require.resolve(argv[0]); | ||
const goldenFilePath = require.resolve(argv[1]); | ||
|
||
const javascriptContent = fs.readFileSync(javascriptFilePath).toString(); | ||
const goldenContent = fs.readFileSync(goldenFilePath).toString(); | ||
|
||
const symbolExtractor = new SymbolExtractor(javascriptFilePath, javascriptContent); | ||
|
||
let passed: boolean = false; | ||
if (doUpdate) { | ||
fs.writeFileSync(goldenFilePath, JSON.stringify(symbolExtractor.actual, undefined, 2)); | ||
console.error('Updated gold file:', goldenFilePath); | ||
passed = true; | ||
} else { | ||
passed = symbolExtractor.compareAndPrintError(goldenFilePath, goldenContent); | ||
if (!passed) { | ||
console.error(`TEST FAILED!`); | ||
console.error(` To update the golden file run: `); | ||
console.error( | ||
` bazel run --define ${update_var}=${update_val} ${process.env['BAZEL_TARGET']}`); | ||
} | ||
} | ||
return passed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright Google Inc. All Rights Reserved. | ||
# | ||
# Use of this source code is governed by an MIT-style license that can be | ||
# found in the LICENSE file at https://angular.io/license | ||
|
||
"""This test verifies that a set of top level symbols from a javascript file match a gold file. | ||
""" | ||
|
||
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_test") | ||
|
||
def js_expected_symbol_test(name, src, golden, **kwargs): | ||
all_data = [src, golden] | ||
all_data += [Label("//tools/symbol-extractor:lib")] | ||
entry_point = "angular/tools/symbol-extractor/cli.js" | ||
|
||
nodejs_test( | ||
name = name, | ||
data = all_data, | ||
entry_point = entry_point, | ||
templated_args = ["$(location %s)" % src, "$(location %s)" % golden], | ||
**kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as ts from 'typescript'; | ||
|
||
|
||
export interface Symbol { name: string; } | ||
|
||
export class SymbolExtractor { | ||
public actual: Symbol[]; | ||
|
||
static symbolSort(a: Symbol, b: Symbol): number { | ||
return a.name == b.name ? 0 : a.name < b.name ? -1 : 1; | ||
} | ||
|
||
static parse(path: string, contents: string): Symbol[] { | ||
const symbols: Symbol[] = []; | ||
const source: ts.SourceFile = ts.createSourceFile(path, contents, ts.ScriptTarget.Latest, true); | ||
let fnDepth = 0; | ||
function visitor(child: ts.Node) { | ||
switch (child.kind) { | ||
case ts.SyntaxKind.FunctionExpression: | ||
fnDepth++; | ||
if (fnDepth <= 1) { | ||
// Only go into function expression once for the outer closure. | ||
ts.forEachChild(child, visitor); | ||
} | ||
break; | ||
case ts.SyntaxKind.SourceFile: | ||
case ts.SyntaxKind.VariableStatement: | ||
case ts.SyntaxKind.VariableDeclarationList: | ||
case ts.SyntaxKind.ExpressionStatement: | ||
case ts.SyntaxKind.CallExpression: | ||
case ts.SyntaxKind.ParenthesizedExpression: | ||
case ts.SyntaxKind.Block: | ||
case ts.SyntaxKind.PrefixUnaryExpression: | ||
ts.forEachChild(child, visitor); | ||
break; | ||
case ts.SyntaxKind.VariableDeclaration: | ||
const varDecl = child as ts.VariableDeclaration; | ||
if (varDecl.initializer) { | ||
symbols.push({name: varDecl.name.getText()}); | ||
} | ||
break; | ||
case ts.SyntaxKind.FunctionDeclaration: | ||
const funcDecl = child as ts.FunctionDeclaration; | ||
funcDecl.name && symbols.push({name: funcDecl.name.getText()}); | ||
break; | ||
default: | ||
// Left for easier debugging. | ||
// console.log('###', ts.SyntaxKind[child.kind], child.getText()); | ||
} | ||
if (symbols.length && symbols[symbols.length - 1].name == 'type') { | ||
debugger; | ||
} | ||
} | ||
visitor(source); | ||
symbols.sort(SymbolExtractor.symbolSort); | ||
return symbols; | ||
} | ||
|
||
static diff(actual: Symbol[], expected: string|((Symbol | string)[])): {[name: string]: string} { | ||
if (typeof expected == 'string') { | ||
expected = JSON.parse(expected); | ||
} | ||
const diff: {[name: string]: ('missing' | 'extra')} = {}; | ||
(expected as(Symbol | string)[]).forEach((nameOrSymbol) => { | ||
diff[typeof nameOrSymbol == 'string' ? nameOrSymbol : nameOrSymbol.name] = 'missing'; | ||
}); | ||
|
||
actual.forEach((s) => { | ||
if (diff[s.name] === 'missing') { | ||
delete diff[s.name]; | ||
} else { | ||
diff[s.name] = 'extra'; | ||
} | ||
}); | ||
return diff; | ||
} | ||
|
||
constructor(private path: string, private contents: string) { | ||
this.actual = SymbolExtractor.parse(path, contents); | ||
} | ||
|
||
expect(expectedSymbols: (string|Symbol)[]) { | ||
expect(SymbolExtractor.diff(this.actual, expectedSymbols)).toEqual({}); | ||
} | ||
|
||
compareAndPrintError(goldenFilePath: string, expected: string|((Symbol | string)[])): boolean { | ||
let passed = true; | ||
const diff = SymbolExtractor.diff(this.actual, expected); | ||
Object.keys(diff).forEach((key) => { | ||
if (passed) { | ||
console.error(`Expected symbols in '${this.path}' did not match gold file.`); | ||
passed = false; | ||
} | ||
console.error(` Symbol: ${key} => ${diff[key]}`); | ||
}); | ||
|
||
return passed; | ||
} | ||
} | ||
|
||
function toSymbol(v: string | Symbol): Symbol { | ||
return typeof v == 'string' ? {'name': v} : v as Symbol; | ||
} | ||
|
||
function toName(symbol: Symbol): string { | ||
return symbol.name; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buildifier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅