From 1c70f470dd59c1732152a9a7a5791a3a751bcf7d Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 23 Aug 2024 21:16:36 +0000 Subject: [PATCH] build: use Bazel `diff_test` to compare file differences Leverage the built-in `diff_test` feature from Bazel to check for file changes. For details, see: https://github.com/bazelbuild/bazel-skylib/blob/main/docs/diff_test_doc.md --- package.json | 2 - packages/angular/ssr/package.json | 1 - .../angular/ssr/test/npm_package/BUILD.bazel | 47 +++++++++++---- .../ssr/test/npm_package/package_spec.ts | 60 +++++++------------ .../test/npm_package/update-package-golden.ts | 15 ----- .../angular/ssr/test/npm_package/utils.ts | 32 ---------- yarn.lock | 12 +--- 7 files changed, 59 insertions(+), 110 deletions(-) delete mode 100644 packages/angular/ssr/test/npm_package/update-package-golden.ts delete mode 100644 packages/angular/ssr/test/npm_package/utils.ts diff --git a/package.json b/package.json index 8974f67de22a..5e380d55cd2a 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,6 @@ "@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", @@ -132,7 +131,6 @@ "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", diff --git a/packages/angular/ssr/package.json b/packages/angular/ssr/package.json index 811944267e97..747189433350 100644 --- a/packages/angular/ssr/package.json +++ b/packages/angular/ssr/package.json @@ -29,7 +29,6 @@ "@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", diff --git a/packages/angular/ssr/test/npm_package/BUILD.bazel b/packages/angular/ssr/test/npm_package/BUILD.bazel index bd8f302e5019..48a239493667 100644 --- a/packages/angular/ssr/test/npm_package/BUILD.bazel +++ b/packages/angular/ssr/test/npm_package/BUILD.bazel @@ -1,4 +1,5 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") +load("@bazel_skylib//rules:diff_test.bzl", "diff_test") +load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test") load("//tools:defaults.bzl", "ts_library") @@ -8,7 +9,6 @@ ts_library( srcs = glob(["**/*.ts"]), deps = [ "@npm//@bazel/runfiles", - "@npm//@types/diff", ], ) @@ -16,20 +16,43 @@ 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", +genrule( + name = "critters_license_file", + srcs = [ "//packages/angular/ssr:npm_package", - "@npm//diff", ], - entry_point = ":update-package-golden.ts", + outs = [ + "THIRD_PARTY_LICENSES.txt", + ], + cmd = """ + cp $(location //packages/angular/ssr:npm_package)/third_party/critters/THIRD_PARTY_LICENSES.txt $(location :THIRD_PARTY_LICENSES.txt) + """, +) + +diff_test( + name = "critters_license_test", + failure_message = """ + + To accept the new golden file, execute: + yarn bazel run //packages/angular/ssr/test/npm_package:critters_license_test.accept + """, + file1 = ":THIRD_PARTY_LICENSES.txt.golden", + file2 = ":critters_license_file", +) + +write_file( + name = "critters_license_test.accept", + out = "critters_license_file_accept.sh", + content = + [ + "#!/usr/bin/env bash", + "cd ${BUILD_WORKSPACE_DIRECTORY}", + "yarn bazel build //packages/angular/ssr:npm_package", + "cp -fv dist/bin/packages/angular/ssr/npm_package/third_party/critters/THIRD_PARTY_LICENSES.txt packages/angular/ssr/test/npm_package/THIRD_PARTY_LICENSES.txt.golden", + ], + is_executable = True, ) diff --git a/packages/angular/ssr/test/npm_package/package_spec.ts b/packages/angular/ssr/test/npm_package/package_spec.ts index 2aadbe6540cc..c4f9fad7f402 100644 --- a/packages/angular/ssr/test/npm_package/package_spec.ts +++ b/packages/angular/ssr/test/npm_package/package_spec.ts @@ -6,15 +6,32 @@ * found in the LICENSE file at https://angular.dev/license */ -import { createPatch } from 'diff'; +import { runfiles } from '@bazel/runfiles'; import { existsSync } from 'node:fs'; import { readFile } from 'node:fs/promises'; -import { join } from 'node:path'; -import { +import { dirname, join } from 'node:path'; + +/** + * Resolve paths for the Critters license file and the golden reference file. + */ +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. + */ +const CRITTERS_ACTUAL_LICENSE_FILE_PATH = join( ANGULAR_SSR_PACKAGE_PATH, - CRITTERS_ACTUAL_LICENSE_FILE_PATH, - CRITTERS_GOLDEN_LICENSE_FILE_PATH, -} from './utils'; + '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. + */ +const CRITTERS_GOLDEN_LICENSE_FILE_PATH = join(__dirname, 'THIRD_PARTY_LICENSES.txt.golden'); describe('NPM Package Tests', () => { it('should not include the contents of third_party/critters/index.js in the FESM bundle', async () => { @@ -27,36 +44,5 @@ describe('NPM Package Tests', () => { 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; - }); }); }); diff --git a/packages/angular/ssr/test/npm_package/update-package-golden.ts b/packages/angular/ssr/test/npm_package/update-package-golden.ts deleted file mode 100644 index 0754d4d89249..000000000000 --- a/packages/angular/ssr/test/npm_package/update-package-golden.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * @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)); diff --git a/packages/angular/ssr/test/npm_package/utils.ts b/packages/angular/ssr/test/npm_package/utils.ts deleted file mode 100644 index fc1a3adfc518..000000000000 --- a/packages/angular/ssr/test/npm_package/utils.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * @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'); diff --git a/yarn.lock b/yarn.lock index 4e6d02c37ab2..2f7f0984ca0e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -668,7 +668,6 @@ __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" @@ -708,7 +707,6 @@ __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" @@ -969,7 +967,6 @@ __metadata: "@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: @@ -4871,13 +4868,6 @@ __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" @@ -8561,7 +8551,7 @@ __metadata: languageName: node linkType: hard -"diff@npm:^5.0.0, diff@npm:^5.1.0, diff@npm:^5.2.0": +"diff@npm:^5.0.0, diff@npm:^5.1.0": version: 5.2.0 resolution: "diff@npm:5.2.0" checksum: 10c0/aed0941f206fe261ecb258dc8d0ceea8abbde3ace5827518ff8d302f0fc9cc81ce116c4d8f379151171336caf0516b79e01abdc1ed1201b6440d895a66689eb4