feat: add assert helper for zip contents
Helps to make sure that Python wheels are correctly constructed, as one example.
This commit is contained in:
parent
78f8151760
commit
083ff26ced
|
@ -2,6 +2,28 @@
|
|||
|
||||
Helpers for making test assertions
|
||||
|
||||
<a id="assert_archive_contains"></a>
|
||||
|
||||
## assert_archive_contains
|
||||
|
||||
<pre>
|
||||
assert_archive_contains(<a href="#assert_archive_contains-name">name</a>, <a href="#assert_archive_contains-archive">archive</a>, <a href="#assert_archive_contains-expected">expected</a>, <a href="#assert_archive_contains-type">type</a>, <a href="#assert_archive_contains-kwargs">kwargs</a>)
|
||||
</pre>
|
||||
|
||||
Assert that an archive file contains at least the given file entries.
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="assert_archive_contains-name"></a>name | name of the resulting sh_test target | none |
|
||||
| <a id="assert_archive_contains-archive"></a>archive | Label of the the .tar or .zip file | none |
|
||||
| <a id="assert_archive_contains-expected"></a>expected | Label of a file containing a (partial) file listing | none |
|
||||
| <a id="assert_archive_contains-type"></a>type | "tar" or "zip" | <code>"zip"</code> |
|
||||
| <a id="assert_archive_contains-kwargs"></a>kwargs | additional named arguments for the resulting sh_test | none |
|
||||
|
||||
|
||||
<a id="assert_contains"></a>
|
||||
|
||||
## assert_contains
|
||||
|
|
|
@ -113,3 +113,53 @@ def assert_json_matches(name, file1, file2, filter1 = ".", 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
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue