From b3582488d2d009cbf1fdabfe0996509941d2be3e Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 23 Aug 2024 18:26:13 +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 | 28 +++++++++++--- ...den.ts => critters-license-test-accept.ts} | 0 .../ssr/test/npm_package/package_spec.ts | 38 +------------------ yarn.lock | 12 +----- 6 files changed, 25 insertions(+), 56 deletions(-) rename packages/angular/ssr/test/npm_package/{update-package-golden.ts => critters-license-test-accept.ts} (100%) 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..2e75b953897b 100644 --- a/packages/angular/ssr/test/npm_package/BUILD.bazel +++ b/packages/angular/ssr/test/npm_package/BUILD.bazel @@ -1,3 +1,4 @@ +load("@bazel_skylib//rules:diff_test.bzl", "diff_test") load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") 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,38 @@ jasmine_node_test( name = "test", srcs = [":unit_test_lib"], data = [ - "THIRD_PARTY_LICENSES.txt.golden", "//packages/angular/ssr:npm_package", "@npm//diff", ], ) +genrule( + name = "critters_license_file", + srcs = [ + "//packages/angular/ssr:npm_package", + ], + 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 = "\n\nTo accept the new golden file, execute:\nyarn bazel run //packages/angular/ssr/test/npm_package:critters_license_test.accept\n", + file1 = ":THIRD_PARTY_LICENSES.txt.golden", + file2 = ":critters_license_file", +) + nodejs_binary( - name = "test.accept", + name = "critters_license_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", + entry_point = ":critters-license-test-accept.ts", ) diff --git a/packages/angular/ssr/test/npm_package/update-package-golden.ts b/packages/angular/ssr/test/npm_package/critters-license-test-accept.ts similarity index 100% rename from packages/angular/ssr/test/npm_package/update-package-golden.ts rename to packages/angular/ssr/test/npm_package/critters-license-test-accept.ts diff --git a/packages/angular/ssr/test/npm_package/package_spec.ts b/packages/angular/ssr/test/npm_package/package_spec.ts index 2aadbe6540cc..23206a2a8f29 100644 --- a/packages/angular/ssr/test/npm_package/package_spec.ts +++ b/packages/angular/ssr/test/npm_package/package_spec.ts @@ -6,15 +6,10 @@ * 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'; +import { ANGULAR_SSR_PACKAGE_PATH, CRITTERS_ACTUAL_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 () => { @@ -27,36 +22,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/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