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/jq/BUILD.bazel
Alex Eagle 32882befdf feat: let jq act like copy_to_bin
This lets you have a file in the output tree with the same path as a source file, but modified.
2022-06-21 18:40:29 -07:00

138 lines
2.6 KiB
Python

load("//lib/tests/jq:diff_test.bzl", "diff_test")
load("//lib:jq.bzl", "jq")
exports_files([
"a_pretty.json",
"a.json",
"b.json",
])
# Identity filter produces identical json
jq(
name = "case_dot_filter",
srcs = ["a.json"],
filter = ".",
)
diff_test(
name = "case_dot_filter_test",
file1 = "a_pretty.json",
file2 = ":case_dot_filter",
)
# Merge filter with slurp merges two jsons
jq(
name = "case_merge_filter",
srcs = [
"a.json",
"b.json",
],
args = ["--slurp"],
filter = ".[0] * .[1]",
)
diff_test(
name = "case_merge_filter_test",
file1 = "a_b_merged.json",
file2 = ":case_merge_filter",
)
# Use predeclared output
jq(
name = "case_predeclared_output",
srcs = ["a.json"],
out = "foo.json",
filter = ".",
)
diff_test(
name = "case_predeclared_output_test",
file1 = "a_pretty.json",
file2 = "foo.json",
)
# No sources produces null (equivalent to --null-input)
jq(
name = "case_no_sources",
srcs = [],
filter = ".",
)
diff_test(
name = "case_no_sources_test",
file1 = ":case_no_sources",
file2 = "null.json",
)
# Sources with --null-input flag produces null
jq(
name = "case_null_input_flag",
srcs = ["a.json"],
args = ["--null-input"],
filter = ".",
)
diff_test(
name = "case_null_input_flag_test",
file1 = ":case_null_input_flag",
file2 = "null.json",
)
# Load filter from file
jq(
name = "case_filter_file",
srcs = [
"a.json",
"b.json",
],
args = ["--slurp"],
filter_file = "merge_filter.txt",
)
diff_test(
name = "case_filter_file_test",
file1 = "a_b_merged.json",
file2 = ":case_filter_file",
)
# Call jq within a genrule
genrule(
name = "case_genrule",
srcs = ["a.json"],
outs = ["genrule_output.json"],
cmd = "$(JQ_BIN) '.' $(location a.json) > $@",
toolchains = ["@jq_toolchains//:resolved_toolchain"],
)
diff_test(
name = "case_genrule_test",
file1 = "genrule_output.json",
file2 = "a_pretty.json",
)
# Raw input (--raw-input / -R)
jq(
name = "case_raw_input",
srcs = ["raw.txt"],
args = [
"--raw-input",
"--slurp",
],
filter = "split(\"\\n\")",
)
diff_test(
name = "case_raw_input_test",
file1 = ":case_raw_input",
file2 = "raw_expected.json",
)
# Similar to copy_to_bin, this will create a file a.json in the output folder.
# That's the same path as an input file, but that's okay as long as the jq
# rule doesn't pre-declare that output.
jq(
name = "a",
srcs = ["a.json"],
filter = """ .foo = "baz" """,
)