From 1d816e011b3f29c182097865953f3c9d682ead10 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Fri, 29 Sep 2023 09:53:00 -0700 Subject: [PATCH] feat: add assert_directory_contains test to testing.bzl (#560) --- docs/testing.md | 21 +++++++ lib/testing.bzl | 55 +++++++++++++++++++ .../assert_directory_contains/BUILD.bazel | 19 +++++++ lib/tests/assert_directory_contains/dir_a/a | 1 + lib/tests/assert_directory_contains/dir_a/a2 | 1 + lib/tests/assert_directory_contains/dir_a/b/b | 1 + .../assert_directory_contains/dir_a/b/b2 | 1 + 7 files changed, 99 insertions(+) create mode 100644 lib/tests/assert_directory_contains/BUILD.bazel create mode 100644 lib/tests/assert_directory_contains/dir_a/a create mode 100644 lib/tests/assert_directory_contains/dir_a/a2 create mode 100644 lib/tests/assert_directory_contains/dir_a/b/b create mode 100644 lib/tests/assert_directory_contains/dir_a/b/b2 diff --git a/docs/testing.md b/docs/testing.md index f4ba272..e2c5436 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -50,6 +50,27 @@ Depends on bash, as it creates an sh_test target. | kwargs | additional named arguments for the resulting sh_test | none | + + +## assert_directory_contains + +
+assert_directory_contains(name, directory, expected, kwargs)
+
+ +Assert that a directory contains at least the given file entries. + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name | name of the resulting sh_test target | none | +| directory | Label of the directory artifact | none | +| expected | a (partial) file listing, either as a Label of a file containing it, or a list of strings | none | +| kwargs | additional named arguments for the resulting sh_test | none | + + ## assert_json_matches diff --git a/lib/testing.bzl b/lib/testing.bzl index dc3df1e..9648b55 100644 --- a/lib/testing.bzl +++ b/lib/testing.bzl @@ -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 + ) diff --git a/lib/tests/assert_directory_contains/BUILD.bazel b/lib/tests/assert_directory_contains/BUILD.bazel new file mode 100644 index 0000000..321b9db --- /dev/null +++ b/lib/tests/assert_directory_contains/BUILD.bazel @@ -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", + ], +) diff --git a/lib/tests/assert_directory_contains/dir_a/a b/lib/tests/assert_directory_contains/dir_a/a new file mode 100644 index 0000000..f6ea049 --- /dev/null +++ b/lib/tests/assert_directory_contains/dir_a/a @@ -0,0 +1 @@ +foobar \ No newline at end of file diff --git a/lib/tests/assert_directory_contains/dir_a/a2 b/lib/tests/assert_directory_contains/dir_a/a2 new file mode 100644 index 0000000..f6ea049 --- /dev/null +++ b/lib/tests/assert_directory_contains/dir_a/a2 @@ -0,0 +1 @@ +foobar \ No newline at end of file diff --git a/lib/tests/assert_directory_contains/dir_a/b/b b/lib/tests/assert_directory_contains/dir_a/b/b new file mode 100644 index 0000000..f6ea049 --- /dev/null +++ b/lib/tests/assert_directory_contains/dir_a/b/b @@ -0,0 +1 @@ +foobar \ No newline at end of file diff --git a/lib/tests/assert_directory_contains/dir_a/b/b2 b/lib/tests/assert_directory_contains/dir_a/b/b2 new file mode 100644 index 0000000..f6ea049 --- /dev/null +++ b/lib/tests/assert_directory_contains/dir_a/b/b2 @@ -0,0 +1 @@ +foobar \ No newline at end of file