2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-12-02 10:15:22 +00:00
bazel-lib/lib/tests/yq/BUILD.bazel

325 lines
6.3 KiB
Python

load("//lib/private:diff_test.bzl", "diff_test")
load("//lib:yq.bzl", "yq")
exports_files(
[
"a.yaml",
],
visibility = ["//visibility:public"],
)
# Identity (dot) expression produces identical yaml
yq(
name = "case_dot_expression",
srcs = ["a.yaml"],
expression = ".",
)
diff_test(
name = "case_dot_expression_test",
file1 = "a.yaml",
file2 = ":case_dot_expression",
)
# No expression same as dot expression
yq(
name = "case_no_expression",
srcs = ["a.yaml"],
)
diff_test(
name = "case_no_expression_test",
file1 = "a.yaml",
file2 = ":case_no_expression",
)
# Output json, no out declared
yq(
name = "case_json_output_no_out",
srcs = ["a.yaml"],
args = ["-o=json"],
expression = ".",
)
diff_test(
name = "case_json_output_no_out_test",
file1 = "//lib/tests/jq:a_pretty.json",
file2 = "case_json_output_no_out.json",
)
# Output json, outs has ".json" extension but "-o=json" not set
yq(
name = "case_json_output_no_arg",
srcs = ["a.yaml"],
outs = ["case_json_output_no_arg.json"],
args = [],
expression = ".",
)
diff_test(
name = "case_json_output_no_arg_test",
file1 = "//lib/tests/jq:a_pretty.json",
file2 = ":case_json_output_no_arg",
)
# Convert json to yaml
yq(
name = "case_convert_json_to_yaml",
srcs = ["//lib/tests/jq:a_pretty.json"],
args = ["-P"],
expression = ".",
)
diff_test(
name = "case_convert_json_to_yaml_test",
file1 = "a.yaml",
file2 = ":case_convert_json_to_yaml",
)
# No srcs, output is a generated expression
yq(
name = "case_generate_from_expression",
srcs = [],
expression = ".a.b.c = \"cat\"",
)
diff_test(
name = "case_generate_from_expression_test",
file1 = "generated-from-expression.yaml",
file2 = ":case_generate_from_expression",
)
# No sources produces empty file (equivalent to --null-input)
yq(
name = "case_no_sources",
srcs = [],
expression = ".",
)
diff_test(
name = "case_no_sources_test",
file1 = ":case_no_sources",
file2 = "empty.yaml",
)
# Merge two documents together
yq(
name = "case_merge_expression",
srcs = [
"a.yaml",
"b.yaml",
],
expression = "select(fileIndex == 0) * select(fileIndex == 1)",
)
diff_test(
name = "case_merge_expression_test",
file1 = "a_b_merged.yaml",
file2 = ":case_merge_expression",
)
# Merge two documents together (alt syntax)
yq(
name = "case_merge_expression_alt",
srcs = [
"a.yaml",
"b.yaml",
],
expression = ". as $item ireduce ({}; . * $item )",
)
diff_test(
name = "case_merge_expression_alt_test",
file1 = "a_b_merged.yaml",
file2 = ":case_merge_expression_alt",
)
# Split into multiple documents
yq(
name = "case_split_expression",
srcs = ["multidoc.yaml"],
outs = [
"test_doc1.yml",
"test_doc2.yml",
],
args = [
"-s '.a'",
"--no-doc",
],
expression = ".",
)
diff_test(
name = "case_split_expression_test_1",
file1 = "split1.yaml",
file2 = "test_doc1.yml",
)
diff_test(
name = "case_split_expression_test_2",
file1 = "split2.yaml",
file2 = "test_doc2.yml",
)
# Outputs properties file
yq(
name = "case_output_properties",
srcs = ["a.yaml"],
outs = ["case_output_properties.properties"],
args = ["-o=props"],
expression = ".",
)
diff_test(
name = "case_output_properties_test",
file1 = "a.properties",
file2 = ":case_output_properties",
)
# Outputs properties file, outs not declared
yq(
name = "case_output_properties_no_outs",
srcs = ["a.yaml"],
args = ["-o=props"],
expression = ".",
)
diff_test(
name = "case_output_properties_no_outs_test",
file1 = "a.properties",
file2 = ":case_output_properties_no_outs",
)
# Outputs csv file
yq(
name = "case_output_csv",
srcs = ["array.yaml"],
outs = ["case_output_csv.csv"],
args = ["-o=c"],
expression = ".",
)
diff_test(
name = "case_output_csv_test",
file1 = "array.csv",
file2 = ":case_output_csv",
)
# Outputs csv file, outs not declared
yq(
name = "case_output_csv_no_outs",
srcs = ["array.yaml"],
args = ["-o=c"],
expression = ".",
)
diff_test(
name = "case_output_csv_no_outs_test",
file1 = "array.csv",
file2 = ":case_output_csv_no_outs",
)
# Outputs tsv file
yq(
name = "case_output_tsv",
srcs = ["array.yaml"],
outs = ["case_output_tsv.tsv"],
args = ["-o=t"],
expression = ".",
)
diff_test(
name = "case_output_tsv_test",
file1 = "array.tsv",
file2 = ":case_output_tsv",
)
# Outputs tsv file, outs not declared
yq(
name = "case_output_tsv_no_outs",
srcs = ["array.yaml"],
args = ["-o=t"],
expression = ".",
)
diff_test(
name = "case_output_tsv_no_outs_test",
file1 = "array.tsv",
file2 = ":case_output_tsv_no_outs",
)
# Convert xml to yaml
yq(
name = "case_convert_xml_to_yaml",
srcs = ["sample.xml"],
args = ["-p=xml"],
expression = ".",
)
diff_test(
name = "case_convert_xml_to_yaml_test",
file1 = "sample.yaml",
file2 = ":case_convert_xml_to_yaml",
)
# Outputs xml file
yq(
name = "case_output_xml",
srcs = ["a.yaml"],
outs = ["case_output_xml.xml"],
args = ["-o=xml"],
expression = ".",
)
diff_test(
name = "case_output_xml_test",
file1 = "a.xml",
file2 = ":case_output_xml",
)
# Outputs xml file, outs not declared
yq(
name = "case_output_xml_no_outs",
srcs = ["a.yaml"],
args = ["-o=xml"],
expression = ".",
)
diff_test(
name = "case_output_xml_no_outs_test",
file1 = "a.xml",
file2 = ":case_output_xml_no_outs",
)
# Merge two json documents together
yq(
name = "case_merge_expression_json",
srcs = [
"//lib/tests/jq:a.json",
"//lib/tests/jq:b.json",
],
args = ["-P"],
expression = ". as $item ireduce ({}; . * $item )",
)
diff_test(
name = "case_merge_expression_json_test",
file1 = "a_b_merged.yaml",
file2 = ":case_merge_expression_json",
)
# Call yq within a genrule
genrule(
name = "case_genrule",
srcs = ["a.yaml"],
outs = ["genrule_output.yaml"],
cmd = "$(YQ_BIN) '.' $(location a.yaml) > $@",
toolchains = ["@yq_toolchains//:resolved_toolchain"],
)
diff_test(
name = "case_genrule_test",
file1 = "genrule_output.yaml",
file2 = "a.yaml",
)