kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/textproto/testdata/textproto_verifier_test.bzl (about)

     1  """Rules for verifying textproto indexer output"""
     2  
     3  # copied from proto_verifier_test.bzl.
     4  # TODO(justbuchanan): refactor
     5  
     6  # Copyright 2019 The Kythe Authors. All rights reserved.
     7  #
     8  # Licensed under the Apache License, Version 2.0 (the "License");
     9  # you may not use this file except in compliance with the License.
    10  # You may obtain a copy of the License at
    11  #
    12  #   http://www.apache.org/licenses/LICENSE-2.0
    13  #
    14  # Unless required by applicable law or agreed to in writing, software
    15  # distributed under the License is distributed on an "AS IS" BASIS,
    16  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  # See the License for the specific language governing permissions and
    18  # limitations under the License.
    19  
    20  load("@bazel_skylib//lib:paths.bzl", "paths")
    21  load(
    22      "@io_kythe//tools/build_rules/verifier_test:verifier_test.bzl",
    23      "KytheVerifierSources",
    24      "extract",
    25      "index_compilation",
    26      "verifier_test",
    27  )
    28  load("@rules_proto//proto:defs.bzl", "ProtoInfo")
    29  load("//kythe/cxx/indexer/proto/testdata:proto_verifier_test.bzl", "get_proto_files_and_proto_paths", "proto_extract_kzip")
    30  
    31  def _invoke(rulefn, name, **kwargs):
    32      """Invoke rulefn with name and kwargs, returning the label of the rule."""
    33      rulefn(name = name, **kwargs)
    34      return "//{}:{}".format(native.package_name(), name)
    35  
    36  def _textproto_extract_kzip_impl(ctx):
    37      toplevel_proto_srcs, all_proto_srcs, pathopt = get_proto_files_and_proto_paths(ctx.attr.protos)
    38  
    39      args = ctx.actions.args()
    40      args.add_all(ctx.attr.opts)
    41      args.add("--")
    42      args.add_all(pathopt, before_each = "--proto_path")
    43  
    44      extract(
    45          srcs = ctx.files.srcs,
    46          ctx = ctx,
    47          extractor = ctx.executable.extractor,
    48          kzip = ctx.outputs.kzip,
    49          mnemonic = "TextprotoExtractKZip",
    50          opts = args,
    51          vnames_config = ctx.file.vnames_config,
    52          deps = all_proto_srcs,
    53      )
    54      return [KytheVerifierSources(files = depset(ctx.files.srcs))]
    55  
    56  textproto_extract_kzip = rule(
    57      attrs = {
    58          "srcs": attr.label_list(
    59              mandatory = True,
    60              allow_empty = False,
    61              allow_files = True,
    62              providers = [ProtoInfo],
    63          ),
    64          "protos": attr.label_list(mandatory = True, allow_empty = False, allow_files = False),
    65          "extractor": attr.label(
    66              default = Label("//kythe/cxx/extractor/textproto:textproto_extractor"),
    67              executable = True,
    68              cfg = "exec",
    69          ),
    70          "opts": attr.string_list(),
    71          "vnames_config": attr.label(
    72              default = Label("//external:vnames_config"),
    73              allow_single_file = True,
    74          ),
    75      },
    76      outputs = {"kzip": "%{name}.kzip"},
    77      implementation = _textproto_extract_kzip_impl,
    78  )
    79  
    80  def textproto_verifier_test(
    81          name,
    82          textprotos,
    83          protos,
    84          size = "small",
    85          tags = [],
    86          extractor_opts = [],
    87          indexer_opts = [],
    88          verifier_opts = [],
    89          convert_marked_source = False,
    90          vnames_config = None,
    91          visibility = None):
    92      """Extract, analyze, and verify a textproto compilation.
    93  
    94      Args:
    95        name: Name of the test
    96        textprotos: Textproto files being tested
    97        protos: Proto libraries that define the textproto's schema
    98        size: Test size
    99        tags: Test tags
   100        extractor_opts: List of options passed to the textproto extractor
   101        indexer_opts: List of options passed to the textproto indexer
   102        verifier_opts: List of options passed to the verifier tool
   103        convert_marked_source: Whether the verifier should convert marked source.
   104        vnames_config: Optional path to a VName configuration file
   105        visibility: Visibility of underlying build targets
   106      Returns:
   107        Name of the test rule
   108      """
   109  
   110      # extract and index each textproto
   111      textproto_entries = []
   112      for textproto in textprotos:
   113          rule_prefix = name + "_" + paths.replace_extension(textproto, "")
   114  
   115          # extract textproto
   116          textproto_kzip = _invoke(
   117              textproto_extract_kzip,
   118              name = rule_prefix + "_kzip",
   119              testonly = True,
   120              srcs = [textproto],
   121              tags = tags,
   122              visibility = visibility,
   123              vnames_config = vnames_config,
   124              protos = protos,
   125              opts = extractor_opts,
   126          )
   127  
   128          # index textproto
   129          entries = _invoke(
   130              index_compilation,
   131              name = rule_prefix + "_entries",
   132              testonly = True,
   133              indexer = "//kythe/cxx/indexer/textproto:textproto_indexer",
   134              target_indexer = "//kythe/cxx/indexer/textproto:textproto_indexer",
   135              opts = indexer_opts + ["--index_file"],
   136              tags = tags + ["manual"],
   137              visibility = visibility,
   138              deps = [textproto_kzip],
   139          )
   140  
   141          textproto_entries.append(entries)
   142  
   143      # extract proto(s)
   144      proto_kzip = _invoke(
   145          proto_extract_kzip,
   146          name = name + "_protos_kzip",
   147          testonly = True,
   148          srcs = protos,
   149          tags = tags,
   150          visibility = visibility,
   151          vnames_config = vnames_config,
   152      )
   153  
   154      # index proto(s)
   155      proto_entries = _invoke(
   156          index_compilation,
   157          name = name + "_proto_entries",
   158          testonly = True,
   159          indexer = "//kythe/cxx/indexer/proto:indexer",
   160          opts = ["--index_file"],
   161          tags = tags,
   162          visibility = visibility,
   163          deps = [proto_kzip],
   164      )
   165  
   166      vopts = verifier_opts + ["--ignore_dups", "--show_goals", "--goal_regex=\"\\s*(?:#|//)-(.*)\""]
   167      if convert_marked_source:
   168          vopts.append("--convert_marked_source")
   169      return _invoke(
   170          verifier_test,
   171          name = name,
   172          size = size,
   173          opts = vopts,
   174          tags = tags,
   175          visibility = visibility,
   176          deps = textproto_entries + [proto_entries],
   177      )