Skip to content

Commit ad65c3f

Browse files
committed
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
1 parent 96693b7 commit ad65c3f

File tree

7 files changed

+59
-110
lines changed

7 files changed

+59
-110
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
"@rollup/plugin-node-resolve": "^13.0.5",
9393
"@types/babel__core": "7.20.5",
9494
"@types/browser-sync": "^2.27.0",
95-
"@types/diff": "^5.2.1",
9695
"@types/express": "^4.16.0",
9796
"@types/http-proxy": "^1.17.4",
9897
"@types/ini": "^4.0.0",
@@ -132,7 +131,6 @@
132131
"critters": "0.0.24",
133132
"css-loader": "7.1.2",
134133
"debug": "^4.1.1",
135-
"diff": "^5.2.0",
136134
"esbuild": "0.23.1",
137135
"esbuild-wasm": "0.23.1",
138136
"eslint": "8.57.0",

packages/angular/ssr/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"@angular/platform-server": "19.0.0-next.1",
3030
"@angular/router": "19.0.0-next.1",
3131
"@bazel/runfiles": "^5.8.1",
32-
"diff": "^5.2.0",
3332
"zone.js": "^0.15.0"
3433
},
3534
"schematics": "./schematics/collection.json",
Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
1+
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
2+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
23
load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test")
34
load("//tools:defaults.bzl", "ts_library")
45

@@ -8,28 +9,50 @@ ts_library(
89
srcs = glob(["**/*.ts"]),
910
deps = [
1011
"@npm//@bazel/runfiles",
11-
"@npm//@types/diff",
1212
],
1313
)
1414

1515
jasmine_node_test(
1616
name = "test",
1717
srcs = [":unit_test_lib"],
1818
data = [
19-
"THIRD_PARTY_LICENSES.txt.golden",
2019
"//packages/angular/ssr:npm_package",
21-
"@npm//diff",
2220
],
2321
)
2422

25-
nodejs_binary(
26-
name = "test.accept",
27-
testonly = True,
28-
data = [
29-
"THIRD_PARTY_LICENSES.txt.golden",
30-
":unit_test_lib",
23+
genrule(
24+
name = "critters_license_file",
25+
srcs = [
3126
"//packages/angular/ssr:npm_package",
32-
"@npm//diff",
3327
],
34-
entry_point = ":update-package-golden.ts",
28+
outs = [
29+
"THIRD_PARTY_LICENSES.txt",
30+
],
31+
cmd = """
32+
cp $(location //packages/angular/ssr:npm_package)/third_party/critters/THIRD_PARTY_LICENSES.txt $(location :THIRD_PARTY_LICENSES.txt)
33+
""",
34+
)
35+
36+
diff_test(
37+
name = "critters_license_test",
38+
failure_message = """
39+
40+
To accept the new golden file, execute:
41+
yarn bazel run //packages/angular/ssr/test/npm_package:critters_license_test.accept
42+
""",
43+
file1 = ":THIRD_PARTY_LICENSES.txt.golden",
44+
file2 = ":critters_license_file",
45+
)
46+
47+
write_file(
48+
name = "critters_license_test.accept",
49+
out = "critters_license_file_accept.sh",
50+
content =
51+
[
52+
"#!/usr/bin/env bash",
53+
"cd ${BUILD_WORKSPACE_DIRECTORY}",
54+
"yarn bazel build //packages/angular/ssr:npm_package",
55+
"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",
56+
],
57+
is_executable = True,
3558
)

packages/angular/ssr/test/npm_package/package_spec.ts

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,32 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { createPatch } from 'diff';
9+
import { runfiles } from '@bazel/runfiles';
1010
import { existsSync } from 'node:fs';
1111
import { readFile } from 'node:fs/promises';
12-
import { join } from 'node:path';
13-
import {
12+
import { dirname, join } from 'node:path';
13+
14+
/**
15+
* Resolve paths for the Critters license file and the golden reference file.
16+
*/
17+
const ANGULAR_SSR_PACKAGE_PATH = dirname(
18+
runfiles.resolve('angular_cli/packages/angular/ssr/npm_package/package.json'),
19+
);
20+
21+
/**
22+
* Path to the actual license file for the Critters library.
23+
* This file is located in the `third_party/critters` directory of the Angular CLI npm package.
24+
*/
25+
const CRITTERS_ACTUAL_LICENSE_FILE_PATH = join(
1426
ANGULAR_SSR_PACKAGE_PATH,
15-
CRITTERS_ACTUAL_LICENSE_FILE_PATH,
16-
CRITTERS_GOLDEN_LICENSE_FILE_PATH,
17-
} from './utils';
27+
'third_party/critters/THIRD_PARTY_LICENSES.txt',
28+
);
29+
30+
/**
31+
* Path to the golden reference license file for the Critters library.
32+
* This file is used as a reference for comparison and is located in the same directory as this script.
33+
*/
34+
const CRITTERS_GOLDEN_LICENSE_FILE_PATH = join(__dirname, 'THIRD_PARTY_LICENSES.txt.golden');
1835

1936
describe('NPM Package Tests', () => {
2037
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', () => {
2744
it('should exist', () => {
2845
expect(existsSync(CRITTERS_ACTUAL_LICENSE_FILE_PATH)).toBe(true);
2946
});
30-
31-
it('should match the expected golden file', async () => {
32-
const [expectedContent, actualContent] = await Promise.all([
33-
readFile(CRITTERS_GOLDEN_LICENSE_FILE_PATH, 'utf-8'),
34-
readFile(CRITTERS_ACTUAL_LICENSE_FILE_PATH, 'utf-8'),
35-
]);
36-
37-
if (expectedContent.trim() === actualContent.trim()) {
38-
return;
39-
}
40-
41-
const patch = createPatch(
42-
CRITTERS_GOLDEN_LICENSE_FILE_PATH,
43-
expectedContent,
44-
actualContent,
45-
'Golden License File',
46-
'Current License File',
47-
{ context: 5 },
48-
);
49-
50-
const errorMessage = `The content of the actual license file differs from the expected golden reference.
51-
Diff:
52-
${patch}
53-
To accept the new golden file, execute:
54-
yarn bazel run ${process.env['BAZEL_TARGET']}.accept
55-
`;
56-
57-
const error = new Error(errorMessage);
58-
error.stack = error.stack?.replace(` Diff:\n ${patch}`, '');
59-
throw error;
60-
});
6147
});
6248
});

packages/angular/ssr/test/npm_package/update-package-golden.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/angular/ssr/test/npm_package/utils.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

yarn.lock

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ __metadata:
668668
"@rollup/plugin-node-resolve": "npm:^13.0.5"
669669
"@types/babel__core": "npm:7.20.5"
670670
"@types/browser-sync": "npm:^2.27.0"
671-
"@types/diff": "npm:^5.2.1"
672671
"@types/express": "npm:^4.16.0"
673672
"@types/http-proxy": "npm:^1.17.4"
674673
"@types/ini": "npm:^4.0.0"
@@ -708,7 +707,6 @@ __metadata:
708707
critters: "npm:0.0.24"
709708
css-loader: "npm:7.1.2"
710709
debug: "npm:^4.1.1"
711-
diff: "npm:^5.2.0"
712710
esbuild: "npm:0.23.1"
713711
esbuild-wasm: "npm:0.23.1"
714712
eslint: "npm:8.57.0"
@@ -969,7 +967,6 @@ __metadata:
969967
"@angular/platform-server": "npm:19.0.0-next.1"
970968
"@angular/router": "npm:19.0.0-next.1"
971969
"@bazel/runfiles": "npm:^5.8.1"
972-
diff: "npm:^5.2.0"
973970
tslib: "npm:^2.3.0"
974971
zone.js: "npm:^0.15.0"
975972
peerDependencies:
@@ -4871,13 +4868,6 @@ __metadata:
48714868
languageName: node
48724869
linkType: hard
48734870

4874-
"@types/diff@npm:^5.2.1":
4875-
version: 5.2.1
4876-
resolution: "@types/diff@npm:5.2.1"
4877-
checksum: 10c0/62dcab32197ac67f212939cdd79aa3953327a482bec55c6a38ad9de8a0662a9f920b59504609a322fc242593bd9afb3d2704702f4bc98087a13171234b952361
4878-
languageName: node
4879-
linkType: hard
4880-
48814871
"@types/estree@npm:*, @types/estree@npm:1.0.5, @types/estree@npm:^1.0.0, @types/estree@npm:^1.0.5":
48824872
version: 1.0.5
48834873
resolution: "@types/estree@npm:1.0.5"
@@ -8561,7 +8551,7 @@ __metadata:
85618551
languageName: node
85628552
linkType: hard
85638553

8564-
"diff@npm:^5.0.0, diff@npm:^5.1.0, diff@npm:^5.2.0":
8554+
"diff@npm:^5.0.0, diff@npm:^5.1.0":
85658555
version: 5.2.0
85668556
resolution: "diff@npm:5.2.0"
85678557
checksum: 10c0/aed0941f206fe261ecb258dc8d0ceea8abbde3ace5827518ff8d302f0fc9cc81ce116c4d8f379151171336caf0516b79e01abdc1ed1201b6440d895a66689eb4

0 commit comments

Comments
 (0)