summaryrefslogtreecommitdiffhomepage
path: root/tools/go_generics/tests/defs.bzl
diff options
context:
space:
mode:
authorAdin Scannell <ascannell@google.com>2020-07-23 18:00:12 -0700
committerAdin Scannell <ascannell@google.com>2020-07-23 18:00:12 -0700
commitab0262bd94c25bc91d5e0d831b75729c253dfde6 (patch)
treea4a75c66b92b136ed1e250b4b9641a15808bf123 /tools/go_generics/tests/defs.bzl
parent14839e027f5310346718aea385cea5e45f017170 (diff)
Convert go_generics tests to starlark.
For some reason these tests were broken when run via the bazel docker container. The mechanism used was a bit crazy (self-extracting bundle), so convert them to use straight-forward starlark rules. This has the added advantaged that they are now independent tests.
Diffstat (limited to 'tools/go_generics/tests/defs.bzl')
-rw-r--r--tools/go_generics/tests/defs.bzl67
1 files changed, 67 insertions, 0 deletions
diff --git a/tools/go_generics/tests/defs.bzl b/tools/go_generics/tests/defs.bzl
new file mode 100644
index 000000000..038629852
--- /dev/null
+++ b/tools/go_generics/tests/defs.bzl
@@ -0,0 +1,67 @@
+"""Generics tests."""
+
+load("//tools/go_generics:defs.bzl", "go_template", "go_template_instance")
+
+def _go_generics_test_impl(ctx):
+ runner = ctx.actions.declare_file(ctx.label.name)
+ runner_content = "\n".join([
+ "#!/bin/bash",
+ "exec diff --ignore-blank-lines --ignore-matching-lines=^[[:space:]]*// %s %s" % (
+ ctx.files.template_output[0].short_path,
+ ctx.files.expected_output[0].short_path,
+ ),
+ "",
+ ])
+ ctx.actions.write(runner, runner_content, is_executable = True)
+ return [DefaultInfo(
+ executable = runner,
+ runfiles = ctx.runfiles(
+ files = ctx.files.template_output + ctx.files.expected_output,
+ collect_default = True,
+ collect_data = True,
+ ),
+ )]
+
+_go_generics_test = rule(
+ implementation = _go_generics_test_impl,
+ attrs = {
+ "template_output": attr.label(mandatory = True, allow_single_file = True),
+ "expected_output": attr.label(mandatory = True, allow_single_file = True),
+ },
+ test = True,
+)
+
+"""
+Instantiates a generics test.
+
+Args:
+ name: the name of the test.
+ inputs: all the input files.
+ output: the output files.
+ opts: the template options.
+"""
+
+def go_generics_test(name, inputs, output, types = None, consts = None, **kwargs):
+ if types == None:
+ types = dict()
+ if consts == None:
+ consts = dict()
+ go_template(
+ name = name + "_template",
+ srcs = inputs,
+ types = types.keys(),
+ consts = consts.keys(),
+ )
+ go_template_instance(
+ name = name + "_output",
+ template = ":" + name + "_template",
+ out = name + "_output.go",
+ types = types,
+ consts = consts,
+ **kwargs
+ )
+ _go_generics_test(
+ name = name + "_test",
+ template_output = name + "_output.go",
+ expected_output = output,
+ )