kythe.io@v0.0.68-0.20240422202219-7225dbc01741/tools/build_rules/testing.bzl (about)

     1  """This module defines macros for making shell tests."""
     2  
     3  def shell_tool_test(
     4          name,
     5          script = [],
     6          scriptfile = "",
     7          tools = {},
     8          data = [],
     9          args = [],
    10          size = "small",
    11          tags = []):
    12      """A macro to invoke a test script with access to specified tools.
    13  
    14      Each tool named by the tools attribute is available in the script via a
    15      variable named by the corresponding key. For example, if tools contains
    16  
    17         "A": "//foo/bar:baz"
    18  
    19      then $A refers to the location of //foo/bar:baz in the script. Each element
    20      of script becomes a single line of the resulting test.
    21  
    22      Exactly one of "script" or "scriptfile" must be set. If "script" is set, it
    23      is used as an inline script; otherwise "scriptfile" must be a file target
    24      containing the script to be included.
    25  
    26      Any targets specified in "data" are included as data dependencies for the
    27      resulting sh_test rule without further interpretation.
    28  
    29      Args:
    30        name: the name for the sh_test to create
    31        script: the script to use.
    32          Either script or scriptfile must be set, but not both.
    33        scriptfile: the file containing the script to use.
    34          Either scriptfile or script must be set, but not both.
    35        tools: tools to use, passed in to test_args and data
    36        data: additional data to pass in to the sh_test
    37        args: additinoal args to pass in to the sh_test
    38        size: size of the test ("small", "medium", etc)
    39        tags: tags for the sh_test
    40      """
    41      if (len(script) == 0) == (scriptfile == ""):
    42          fail('You must set exactly one of "script" or "scriptfile"')
    43  
    44      bases = sorted(tools.keys())
    45      lines = ["set -e", 'cat >$@ <<"EOF"'] + [
    46          'readonly %s="$${%d:?missing %s tool}"' % (base, i + 1, base)
    47          for i, base in enumerate(bases)
    48      ] + ["shift %d" % len(bases)] + script + ["EOF"]
    49      if scriptfile:
    50          lines += [
    51              "# --- end generated section ---",
    52              'cat >>$@ "$(location %s)"' % scriptfile,
    53          ]
    54  
    55      genscript = name + "_test_script"
    56      native.genrule(
    57          name = genscript,
    58          outs = [genscript + ".sh"],
    59          srcs = [scriptfile] if scriptfile else [],
    60          testonly = True,
    61          cmd = "\n".join(lines),
    62          tags = tags,
    63      )
    64      test_args = ["$(location %s)" % tools[base] for base in bases]
    65      native.sh_test(
    66          name = name,
    67          data = sorted(tools.values()) + data,
    68          srcs = [genscript],
    69          args = test_args + args,
    70          size = size,
    71          tags = tags,
    72      )