Skip to content

Commit

Permalink
build: use Bazel diff_test to compare file differences
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 committed Aug 23, 2024
1 parent f1d3fe0 commit 1c70f47
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 110 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion packages/angular/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 35 additions & 12 deletions packages/angular/ssr/test/npm_package/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -8,28 +9,50 @@ ts_library(
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",
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,
)
60 changes: 23 additions & 37 deletions packages/angular/ssr/test/npm_package/package_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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;
});
});
});
15 changes: 0 additions & 15 deletions packages/angular/ssr/test/npm_package/update-package-golden.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/angular/ssr/test/npm_package/utils.ts

This file was deleted.

12 changes: 1 addition & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1c70f47

Please sign in to comment.