yaml_test: Add argument to allow non-strict YAML decoding.

PiperOrigin-RevId: 450576605
This commit is contained in:
Etienne Perot
2022-05-23 19:11:14 -07:00
committed by gVisor bot
parent 6a922404e7
commit a6fbca2eec
2 changed files with 13 additions and 4 deletions
+7 -1
View File
@@ -6,9 +6,10 @@ def _yaml_test_impl(ctx):
ctx.actions.write(runner, "\n".join([
"#!/bin/bash",
"set -euo pipefail",
"%s -schema=%s -- %s" % (
"%s -schema=%s -strict=%s -- %s" % (
ctx.files._tool[0].short_path,
ctx.files.schema[0].short_path,
"true" if ctx.attr.strict else "false",
" ".join([f.short_path for f in ctx.files.srcs]),
),
]), is_executable = True)
@@ -31,6 +32,11 @@ yaml_test = rule(
allow_single_file = True,
mandatory = True,
),
"strict": attr.bool(
doc = "Whether to use strict mode for YAML decoding.",
mandatory = False,
default = True,
),
"_tool": attr.label(
executable = True,
cfg = "host",
+6 -3
View File
@@ -26,6 +26,11 @@ import (
yaml "gopkg.in/yaml.v2"
)
var (
schema = flag.String("schema", "", "path to JSON schema file.")
strict = flag.Bool("strict", true, "Whether to enable strict mode for YAML decoding")
)
func fixup(v interface{}) (interface{}, error) {
switch x := v.(type) {
case map[interface{}]interface{}:
@@ -65,7 +70,7 @@ func loadFile(filename string) (gojsonschema.JSONLoader, error) {
}
defer f.Close()
dec := yaml.NewDecoder(f)
dec.SetStrict(true)
dec.SetStrict(*strict)
var object interface{}
if err := dec.Decode(&object); err != nil {
return nil, err
@@ -81,8 +86,6 @@ func loadFile(filename string) (gojsonschema.JSONLoader, error) {
return gojsonschema.NewStringLoader(string(bytes)), nil
}
var schema = flag.String("schema", "", "path to JSON schema file.")
func main() {
flag.Parse()
if *schema == "" || len(flag.Args()) == 0 {