angular/packages/bazel/test/ng_package/example_package.spec.ts
Alex Eagle 689f351092 build: expose flatModuleOutFile option on ng_module (#22814)
This lets projects like Material change ng_package "bundle index" files to non-conflicting paths

Currently packages like @angular/core ship with the generated metadata
in a path like 'core.js' which overwrites one of the inputs.

Angular material puts the generated file in a path like 'index.js'

Either way these files generated by ng_module rules have the potential
to collide with inputs given by the user, which results in an error.

Instead, give users the freedom to choose a different non-conflicting name.

Also this refactors the ng_package rule, removing the redundant
secondary_entry_points attribute.

Instead, we assume that any ng_module in the deps with a module_name
attribute is a secondary entry point.

PR Close #22814
2018-03-20 13:28:57 -07:00

60 lines
1.7 KiB
TypeScript

/**
* @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';
const TEST_DIR = path.resolve(path.join('packages', 'bazel', 'test', 'ng_package'));
function listDirectories(p: string, depth = 0) {
const result: string[] = [];
if (fs.statSync(p).isDirectory()) {
fs.readdirSync(p).forEach(f => {
result.push(
' '.repeat(depth) + path.join(p, f), ...listDirectories(path.join(p, f), depth + 1));
});
}
return result;
}
function catFiles(p: string) {
const result: string[] = [];
if (fs.statSync(p).isDirectory()) {
fs.readdirSync(p).forEach(dir => { result.push(...catFiles(path.join(p, dir))); });
} else {
result.push(`--- ${p} ---`, '', fs.readFileSync(p, 'utf-8'), '');
}
return result;
}
const goldenFile = path.join(TEST_DIR, 'example_package.golden');
process.chdir(path.join(TEST_DIR, 'example/npm_package'));
const actual = [...listDirectories('.'), ...catFiles('.')].join('\n').replace(
/bazel-out\/.*\/bin/g, 'bazel-bin');
const expected = fs.readFileSync(goldenFile, 'utf-8');
if (require.main === module) {
const args = process.argv.slice(2);
if (args[0] === '--accept') {
fs.writeFileSync(require.resolve(goldenFile), actual, 'utf-8');
}
} else {
describe('example ng_package', () => {
it('should match golden file', () => {
if (actual === expected) {
return;
}
fail(`example ng_package differs from golden file
Accept the new golden file:
bazel run ${process.env['BAZEL_TARGET']}.accept
`);
});
});
}