2021-12-09 00:47:45 +00:00
|
|
|
load("//lib/tests/jq:diff_test.bzl", "diff_test")
|
|
|
|
load("//lib:jq.bzl", "jq")
|
|
|
|
|
2022-04-20 04:45:06 +00:00
|
|
|
exports_files([
|
|
|
|
"a_pretty.json",
|
|
|
|
"a.json",
|
|
|
|
"b.json",
|
|
|
|
])
|
|
|
|
|
2021-12-09 00:47:45 +00:00
|
|
|
# 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",
|
|
|
|
)
|
|
|
|
|
2021-12-20 20:47:39 +00:00
|
|
|
# Load filter from file
|
|
|
|
jq(
|
|
|
|
name = "case_filter_file",
|
2022-02-11 15:52:01 +00:00
|
|
|
srcs = [
|
|
|
|
"a.json",
|
|
|
|
"b.json",
|
|
|
|
],
|
2021-12-20 20:47:39 +00:00
|
|
|
args = ["--slurp"],
|
|
|
|
filter_file = "merge_filter.txt",
|
|
|
|
)
|
|
|
|
|
|
|
|
diff_test(
|
|
|
|
name = "case_filter_file_test",
|
|
|
|
file1 = "a_b_merged.json",
|
|
|
|
file2 = ":case_filter_file",
|
|
|
|
)
|
|
|
|
|
2021-12-09 00:47:45 +00:00
|
|
|
# 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",
|
|
|
|
)
|
2022-06-17 16:46:11 +00:00
|
|
|
|
|
|
|
# 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",
|
|
|
|
)
|
2022-06-22 01:27:49 +00:00
|
|
|
|
|
|
|
# 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" """,
|
|
|
|
)
|