Skip to content

Commit

Permalink
feat: add assert_directory_contains test to testing.bzl
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Sep 29, 2023
1 parent e8bbdbf commit 9419432
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
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):
"""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 lf | 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

0 comments on commit 9419432

Please sign in to comment.