Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add assert_directory_contains test to testing.bzl #560

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/testing.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions lib/testing.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
"""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
)
19 changes: 19 additions & 0 deletions lib/tests/assert_directory_contains/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
1 change: 1 addition & 0 deletions lib/tests/assert_directory_contains/dir_a/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar
1 change: 1 addition & 0 deletions lib/tests/assert_directory_contains/dir_a/a2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar
1 change: 1 addition & 0 deletions lib/tests/assert_directory_contains/dir_a/b/b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar
1 change: 1 addition & 0 deletions lib/tests/assert_directory_contains/dir_a/b/b2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar