forked from bazel-contrib/rules_go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbazel_tests.bzl
368 lines (333 loc) · 11.5 KB
/
bazel_tests.bzl
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
load(
"@io_bazel_rules_go//go/private:context.bzl",
"go_context",
)
load(
"@io_bazel_rules_go//go/private:common.bzl",
"env_execute",
)
load(
"@io_bazel_rules_go//go/private:go_toolchain.bzl",
"generate_toolchain_names",
)
load(
"@io_bazel_rules_go//go/private:rules/rule.bzl",
"go_rule",
)
load(
"@io_bazel_rules_go//go/private:skylib/lib/paths.bzl",
"paths",
)
load(
"@io_bazel_rules_go//go/private:skylib/lib/shell.bzl",
"shell",
)
# _bazelrc is the bazel.rc file that sets the default options for tests
def _bazelrc(strategy):
return """
build --verbose_failures
build --sandbox_debug
build --test_output=errors
build --spawn_strategy={strategy}
build --genrule_strategy={strategy}
test --test_strategy={strategy}
test --nocache_test_results
build:isolate --
build:fetch --fetch=True
""".format(strategy = strategy)
# _basic_workspace is the content appended to all test workspace files
# it contains the calls required to make the go rules work
_basic_workspace = """
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
"""
# _bazel_test_script_template is the template for the bazel invocation script
_bazel_test_script_template = """
set -u
echo running in {work_dir}
unset TEST_TMPDIR
RULES_GO_OUTPUT={output}
mkdir -p {work_dir}
mkdir -p {cache_dir}
cp -f {workspace} {work_dir}/WORKSPACE
cp -f {build} {work_dir}/BUILD.bazel
extra_files=({extra_files})
if [ "${{#extra_files[@]}}" -ne 0 ]; then
cp -f "${{extra_files[@]}}" {work_dir}/
fi
cd {work_dir}
output_base_arg=""
if (( {clean_build} )); then
tmp_output_base="$(mktemp -d)"
output_base_arg="--output_base=${{tmp_output_base}}"
fi
cmd=(
{bazel} --bazelrc {bazelrc} ${{output_base_arg}} {command}
--experimental_repository_cache={cache_dir} --config {config} {args}
{target}
)
echo "${{cmd[*]}}" >bazel-output.txt
"${{cmd[@]}}" >>bazel-output.txt 2>&1
result=$?
function at_exit {{
echo "bazel exited with status $result"
echo "----- bazel-output.txt begin -----"
cat bazel-output.txt
echo "----- bazel-output.txt end -----"
for log in {logs}; do
if [ ! -e "$log" ]; then
echo "----- $log not found -----"
else
echo "----- $log begin -----"
cat "$log"
echo "----- $log end -----"
fi
done
if (( {clean_build} )); then
{bazel} --bazelrc {bazelrc} ${{output_base_arg}} clean &> /dev/null
{bazel} --bazelrc {bazelrc} ${{output_base_arg}} shutdown &> /dev/null
fi
}}
trap at_exit EXIT
{check}
exit $result
"""
# _env_build_template is the template for the bazel test environment repository build file
_env_build_template = """
load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test_settings")
bazel_test_settings(
name = "settings",
bazel = "{bazel}",
exec_root = "{exec_root}",
scratch_dir = "{scratch_dir}",
visibility = ["//visibility:public"],
)
filegroup(
name = "standalone_bazelrc",
srcs = ["test.bazelrc"],
visibility = ["//visibility:public"],
)
filegroup(
name = "sandboxed_bazelrc",
srcs = ["sandboxed_test.bazelrc"],
visibility = ["//visibility:public"],
)
"""
CURRENT_VERSION = "current"
def _bazel_test_script_impl(ctx):
go = go_context(ctx)
script_file = go.declare_file(go, ext = ".bash")
if not ctx.attr.targets:
# Skip test when there are no targets. Targets may be platform-specific,
# and we may not have any targets on some platforms.
ctx.actions.write(script_file, "", is_executable = True)
return [DefaultInfo(files = depset([script_file]))]
register = ""
if any([ext.label.workspace_root.endswith("/go_sdk") for ext in ctx.attr.externals]):
register += "register_toolchains(\n{}\n)\n".format(
"\n".join([
' "@go_sdk//:{}",'.format(name)
for name in generate_toolchain_names()
]),
)
if ctx.attr.go_version == CURRENT_VERSION:
register += "go_register_toolchains()\n"
elif ctx.attr.go_version != None:
register += 'go_register_toolchains(go_version="{}")\n'.format(ctx.attr.go_version)
workspace_content = 'workspace(name = "bazel_test")\n\n'
for ext in ctx.attr.externals:
root = ext.label.workspace_root
_, _, name = ext.label.workspace_root.rpartition("/")
workspace_content += 'local_repository(name="{name}", path="{exec_root}/{root}")\n'.format(
name = name,
root = root,
exec_root = ctx.attr._settings.exec_root,
)
if ctx.attr.workspace:
workspace_content += ctx.attr.workspace
else:
workspace_content += _basic_workspace.format()
workspace_content += register
workspace_file = go.declare_file(go, path = "WORKSPACE.in")
ctx.actions.write(workspace_file, workspace_content)
build_file = go.declare_file(go, path = "BUILD.in")
ctx.actions.write(build_file, ctx.attr.build)
output = "external/" + ctx.workspace_name + "/" + ctx.label.package
targets = ["@" + ctx.workspace_name + "//" + ctx.label.package + t if t.startswith(":") else t for t in ctx.attr.targets]
logs = []
if ctx.attr.command in ("test", "coverage"):
# TODO(jayconrod): read logs for other packages
logs = [
"bazel-testlogs/{}/{}/test.log".format(output, t[1:])
for t in ctx.attr.targets
if t.startswith(":")
]
script_content = _bazel_test_script_template.format(
bazelrc = shell.quote(ctx.attr._settings.exec_root + "/" + ctx.file.bazelrc.path),
config = ctx.attr.config,
extra_files = " ".join([shell.quote(paths.join(ctx.attr._settings.exec_root, "execroot", "io_bazel_rules_go", file.path)) for file in ctx.files.extra_files]),
command = ctx.attr.command,
args = " ".join(ctx.attr.args),
target = " ".join(targets),
logs = " ".join([shell.quote(l) for l in logs]),
check = ctx.attr.check,
workspace = shell.quote(workspace_file.short_path),
build = shell.quote(build_file.short_path),
output = shell.quote(output),
bazel = ctx.attr._settings.bazel,
work_dir = shell.quote(ctx.attr._settings.scratch_dir + "/" + ctx.attr.config),
cache_dir = shell.quote(ctx.attr._settings.scratch_dir + "/cache"),
clean_build = "1" if ctx.attr.clean_build else "0",
)
ctx.actions.write(output = script_file, is_executable = True, content = script_content)
return struct(
files = depset([script_file]),
runfiles = ctx.runfiles(
[workspace_file, build_file] + ctx.files.extra_files,
collect_data = True,
),
)
_bazel_test_script = go_rule(
_bazel_test_script_impl,
attrs = {
"command": attr.string(
mandatory = True,
values = [
"build",
"test",
"coverage",
"run",
],
),
"args": attr.string_list(default = []),
"targets": attr.string_list(mandatory = True),
"externals": attr.label_list(allow_files = True),
"go_version": attr.string(default = CURRENT_VERSION),
"workspace": attr.string(),
"build": attr.string(),
"check": attr.string(),
"config": attr.string(default = "isolate"),
"extra_files": attr.label_list(allow_files = True),
"data": attr.label_list(allow_files = True),
"clean_build": attr.bool(default = False),
"bazelrc": attr.label(
allow_single_file = True,
default = "@bazel_test//:standalone_bazelrc",
),
"_settings": attr.label(default = "@bazel_test//:settings"),
},
)
def bazel_test(name, command = None, args = None, targets = None, go_version = None, tags = [], externals = [], workspace = "", build = "", check = "", config = None, extra_files = [], standalone = True, clean_build = False):
script_name = name + "_script"
externals = externals + [
"@io_bazel_rules_go//:AUTHORS",
]
if go_version == None or go_version == CURRENT_VERSION:
externals.append("@go_sdk//:packages.txt")
bazelrc = "@bazel_test//:standalone_bazelrc" if standalone else "@bazel_test//:sandboxed_bazelrc"
_bazel_test_script(
name = script_name,
args = args,
bazelrc = bazelrc,
build = build,
check = check,
clean_build = clean_build,
command = command,
config = config,
externals = externals,
extra_files = extra_files,
go_version = go_version,
targets = targets,
workspace = workspace,
)
native.sh_test(
name = name,
size = "large",
timeout = "moderate",
srcs = [":" + script_name],
tags = ["local", "bazel", "exclusive"] + tags,
data = [
"@bazel_gazelle//cmd/gazelle",
bazelrc,
"@io_bazel_rules_go//tests:rules_go_deps",
],
)
def _md5_sum_impl(ctx):
go = go_context(ctx)
out = go.declare_file(go, ext = ".md5")
arguments = ctx.actions.args()
arguments.add_all(["-output", out])
arguments.add_all(ctx.files.srcs)
ctx.actions.run(
inputs = ctx.files.srcs,
outputs = [out],
mnemonic = "GoMd5sum",
executable = ctx.executable._md5sum,
arguments = [arguments],
)
return struct(files = depset([out]))
md5_sum = go_rule(
_md5_sum_impl,
attrs = {
"srcs": attr.label_list(allow_files = True),
"_md5sum": attr.label(
executable = True,
default = "@io_bazel_rules_go//go/tools/builders:md5sum",
cfg = "host",
),
},
)
def _test_environment_impl(ctx):
# Find bazel
bazel = ""
if "BAZEL" in ctx.os.environ:
bazel = ctx.os.environ["BAZEL"]
elif "BAZEL_VERSION" in ctx.os.environ:
home = ctx.os.environ["HOME"]
bazel = home + "/.bazel/{0}/bin/bazel".format(ctx.os.environ["BAZEL_VERSION"])
if bazel == "" or not ctx.path(bazel).exists:
bazel = ctx.which("bazel")
# Get a temporary directory to use as our scratch workspace
if ctx.os.name.startswith("windows"):
scratch_dir = ctx.os.environ["TMP"].replace("\\", "/") + "/bazel_go_test"
else:
result = env_execute(ctx, ["mktemp", "-d"])
if result.return_code:
fail("failed to create temporary directory for bazel tests: {}".format(result.stderr))
scratch_dir = result.stdout.strip()
# Work out where we are running so we can find externals
exec_root, _, _ = str(ctx.path(".")).rpartition("/external/")
# build the basic environment
ctx.file("WORKSPACE", 'workspace(name = "{}")'.format(ctx.name))
ctx.file("BUILD.bazel", _env_build_template.format(
bazel = bazel,
exec_root = exec_root,
scratch_dir = scratch_dir,
))
ctx.file("test.bazelrc", content = _bazelrc("standalone"))
ctx.file("sandboxed_test.bazelrc", content = _bazelrc("sandboxed"))
_test_environment = repository_rule(
attrs = {},
environ = [
"BAZEL",
"BAZEL_VERSION",
"HOME",
],
implementation = _test_environment_impl,
)
def _bazel_test_settings_impl(ctx):
return struct(
bazel = ctx.attr.bazel,
exec_root = ctx.attr.exec_root,
scratch_dir = ctx.attr.scratch_dir,
)
bazel_test_settings = rule(
_bazel_test_settings_impl,
attrs = {
"bazel": attr.string(mandatory = True),
"exec_root": attr.string(mandatory = True),
"scratch_dir": attr.string(mandatory = True),
},
)
def test_environment():
_test_environment(name = "bazel_test")