kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/proto/testdata/proto_extractor_test.bzl (about) 1 """Rules for testing the proto extractor""" 2 3 # Copyright 2018 The Kythe Authors. All rights reserved. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 load("@bazel_skylib//lib:dicts.bzl", "dicts") 18 19 def _extract_kzip_impl(ctx): 20 cmd = [ctx.executable.extractor.path] + [p.path for p in ctx.files.srcs] + ctx.attr.opts 21 ctx.actions.run_shell( 22 mnemonic = "Extract", 23 command = " ".join(cmd), 24 env = dicts.add(ctx.attr.extra_env, {"KYTHE_OUTPUT_FILE": ctx.outputs.kzip.path}), 25 outputs = [ctx.outputs.kzip], 26 tools = [ctx.executable.extractor], 27 inputs = ctx.files.srcs + ctx.files.deps, 28 ) 29 return [DefaultInfo(runfiles = ctx.runfiles(files = [ctx.outputs.kzip]))] 30 31 extract_kzip = rule( 32 implementation = _extract_kzip_impl, 33 attrs = { 34 "srcs": attr.label_list(allow_files = True, mandatory = True), 35 "deps": attr.label_list(allow_files = True), 36 "extractor": attr.label( 37 cfg = "exec", 38 executable = True, 39 default = Label("//kythe/cxx/extractor/proto:proto_extractor"), 40 ), 41 "opts": attr.string_list(), 42 "extra_env": attr.string_dict(), 43 }, 44 outputs = {"kzip": "%{name}.kzip"}, 45 ) 46 47 def _kzip_diff_test_impl(ctx): 48 # Write a script that `bazel test` will execute. 49 script = " ".join([ 50 ctx.executable.diff_bin.short_path, 51 ctx.executable.kzip_tool.short_path, 52 ctx.executable.formatjson.short_path, 53 ctx.files.kzip[0].short_path, 54 ctx.files.golden_file[0].short_path, 55 ]) 56 ctx.actions.write( 57 output = ctx.outputs.executable, 58 content = script, 59 ) 60 61 runfiles = ctx.runfiles(files = [ 62 ctx.executable.diff_bin, 63 ctx.executable.kzip_tool, 64 ctx.executable.formatjson, 65 ctx.file.kzip, 66 ctx.file.golden_file, 67 ]) 68 return [DefaultInfo(runfiles = runfiles)] 69 70 kzip_diff_test = rule( 71 implementation = _kzip_diff_test_impl, 72 attrs = { 73 "golden_file": attr.label(mandatory = True, allow_single_file = True), 74 "kzip": attr.label(mandatory = True, allow_single_file = True), 75 "diff_bin": attr.label( 76 cfg = "exec", 77 executable = True, 78 default = Label("//kythe/cxx/extractor/proto/testdata:kzip_diff_test"), 79 ), 80 "kzip_tool": attr.label( 81 cfg = "exec", 82 executable = True, 83 default = Label("//kythe/go/platform/tools/kzip"), 84 ), 85 "formatjson": attr.label( 86 cfg = "exec", 87 executable = True, 88 default = Label("//kythe/go/util/formatjson"), 89 ), 90 }, 91 test = True, 92 ) 93 94 def extractor_golden_test( 95 name, 96 srcs, 97 deps = [], 98 opts = [], 99 extra_env = {}, 100 extractor = "//kythe/cxx/extractor/proto:proto_extractor"): 101 """Runs the extractor and compares the result to a golden file. 102 103 Args: 104 name: test name (note: _test will be appended to the end) 105 srcs: files to extract 106 deps: any other required deps 107 opts: arguments to pass to the extractor 108 extra_env: environment variables to configure extractor behavior 109 extractor: the extractor binary to use 110 """ 111 kzip = name + "_kzip" 112 extract_kzip( 113 name = kzip, 114 opts = opts, 115 deps = deps, 116 srcs = srcs, 117 extra_env = extra_env, 118 extractor = extractor, 119 testonly = True, 120 ) 121 122 kzip_diff_test( 123 name = name + "_test", 124 kzip = kzip, 125 golden_file = name + ".UNIT", 126 )