github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/go_generics/tests/defs.bzl (about)

     1  """Generics tests."""
     2  
     3  load("//tools/go_generics:defs.bzl", "go_template", "go_template_instance")
     4  
     5  def _go_generics_test_impl(ctx):
     6      runner = ctx.actions.declare_file(ctx.label.name)
     7      runner_content = "\n".join([
     8          "#!/bin/bash",
     9          "exec diff --ignore-blank-lines --ignore-matching-lines=^[[:space:]]*// %s %s" % (
    10              ctx.files.template_output[0].short_path,
    11              ctx.files.expected_output[0].short_path,
    12          ),
    13          "",
    14      ])
    15      ctx.actions.write(runner, runner_content, is_executable = True)
    16      return [DefaultInfo(
    17          executable = runner,
    18          runfiles = ctx.runfiles(
    19              files = ctx.files.template_output + ctx.files.expected_output,
    20              collect_default = True,
    21              collect_data = True,
    22          ),
    23      )]
    24  
    25  _go_generics_test = rule(
    26      implementation = _go_generics_test_impl,
    27      attrs = {
    28          "template_output": attr.label(mandatory = True, allow_single_file = True),
    29          "expected_output": attr.label(mandatory = True, allow_single_file = True),
    30      },
    31      test = True,
    32  )
    33  
    34  def go_generics_test(name, inputs, output, types = None, consts = None, **kwargs):
    35      """Instantiates a generics test.
    36  
    37      Args:
    38          name: the name of the test.
    39          inputs: all the input files.
    40          output: the output files.
    41          types: the template types (dictionary).
    42          consts: the template consts (dictionary).
    43          **kwargs: additional arguments for the template_instance.
    44      """
    45      if types == None:
    46          types = dict()
    47      if consts == None:
    48          consts = dict()
    49      go_template(
    50          name = name + "_template",
    51          srcs = inputs,
    52          types = types.keys(),
    53          consts = consts.keys(),
    54      )
    55      go_template_instance(
    56          name = name + "_output",
    57          template = ":" + name + "_template",
    58          out = name + "_output.go",
    59          types = types,
    60          consts = consts,
    61          **kwargs
    62      )
    63      _go_generics_test(
    64          name = name + "_test",
    65          template_output = name + "_output.go",
    66          expected_output = output,
    67      )