Skip to content

Commit 7ecf94d

Browse files
authored
Merge pull request #3 from stackb/pcj/testing
Add golden file tests
2 parents 6bab357 + 798ed7d commit 7ecf94d

7 files changed

Lines changed: 131 additions & 13 deletions

File tree

.bcr/presubmit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ bcr_test_module:
88
name: "Build module"
99
platform: ${{ platform }}
1010
bazel: ${{ bazel }}
11-
build_targets:
11+
test_targets:
1212
- "//..."

.github/workflows/ci.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ jobs:
2525
disk-cache: true
2626
# Share repository cache between workflows.
2727
repository-cache: true
28-
- name: bazel build
28+
- name: bazel test
2929
run: >-
3030
bazelisk
3131
--bazelrc=.github/workflows/ci.bazelrc
3232
--bazelrc=.bazelrc
33-
build
34-
...
33+
test
34+
//...

.github/workflows/release_ruleset.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ jobs:
9393
# Fetch built artifacts (if any) from earlier jobs, which the release script may want to read.
9494
# Extract into ${GITHUB_WORKSPACE}/artifacts/*
9595
- uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e # v4.2.1
96-
96+
9797
- name: Build release artifacts and prepare release notes
9898
run: |
9999
if [ ! -f ".github/workflows/release_prep.sh" ]; then
@@ -205,4 +205,4 @@ jobs:
205205
tag_name: ${{ inputs.tag_name }}
206206
files: |
207207
release_files/**/*
208-
attestations/*
208+
attestations/*

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module(
1212

1313
bazel_dep(name = "aspect_rules_js", version = "2.6.0")
1414
bazel_dep(name = "aspect_rules_ts", version = "3.7.0")
15-
bazel_dep(name = "bazel_skylib", version = "1.8.1")
15+
bazel_dep(name = "bazel_skylib", version = "1.8.2")
1616
bazel_dep(name = "rules_nodejs", version = "6.5.0")
1717

1818
# --------------------------------------------------------------------------------

examples/helloworld/BUILD.bazel

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
load("//rules:defs.bzl", "closure_ts_compile")
2+
load("//rules:testing.bzl", "golden_file_test")
23

34
closure_ts_compile(
45
name = "lib",
56
srcs = ["index.ts"],
67
)
78

8-
# bazel run //examples/helloworld:files.update
9-
# bazel test //examples/helloworld:files.test
10-
# golden_filegroup(
11-
# name = "files",
12-
# srcs = [":index.ts"],
13-
# )
9+
# bazel test //examples/helloworld:golden_test
10+
# bazel run //examples/helloworld:golden_test.update
11+
golden_file_test(
12+
name = "golden_test",
13+
srcs = [":lib"],
14+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @fileoverview added by tsickle
3+
* Generated from: examples/helloworld/index.ts
4+
*/
5+
goog.module('examples.helloworld.index');
6+
var module = module || { id: 'examples/helloworld/index.ts' };
7+
goog.require('tslib');
8+
/**
9+
* @param {string} name
10+
* @return {string}
11+
*/
12+
function sayHello(name) {
13+
return `hello, ${name}!`;
14+
}
15+
exports.sayHello = sayHello;

rules/testing.bzl

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
def _golden_file_test_impl(ctx):
2+
outputs = [ctx.outputs.executable]
3+
4+
content = "\n".join([
5+
"""
6+
if diff -u {dst} {src}; then
7+
echo "files contents are identical"
8+
else
9+
echo "files differ"
10+
exit 1
11+
fi
12+
""".format(
13+
src = src.short_path,
14+
dst = src.short_path + ".golden",
15+
)
16+
for src in ctx.files.srcs
17+
])
18+
19+
ctx.actions.write(
20+
ctx.outputs.executable,
21+
"set -x\n\n" + content,
22+
is_executable = True,
23+
)
24+
return [
25+
DefaultInfo(
26+
files = depset(outputs),
27+
runfiles = ctx.runfiles(files = ctx.files.srcs + ctx.files.goldens),
28+
),
29+
]
30+
31+
_golden_file_test = rule(
32+
doc = "Asserts the two files are equal",
33+
implementation = _golden_file_test_impl,
34+
attrs = {
35+
"srcs": attr.label_list(
36+
doc = "the source files under test",
37+
allow_files = True,
38+
mandatory = True,
39+
),
40+
"goldens": attr.label_list(
41+
doc = "the expected golden files",
42+
allow_files = True,
43+
mandatory = True,
44+
),
45+
},
46+
executable = True,
47+
test = True,
48+
)
49+
50+
def _golden_file_update_impl(ctx):
51+
outputs = [ctx.outputs.executable]
52+
53+
content = "\n".join([
54+
"""
55+
cp -f {src} {dst}
56+
echo "Updated golden file: {dst}"
57+
""".format(
58+
src = src.short_path,
59+
dst = "$BUILD_WORKING_DIRECTORY/%s/%s.golden" % (ctx.label.package, src.basename),
60+
)
61+
for src in ctx.files.srcs
62+
])
63+
64+
ctx.actions.write(
65+
ctx.outputs.executable,
66+
"set -x\n\n" + content,
67+
is_executable = True,
68+
)
69+
return [
70+
DefaultInfo(
71+
files = depset(outputs),
72+
runfiles = ctx.runfiles(files = ctx.files.srcs),
73+
),
74+
]
75+
76+
_golden_file_update = rule(
77+
doc = "Asserts the two files are equal",
78+
implementation = _golden_file_update_impl,
79+
attrs = {
80+
"srcs": attr.label_list(
81+
doc = "the source files to update",
82+
allow_files = True,
83+
mandatory = True,
84+
),
85+
},
86+
executable = True,
87+
)
88+
89+
def golden_file_test(name, srcs, **kwargs):
90+
tags = kwargs.get("tags", [])
91+
_golden_file_test(
92+
name = name,
93+
srcs = srcs,
94+
goldens = native.glob(["*.golden"]),
95+
tags = tags,
96+
**kwargs
97+
)
98+
_golden_file_update(
99+
name = name + ".update",
100+
srcs = srcs,
101+
tags = tags,
102+
)

0 commit comments

Comments
 (0)