feat: add assert_directory_contains test to testing.bzl (#560)

This commit is contained in:
Greg Magolan 2023-09-29 09:53:00 -07:00 committed by GitHub
parent e8bbdbfda6
commit 1d816e011b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 99 additions and 0 deletions

21
docs/testing.md generated
View File

@ -50,6 +50,27 @@ Depends on bash, as it creates an sh_test target.
| <a id="assert_contains-kwargs"></a>kwargs | additional named arguments for the resulting sh_test | none |
<a id="assert_directory_contains"></a>
## assert_directory_contains
<pre>
assert_directory_contains(<a href="#assert_directory_contains-name">name</a>, <a href="#assert_directory_contains-directory">directory</a>, <a href="#assert_directory_contains-expected">expected</a>, <a href="#assert_directory_contains-kwargs">kwargs</a>)
</pre>
Assert that a directory contains at least the given file entries.
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="assert_directory_contains-name"></a>name | name of the resulting sh_test target | none |
| <a id="assert_directory_contains-directory"></a>directory | Label of the directory artifact | none |
| <a id="assert_directory_contains-expected"></a>expected | a (partial) file listing, either as a Label of a file containing it, or a list of strings | none |
| <a id="assert_directory_contains-kwargs"></a>kwargs | additional named arguments for the resulting sh_test | none |
<a id="assert_json_matches"></a>
## assert_json_matches

View File

@ -193,3 +193,58 @@ def assert_archive_contains(name, archive, expected, type = None, **kwargs):
timeout = "short",
**kwargs
)
def assert_directory_contains(name, directory, expected, **kwargs):
"""Assert that a directory contains at least the given file entries.
Args:
name: name of the resulting sh_test target
directory: Label of the directory artifact
expected: a (partial) file listing, either as a Label of a file containing it, or a list of strings
**kwargs: additional named arguments for the resulting sh_test
"""
# -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
expected_name = "_expected_" + name
if types.is_list(expected):
write_file(
name = expected_name,
out = expected_name + ".mf",
content = expected,
)
else:
expected_name = expected
write_file(
name = script_name,
out = "assert_{}.sh".format(name),
content = [
"#!/usr/bin/env bash",
"actual=$(mktemp)",
"pushd $1 > /dev/null",
"find . -type l,f | cut -b 3- > $actual",
"popd > /dev/null",
"# 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 directory' $1".format(expected_name),
" exit 1",
"fi",
],
)
native.sh_test(
name = name,
srcs = [script_name],
args = ["$(rootpath %s)" % directory, "$(rootpath %s)" % expected_name],
data = [directory, expected_name],
timeout = "short",
**kwargs
)

View File

@ -0,0 +1,19 @@
load("//lib:copy_directory.bzl", "copy_directory")
load("//lib:testing.bzl", "assert_directory_contains")
copy_directory(
name = "a",
src = "dir_a",
out = "a",
)
assert_directory_contains(
name = "check_a",
directory = ":a",
expected = [
"a",
"a2",
"b/b",
"b/b2",
],
)

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar

View File

@ -0,0 +1 @@
foobar