gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/yamltest/defs.bzl (about)

     1  """Tools for testing yaml files against schemas."""
     2  
     3  def _yaml_test_impl(ctx):
     4      """Implementation for yaml_test."""
     5      runner = ctx.actions.declare_file(ctx.label.name)
     6      ctx.actions.write(runner, "\n".join([
     7          "#!/bin/bash",
     8          "set -euo pipefail",
     9          "%s -schema=%s -strict=%s -- %s" % (
    10              ctx.files._tool[0].short_path,
    11              ctx.files.schema[0].short_path,
    12              "true" if ctx.attr.strict else "false",
    13              " ".join([f.short_path for f in ctx.files.srcs]),
    14          ),
    15      ]), is_executable = True)
    16      return [DefaultInfo(
    17          runfiles = ctx.runfiles(files = ctx.files._tool + ctx.files.schema + ctx.files.srcs),
    18          executable = runner,
    19      )]
    20  
    21  yaml_test = rule(
    22      implementation = _yaml_test_impl,
    23      doc = "Tests a yaml file against a schema.",
    24      attrs = {
    25          "srcs": attr.label_list(
    26              doc = "The input yaml files.",
    27              mandatory = True,
    28              allow_files = True,
    29          ),
    30          "schema": attr.label(
    31              doc = "The schema file in JSON schema format.",
    32              allow_single_file = True,
    33              mandatory = True,
    34          ),
    35          "strict": attr.bool(
    36              doc = "Whether to use strict mode for YAML decoding.",
    37              mandatory = False,
    38              default = True,
    39          ),
    40          "_tool": attr.label(
    41              executable = True,
    42              cfg = "target",
    43              default = Label("//tools/yamltest:yamltest"),
    44          ),
    45      },
    46      test = True,
    47  )