2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-11-25 11:32:33 +00:00
bazel-lib/lib/testing.bzl

166 lines
4.8 KiB
Python
Raw Normal View History

"Helpers for making test assertions"
load("//lib:params_file.bzl", "params_file")
load("@bazel_skylib//lib:types.bzl", "types")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//lib:utils.bzl", "default_timeout")
load("//lib:jq.bzl", "jq")
def assert_contains(name, actual, expected, size = None, timeout = None):
"""Generates a test target which fails if the file doesn't contain the string.
Depends on bash, as it creates an sh_test target.
Args:
name: target to create
actual: Label of a file
expected: a string which should appear in the file
size: the size attribute of the test target
timeout: the timeout attribute of the test target
"""
test_sh = "_{}_test.sh".format(name)
write_file(
name = "_" + name,
out = test_sh,
content = [
"#!/usr/bin/env bash",
"set -o errexit",
"grep --fixed-strings '{}' $1".format(expected),
],
)
native.sh_test(
name = name,
srcs = [test_sh],
args = ["$(rootpath %s)" % actual],
size = size,
timeout = default_timeout(size, timeout),
data = [actual],
)
def assert_outputs(name, actual, expected):
"""Assert that the default outputs of a target are the expected ones.
Args:
name: name of the resulting diff_test
actual: string of the label to check the outputs
expected: a list of rootpaths of expected outputs, as they would appear in a runfiles manifest
"""
if not types.is_list(expected):
fail("expected should be a list of strings")
params_file(
name = "_actual_" + name,
data = [actual],
args = ["$(rootpaths {})".format(actual)],
out = "_{}_outputs.txt".format(name),
)
write_file(
name = "_expected_ " + name,
content = expected,
out = "_expected_{}.txt".format(name),
)
diff_test(
name = name,
file1 = "_expected_ " + name,
file2 = "_actual_" + name,
)
def assert_json_matches(name, file1, file2, filter1 = ".", filter2 = "."):
"""Assert that the given json files have the same semantic content.
Uses jq to filter each file. The default value of `"."` as the filter
means to compare the whole file.
2023-01-09 19:22:36 +00:00
See the [jq rule](./jq.md#jq) for more about the filter expressions as well as
setup notes for the `jq` toolchain.
Args:
name: name of resulting diff_test target
file1: a json file
file2: another json file
filter1: a jq filter to apply to file1
filter2: a jq filter to apply to file2
"""
name1 = "_{}_jq1".format(name)
name2 = "_{}_jq2".format(name)
jq(
name = name1,
srcs = [file1],
filter = filter1,
)
jq(
name = name2,
srcs = [file2],
filter = filter2,
)
diff_test(
name = name,
file1 = name1,
file2 = name2,
failure_message = "'{}' from {} doesn't match '{}' from {}".format(
filter1,
file1,
filter2,
file2,
),
)
def assert_archive_contains(name, archive, expected, type = "zip", **kwargs):
"""Assert that an archive file contains at least the given file entries.
Args:
name: name of the resulting sh_test target
archive: Label of the the .tar or .zip file
expected: Label of a file containing a (partial) file listing
type: "tar" or "zip"
**kwargs: additional named arguments for the resulting sh_test
"""
if not type in ["tar", "zip"]:
fail("type must be 'tar' or 'zip'")
# Command to list the files in the archive
command = "zipinfo -1" if type == "zip" else "tar -tf"
# -f $actual: use this file to contain one pattern per line
# -F: treat each pattern as a plain string, not a regex
# -x: match whole lines only
# -v: only print lines which don't match
grep = "grep -F -x -v -f $actual"
script_name = "_gen_assert_" + name
write_file(
name = script_name,
out = "assert_{}.sh".format(name),
content = [
"#!/usr/bin/env bash",
"actual=$(mktemp)",
"{} $1 > $actual".format(command),
"# Grep exits 1 if no matches, which is success for this test.",
"if {} $2; then".format(grep),
" echo",
" echo 'ERROR: above line(s) appeared in {} but are not present in the archive' $1".format(expected),
" exit 1",
"fi",
],
)
native.sh_test(
name = name,
srcs = [script_name],
args = ["$(rootpath %s)" % archive, "$(rootpath %s)" % expected],
data = [archive, expected],
timeout = "short",
**kwargs
)