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 24, 2024
1 parent faee5c6 commit 96693b7
Show file tree
Hide file tree
Showing 12 changed files with 529 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
35 changes: 35 additions & 0 deletions packages/angular/ssr/test/npm_package/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 = glob(["**/*.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 = ":update-package-golden.ts",
)
350 changes: 350 additions & 0 deletions packages/angular/ssr/test/npm_package/THIRD_PARTY_LICENSES.txt.golden

Large diffs are not rendered by default.

62 changes: 62 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,62 @@
/**
* @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 { createPatch } from 'diff';
import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import {
ANGULAR_SSR_PACKAGE_PATH,
CRITTERS_ACTUAL_LICENSE_FILE_PATH,
CRITTERS_GOLDEN_LICENSE_FILE_PATH,
} from './utils';

describe('NPM Package Tests', () => {
it('should not include the contents of third_party/critters/index.js in the FESM bundle', async () => {
const fesmFilePath = join(ANGULAR_SSR_PACKAGE_PATH, 'fesm2022/ssr.mjs');
const fesmContent = await readFile(fesmFilePath, 'utf-8');
expect(fesmContent).toContain(`import Critters from '../third_party/critters/index.js'`);
});

describe('third_party/critters/THIRD_PARTY_LICENSES.txt', () => {
it('should exist', () => {
expect(existsSync(CRITTERS_ACTUAL_LICENSE_FILE_PATH)).toBe(true);
});

it('should match the expected golden file', async () => {
const [expectedContent, actualContent] = await Promise.all([
readFile(CRITTERS_GOLDEN_LICENSE_FILE_PATH, 'utf-8'),
readFile(CRITTERS_ACTUAL_LICENSE_FILE_PATH, 'utf-8'),
]);

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

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

const errorMessage = `The content of the actual license file differs from the expected golden reference.
Diff:
${patch}
To accept the new golden file, execute:
yarn bazel run ${process.env['BAZEL_TARGET']}.accept
`;

const error = new Error(errorMessage);
error.stack = error.stack?.replace(` Diff:\n ${patch}`, '');
throw error;
});
});
});
15 changes: 15 additions & 0 deletions packages/angular/ssr/test/npm_package/update-package-golden.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @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 { readFileSync, writeFileSync } from 'node:fs';
import { CRITTERS_ACTUAL_LICENSE_FILE_PATH, CRITTERS_GOLDEN_LICENSE_FILE_PATH } from './utils';

/**
* Updates the golden reference license file.
*/
writeFileSync(CRITTERS_GOLDEN_LICENSE_FILE_PATH, readFileSync(CRITTERS_ACTUAL_LICENSE_FILE_PATH));
32 changes: 32 additions & 0 deletions packages/angular/ssr/test/npm_package/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @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 { dirname, join } from 'node:path';

/**
* Resolve paths for the Critters license file and the golden reference file.
*/
export const ANGULAR_SSR_PACKAGE_PATH = dirname(
runfiles.resolve('angular_cli/packages/angular/ssr/npm_package/package.json'),
);

/**
* Path to the actual license file for the Critters library.
* This file is located in the `third_party/critters` directory of the Angular CLI npm package.
*/
export const CRITTERS_ACTUAL_LICENSE_FILE_PATH = join(
ANGULAR_SSR_PACKAGE_PATH,
'third_party/critters/THIRD_PARTY_LICENSES.txt',
);

/**
* Path to the golden reference license file for the Critters library.
* This file is used as a reference for comparison and is located in the same directory as this script.
*/
export const CRITTERS_GOLDEN_LICENSE_FILE_PATH = join(__dirname, 'THIRD_PARTY_LICENSES.txt.golden');
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.trim().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/estree@npm:*, @types/estree@npm:1.0.5, @types/estree@npm:^1.0.0, @types/estree@npm:^1.0.5":
version: 1.0.5
resolution: "@types/estree@npm:1.0.5"
Expand Down Expand Up @@ -8549,7 +8561,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 96693b7

Please sign in to comment.