github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/rules/example.bzl (about)

     1  "example.bzl provides the gazelle_testdata_example rule."
     2  
     3  load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")
     4  
     5  def _examplegen_impl(ctx):
     6      config_json = ctx.outputs.json
     7      output_test = ctx.outputs.test
     8      output_markdown = ctx.outputs.markdown
     9  
    10      config = struct(
    11          name = ctx.label.name,
    12          label = str(ctx.label),
    13          testOut = output_test.path,
    14          testContent = ctx.attr.test_content,
    15          markdownOut = output_markdown.path,
    16          workspaceIn = ctx.file.workspace_template.path,
    17          stripPrefix = ctx.attr.strip_prefix,
    18          files = [f.path for f in ctx.files.srcs],
    19      )
    20  
    21      ctx.actions.write(
    22          output = config_json,
    23          content = config.to_json(),
    24      )
    25  
    26      ctx.actions.run(
    27          mnemonic = "ExampleGenerate",
    28          progress_message = "Generating %s test" % ctx.attr.name,
    29          executable = ctx.file._examplegen,
    30          arguments = ["--config_json=%s" % config_json.path],
    31          inputs = [config_json, ctx.file.workspace_template] + ctx.files.srcs,
    32          outputs = [output_test, output_markdown],
    33      )
    34  
    35      return [DefaultInfo(
    36          files = depset([config_json, output_test, output_markdown]),
    37      )]
    38  
    39  _examplegen = rule(
    40      implementation = _examplegen_impl,
    41      attrs = {
    42          "srcs": attr.label_list(
    43              doc = "Sources for the test txtar file",
    44              allow_files = True,
    45          ),
    46          "strip_prefix": attr.string(
    47              doc = "path prefix to remove from test files in the txtar",
    48          ),
    49          "test_content": attr.string(
    50              doc = "optional chunk of golang test content.  Default behavior is 'bazel build ...'",
    51              default = """
    52  func TestBuild(t *testing.T) {
    53  	if err := bazel_testing.RunBazel("build", "..."); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  }
    57  """,
    58          ),
    59          "workspace_template": attr.label(
    60              doc = "Template for the test WORKSPACE",
    61              allow_single_file = True,
    62              mandatory = True,
    63          ),
    64          "_examplegen": attr.label(
    65              doc = "The examplegen generator tool",
    66              default = "//cmd/examplegen",
    67              allow_single_file = True,
    68              executable = True,
    69              cfg = "exec",
    70          ),
    71      },
    72      outputs = {
    73          "json": "%{name}.json",
    74          "test": "%{name}_test.go",
    75          "markdown": "%{name}.md",
    76      },
    77  )
    78  
    79  def gazelle_testdata_example(**kwargs):
    80      """
    81      gazelle_testdata_example rule runs an go_bazel_test for an example dir
    82  
    83      Args:
    84          **kwargs: the kwargs dict passed to 'go_bazel_test'
    85      """
    86      name = kwargs.pop("name")
    87      srcs = kwargs.pop("srcs", [])
    88      deps = kwargs.pop("deps", [])
    89      strip_prefix = kwargs.pop("strip_prefix", "")
    90  
    91      test_content = kwargs.pop("test_content", None)
    92      rule_files = kwargs.pop("rule_files", ["//:all_files"])
    93  
    94      _examplegen(
    95          name = name,
    96          srcs = srcs,
    97          strip_prefix = strip_prefix,
    98          test_content = test_content,
    99          workspace_template = kwargs.pop("workspace_template", ""),
   100      )
   101  
   102      go_bazel_test(
   103          name = name + "_test",
   104          srcs = [name + "_test.go"],
   105          deps = deps + ["@com_github_google_go_cmp//cmp"],
   106          rule_files = rule_files,
   107          **kwargs
   108      )