Skip to content

Commit

Permalink
build: add function to validate and update 3rd party license files
Browse files Browse the repository at this point in the history
- Implemented functionality to compare and update third-party license files
- Added handling for '--accept' argument to update the golden license file
- Included tests to ensure the Critters license file matches the golden reference

This update ensures license file consistency and provides an easy way to update the reference file when necessary.
  • Loading branch information
alan-agius4 committed Aug 23, 2024
1 parent 5d45ecd commit 17a0038
Show file tree
Hide file tree
Showing 10 changed files with 500 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
/CONTRIBUTING.md
.yarn/
dist/
third_party/
third_party/github.com/
/tests/legacy-cli/e2e/assets/
/tools/test/*.json
/tools/test/*.json
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@bazel/buildifier": "7.1.2",
"@bazel/concatjs": "patch:@bazel/concatjs@npm%3A5.8.1#~/.yarn/patches/@bazel-concatjs-npm-5.8.1-1bf81df846.patch",
"@bazel/jasmine": "patch:@bazel/jasmine@npm%3A5.8.1#~/.yarn/patches/@bazel-jasmine-npm-5.8.1-3370fee155.patch",
"@bazel/runfiles": "^5.8.1",
"@discoveryjs/json-ext": "0.6.1",
"@inquirer/confirm": "3.1.22",
"@inquirer/prompts": "5.3.8",
Expand All @@ -91,6 +92,7 @@
"@rollup/plugin-node-resolve": "^13.0.5",
"@types/babel__core": "7.20.5",
"@types/browser-sync": "^2.27.0",
"@types/diff": "^5.2.1",
"@types/express": "^4.16.0",
"@types/http-proxy": "^1.17.4",
"@types/ini": "^4.0.0",
Expand Down Expand Up @@ -130,6 +132,7 @@
"critters": "0.0.24",
"css-loader": "7.1.2",
"debug": "^4.1.1",
"diff": "^5.2.0",
"esbuild": "0.23.1",
"esbuild-wasm": "0.23.1",
"eslint": "8.57.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"@angular/platform-browser": "19.0.0-next.1",
"@angular/platform-server": "19.0.0-next.1",
"@angular/router": "19.0.0-next.1",
"@bazel/runfiles": "^5.8.1",
"diff": "^5.2.0",
"zone.js": "^0.15.0"
},
"schematics": "./schematics/collection.json",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/ssr/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ts_library(
testonly = True,
srcs = glob(
include = ["**/*_spec.ts"],
exclude = ESM_TESTS,
exclude = ESM_TESTS + ["npm_package/**"],
),
deps = [
"//packages/angular/ssr",
Expand Down
36 changes: 36 additions & 0 deletions packages/angular/ssr/test/npm_package/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test")
load("//tools:defaults.bzl", "ts_library")

ts_library(
name = "unit_test_lib",
testonly = True,
srcs = ["package_spec.ts"],
deps = [
"@npm//@bazel/runfiles",
"@npm//@types/diff",
],
)

jasmine_node_test(
name = "test",
srcs = [":unit_test_lib"],
data = [
"THIRD_PARTY_LICENSES.txt.golden",
"//packages/angular/ssr:npm_package",
"@npm//diff",
],
)

nodejs_binary(
name = "test.accept",
testonly = True,
data = [
"THIRD_PARTY_LICENSES.txt.golden",
":unit_test_lib",
"//packages/angular/ssr:npm_package",
"@npm//diff",
],
entry_point = ":package_spec.ts",
templated_args = ["--accept"],
)
365 changes: 365 additions & 0 deletions packages/angular/ssr/test/npm_package/THIRD_PARTY_LICENSES.txt.golden

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions packages/angular/ssr/test/npm_package/package_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license
* Copyright Google LLC 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.dev/license
*/

import { runfiles } from '@bazel/runfiles';
import { createPatch } from 'diff';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';

// Resolve paths for the Critters license file and the golden reference file
const angularSsrPackagePath = dirname(
runfiles.resolve('angular_cli/packages/angular/ssr/npm_package/package.json'),
);
const crittersActualLicenseFilePath = join(
angularSsrPackagePath,
'third_party/critters/THIRD_PARTY_LICENSES.txt',
);
const crittersGoldenLicenseFilePath = join(__dirname, 'THIRD_PARTY_LICENSES.txt.golden');

// If '--accept' argument is provided, update the golden reference file
if (process.argv.slice(2).includes('--accept')) {
writeFileSync(crittersGoldenLicenseFilePath, readFileSync(crittersActualLicenseFilePath));
} else {
describe('NPM Package', () => {
it('Should have the third-party licenses file', () => {
expect(existsSync(crittersActualLicenseFilePath)).toBe(true);
});

it('Critters third-party licenses should match the golden reference', async () => {
const [expectedContent, actualContent] = await Promise.all([
readFile(crittersGoldenLicenseFilePath, 'utf-8'),
readFile(crittersActualLicenseFilePath, 'utf-8'),
]);

if (expectedContent.trim() === actualContent.trim()) {
return;
}

const patch = createPatch(
crittersGoldenLicenseFilePath,
expectedContent,
actualContent,
'Golden License File',
'Current License File',
{ context: 5 },
);

const failure = new Error(`Actual contents differ from the golden reference
Diff:
${patch}
To accept the new golden file, run:
yarn bazel run ${process.env['BAZEL_TARGET']}.accept
`);

failure.stack = failure.stack?.replace(` Diff:\n ${patch}`, '');
throw failure;
});
});
}
19 changes: 13 additions & 6 deletions packages/angular/ssr/third_party/critters/esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ const EXTRACTION_FILE_SEPARATOR = '-'.repeat(80) + '\n';
* @returns A string containing the content of the output licenses file.
*/
async function extractLicenses(metafile) {
let extractedLicenseContent = `${EXTRACTION_FILE_HEADER}\n${EXTRACTION_FILE_SEPARATOR}`;

const seenPaths = new Set();
const seenPackages = new Set();
const extractedLicenses = {};

for (const entry of Object.values(metafile.outputs)) {
for (const [inputPath, { bytesInOutput }] of Object.entries(entry.inputs)) {
Expand Down Expand Up @@ -186,12 +185,20 @@ async function extractLicenses(metafile) {
}

// Generate the package's license entry in the output content
extractedLicenseContent += `Package: ${packageJson.name}\n`;
extractedLicenseContent += `License: ${JSON.stringify(packageJson.license, null, 2)}\n`;
extractedLicenseContent += `\n${licenseText}\n`;
let extractedLicenseContent = `Package: ${packageJson.name}\n`;
extractedLicenseContent += `License: ${JSON.stringify(packageJson.license, undefined, 2)}\n`;
extractedLicenseContent += `\n${licenseText.replace(/\r?\n/g, '\n')}\n`;
extractedLicenseContent += EXTRACTION_FILE_SEPARATOR;

extractedLicenses[packageJson.name] = extractedLicenseContent;
}
}

return extractedLicenseContent;
// Get the keys of the object and sort them and etract and join the values corresponding to the sorted keys
const joinedLicenseContent = Object.keys(extractedLicenses)
.sort()
.map((pkgName) => extractedLicenses[pkgName])
.join('');

return `${EXTRACTION_FILE_HEADER}\n${EXTRACTION_FILE_SEPARATOR}${joinedLicenseContent}`;
}
2 changes: 1 addition & 1 deletion packages/angular/ssr/third_party/critters/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* found in the LICENSE file at https://angular.io/license
*/

export {default} from 'critters';
export { default } from 'critters';
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ __metadata:
"@bazel/buildifier": "npm:7.1.2"
"@bazel/concatjs": "patch:@bazel/concatjs@npm%3A5.8.1#~/.yarn/patches/@bazel-concatjs-npm-5.8.1-1bf81df846.patch"
"@bazel/jasmine": "patch:@bazel/jasmine@npm%3A5.8.1#~/.yarn/patches/@bazel-jasmine-npm-5.8.1-3370fee155.patch"
"@bazel/runfiles": "npm:^5.8.1"
"@discoveryjs/json-ext": "npm:0.6.1"
"@inquirer/confirm": "npm:3.1.22"
"@inquirer/prompts": "npm:5.3.8"
Expand All @@ -667,6 +668,7 @@ __metadata:
"@rollup/plugin-node-resolve": "npm:^13.0.5"
"@types/babel__core": "npm:7.20.5"
"@types/browser-sync": "npm:^2.27.0"
"@types/diff": "npm:^5.2.1"
"@types/express": "npm:^4.16.0"
"@types/http-proxy": "npm:^1.17.4"
"@types/ini": "npm:^4.0.0"
Expand Down Expand Up @@ -706,6 +708,7 @@ __metadata:
critters: "npm:0.0.24"
css-loader: "npm:7.1.2"
debug: "npm:^4.1.1"
diff: "npm:^5.2.0"
esbuild: "npm:0.23.1"
esbuild-wasm: "npm:0.23.1"
eslint: "npm:8.57.0"
Expand Down Expand Up @@ -965,6 +968,8 @@ __metadata:
"@angular/platform-browser": "npm:19.0.0-next.1"
"@angular/platform-server": "npm:19.0.0-next.1"
"@angular/router": "npm:19.0.0-next.1"
"@bazel/runfiles": "npm:^5.8.1"
diff: "npm:^5.2.0"
tslib: "npm:^2.3.0"
zone.js: "npm:^0.15.0"
peerDependencies:
Expand Down Expand Up @@ -4866,6 +4871,13 @@ __metadata:
languageName: node
linkType: hard

"@types/diff@npm:^5.2.1":
version: 5.2.1
resolution: "@types/diff@npm:5.2.1"
checksum: 10c0/62dcab32197ac67f212939cdd79aa3953327a482bec55c6a38ad9de8a0662a9f920b59504609a322fc242593bd9afb3d2704702f4bc98087a13171234b952361
languageName: node
linkType: hard

"@types/eslint-scope@npm:^3.7.3":
version: 3.7.7
resolution: "@types/eslint-scope@npm:3.7.7"
Expand Down Expand Up @@ -8569,7 +8581,7 @@ __metadata:
languageName: node
linkType: hard

"diff@npm:^5.0.0, diff@npm:^5.1.0":
"diff@npm:^5.0.0, diff@npm:^5.1.0, diff@npm:^5.2.0":
version: 5.2.0
resolution: "diff@npm:5.2.0"
checksum: 10c0/aed0941f206fe261ecb258dc8d0ceea8abbde3ace5827518ff8d302f0fc9cc81ce116c4d8f379151171336caf0516b79e01abdc1ed1201b6440d895a66689eb4
Expand Down

0 comments on commit 17a0038

Please sign in to comment.