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