2018-02-28 09:51:40 -05:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2018-03-22 13:37:43 -04:00
|
|
|
import {createPatch} from 'diff';
|
2018-02-28 09:51:40 -05:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
|
2018-03-15 21:04:34 -04:00
|
|
|
const TEST_DIR = path.resolve(path.join('packages', 'bazel', 'test', 'ng_package'));
|
2018-02-28 09:51:40 -05:00
|
|
|
|
2018-03-15 21:04:34 -04:00
|
|
|
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;
|
|
|
|
}
|
2018-02-28 09:51:40 -05:00
|
|
|
|
2018-03-15 21:04:34 -04:00
|
|
|
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;
|
|
|
|
}
|
2018-03-22 13:37:43 -04:00
|
|
|
|
|
|
|
// Compute the patch and strip the header
|
|
|
|
let patch =
|
|
|
|
createPatch(goldenFile, expected, actual, 'Golden file', 'Generated file', {context: 5});
|
|
|
|
const endOfHeader = patch.indexOf('\n', patch.indexOf('\n') + 1) + 1;
|
|
|
|
patch = patch.substring(endOfHeader);
|
|
|
|
|
2018-03-15 21:04:34 -04:00
|
|
|
fail(`example ng_package differs from golden file
|
2018-03-22 13:37:43 -04:00
|
|
|
Diff:
|
|
|
|
${patch}
|
|
|
|
|
2018-03-15 21:04:34 -04:00
|
|
|
Accept the new golden file:
|
|
|
|
bazel run ${process.env['BAZEL_TARGET']}.accept
|
2018-03-22 13:37:43 -04:00
|
|
|
`);
|
2018-03-15 21:04:34 -04:00
|
|
|
});
|
2018-02-28 09:51:40 -05:00
|
|
|
});
|
2018-03-15 21:04:34 -04:00
|
|
|
}
|