-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: add test to ensure compatibility with ng-linker #22351
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
Merged
andrewseguin
merged 3 commits into
angular:master
from
devversion:build/add-angular-linker-compability-test
May 17, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
load("@build_bazel_rules_nodejs//:index.bzl", "js_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
# JavaScript library that exposes a script for retrieving all NPM packages | ||
# available in the runfiles of an action. | ||
js_library( | ||
name = "npm-packages-from-runfiles", | ||
srcs = ["npm-packages-from-runfiles.js"], | ||
) |
This file contains hidden or 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,23 @@ | ||
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test") | ||
|
||
# Test which ensures that specified NPM packages can be transformed from their partial | ||
# declarations to definitions using the `@angular/compiler-cli` linker babel plugin. | ||
nodejs_test( | ||
devversion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name = "linker", | ||
data = [ | ||
"//integration:npm-packages-from-runfiles", | ||
"//src/cdk:npm_package", | ||
"//src/cdk-experimental:npm_package", | ||
"//src/google-maps:npm_package", | ||
"//src/material:npm_package", | ||
"//src/material-experimental:npm_package", | ||
"//src/youtube-player:npm_package", | ||
"@npm//@angular/compiler-cli", | ||
"@npm//@babel/core", | ||
"@npm//@babel/traverse", | ||
"@npm//chalk", | ||
"@npm//glob", | ||
], | ||
entry_point = "link-packages-test.js", | ||
tags = ["partial-compilation-integration"], | ||
) |
This file contains hidden or 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,100 @@ | ||
/** | ||
* Test that collects all partially built NPM packages and links their Angular | ||
* declarations to the corresponding definitions. | ||
*/ | ||
|
||
const {NodeJSFileSystem} = require('@angular/compiler-cli/src/ngtsc/file_system'); | ||
const {ConsoleLogger, LogLevel} = require('@angular/compiler-cli/src/ngtsc/logging'); | ||
const {createEs2015LinkerPlugin} = require('@angular/compiler-cli/linker/babel'); | ||
const {getNpmPackagesFromRunfiles} = require('../npm-packages-from-runfiles'); | ||
const {readFileSync} = require('fs'); | ||
const {join} = require('path'); | ||
const babel = require('@babel/core'); | ||
const {default: traverse} = require('@babel/traverse'); | ||
const glob = require('glob'); | ||
const chalk = require('chalk'); | ||
|
||
/** File system used by the Angular linker plugin. */ | ||
const fileSystem = new NodeJSFileSystem(); | ||
/** Logger used by the Angular linker plugin. */ | ||
const logger = new ConsoleLogger(LogLevel.info); | ||
/** List of NPM packages available in the Bazel runfiles. */ | ||
const npmPackages = getNpmPackagesFromRunfiles(); | ||
/** Whether any package could not be linked successfully. */ | ||
let failedPackages = false; | ||
|
||
// Iterate through all determined NPM packages and ensure that entry point | ||
// files can be processed successfully by the Angular linker. | ||
for (const pkg of npmPackages) { | ||
const {failures, passedFiles} = testPackage(pkg); | ||
|
||
console.info(chalk.cyan(`------- Package: @angular/${pkg.name} -------`)); | ||
console.info(`Passed files: ${passedFiles.length}`); | ||
console.info(`Failed files: ${failures.length}`); | ||
|
||
if (failures.length > 0) { | ||
failures.forEach(({debugFileName, error}) => { | ||
console.error(` • ${chalk.yellow(debugFileName)}: ${error}`); | ||
}); | ||
failedPackages = true; | ||
} | ||
|
||
console.info('-------------------------------------'); | ||
console.info(); | ||
} | ||
|
||
if (failedPackages) { | ||
console.error(chalk.red(`✘ Not all packages could be linked successfully. See errors above.`)); | ||
// If there are failures, exit the process with a non-zero exit code. Bazel | ||
// uses exit code `3` to indicate non-fatal test failures. | ||
process.exitCode = 3; | ||
} else { | ||
console.info(chalk.green('✓ All packages have been successfully linked.')); | ||
} | ||
|
||
/** | ||
* Tests the specified package against the Angular linker plugin. | ||
* @param pkg Package being tested. | ||
* @returns An object containing linker failures and passed files. | ||
*/ | ||
function testPackage(pkg) { | ||
const entryPointFesmFiles = glob.sync(`fesm2015/**/*.js`, {cwd: pkg.pkgPath}); | ||
const passedFiles = []; | ||
const failures = []; | ||
|
||
// Iterate through each entry point and confirm that all partial declarations can be linked | ||
// to their corresponding Angular definitions without errors. | ||
for (const fesmFileName of entryPointFesmFiles) { | ||
const diskFilePath = join(pkg.pkgPath, fesmFileName); | ||
const debugFileName = join(pkg.name, fesmFileName); | ||
const fileContent = readFileSync(diskFilePath, 'utf8'); | ||
const linkerPlugin = createEs2015LinkerPlugin({fileSystem, logger}); | ||
|
||
// Babel throws errors if the transformation fails. We catch these so that we | ||
// can print incompatible entry points with their errors at the end. | ||
try { | ||
devversion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const {ast} = babel.transformSync(fileContent, { | ||
ast: true, | ||
filename: diskFilePath, | ||
filenameRelative: debugFileName, | ||
plugins: [linkerPlugin] | ||
}); | ||
|
||
// Naively check if there are any Angular declarations left that haven't been linked. | ||
traverse(ast, { | ||
petebacondarwin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Identifier: (astPath) => { | ||
if (astPath.node.name.startsWith('ɵɵngDeclare')) { | ||
throw astPath.buildCodeFrameError( | ||
'Found Angular declaration that has not been linked.', Error); | ||
} | ||
} | ||
}); | ||
|
||
passedFiles.push(debugFileName); | ||
} catch (error) { | ||
failures.push({debugFileName, error}); | ||
} | ||
} | ||
|
||
return {passedFiles, failures} | ||
} |
This file contains hidden or 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,36 @@ | ||
/** | ||
* Collection of common logic for dealing with Bazel runfiles within | ||
* integration tests. | ||
*/ | ||
|
||
const {relative, sep, join} = require('path'); | ||
const {readdirSync, readFileSync, existsSync} = require('fs'); | ||
|
||
/** | ||
* Gets all built Angular NPM package artifacts by querying the Bazel runfiles. | ||
* In case there is a runfiles manifest (e.g. on Windows), the packages are resolved | ||
* through the manifest because the runfiles are not symlinked and cannot be searched | ||
* within the real filesystem. | ||
* TODO: Simplify if Bazel on Windows uses runfile symlinking. | ||
*/ | ||
exports.getNpmPackagesFromRunfiles = function() { | ||
// Path to the Bazel runfiles manifest if present. This file is present if runfiles are | ||
// not symlinked into the runfiles directory. | ||
const runfilesManifestPath = process.env.RUNFILES_MANIFEST_FILE; | ||
const workspacePath = 'angular_material/src'; | ||
if (!runfilesManifestPath) { | ||
const packageRunfilesDir = join(process.env.RUNFILES, workspacePath); | ||
return readdirSync(packageRunfilesDir) | ||
.map(name => ({name, pkgPath: join(packageRunfilesDir, name, 'npm_package/')})) | ||
.filter(({pkgPath}) => existsSync(pkgPath)); | ||
} | ||
const workspaceManifestPathRegex = new RegExp(`^${workspacePath}/[\\w-]+/npm_package$`); | ||
return readFileSync(runfilesManifestPath, 'utf8') | ||
.split('\n') | ||
.map(mapping => mapping.split(' ')) | ||
.filter(([runfilePath]) => runfilePath.match(workspaceManifestPathRegex)) | ||
.map(([runfilePath, realPath]) => ({ | ||
name: relative(workspacePath, runfilePath).split(sep)[0], | ||
pkgPath: realPath, | ||
})); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.