Skip to content

Commit

Permalink
Allow some common rule args in verify_archive_test (#917)
Browse files Browse the repository at this point in the history
* Forward kwargs from verify_archive_test to py_test
* Add verify_archive test suite
* use visibility:private
* use allowlist for common attrs
* reorder tests to retrigger flaky CI
* Add a link to all supported kwargs
  • Loading branch information
krakeusz authored Jan 15, 2025
1 parent d3588e8 commit 12fed4b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/verify_archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ The execution time is O(# expected patterns * size of archive).

load("@rules_python//python:defs.bzl", "py_test")

# Attribute names common to all build rules. See https://bazel.build/reference/be/common-definitions
COMMON_BUILD_ATTR_NAMES = [
"tags",
"target_compatible_with",
"testonly",
"visibility",
]

# Attribute names common to all test rules. See https://bazel.build/reference/be/common-definitions#common-attributes-tests
COMMON_TEST_ATTR_NAMES = COMMON_BUILD_ATTR_NAMES + [
"size",
"timeout",
"flaky",
]

def _gen_verify_archive_test_main_impl(ctx):
ctx.actions.expand_template(
template = ctx.file._template,
Expand Down Expand Up @@ -95,8 +110,8 @@ def verify_archive_test(
must_not_contain_regex = None,
min_size = 1,
max_size = -1,
tags = None,
verify_links = None):
verify_links = None,
**kwargs):
"""Tests that an archive contains specific file patterns.
This test is used to verify that an archive contains the expected content.
Expand All @@ -109,8 +124,9 @@ def verify_archive_test(
must_not_contain_regex: A list of path regexes which must not appear in the archive.
min_size: The minimum number of entries which must be in the archive.
max_size: The maximum number of entries which must be in the archive.
tags: standard meaning
verify_links: Dict keyed by paths which must appear, and be symlinks to their values.
**kwargs: The args to be passed to the underlying rules, if supported.
See https://github.com/bazelbuild/rules_pkg/blob/main/pkg/verify_archive.bzl for the full list.
"""
test_src = name + "__internal_main.py"
_gen_verify_archive_test_main(
Expand All @@ -124,13 +140,14 @@ def verify_archive_test(
must_not_contain_regex = must_not_contain_regex,
min_size = min_size,
max_size = max_size,
tags = tags,
verify_links = verify_links,
**{key: kwargs[key] for key in COMMON_BUILD_ATTR_NAMES if key in kwargs}
)
py_test(
name = name,
srcs = [":" + test_src],
main = test_src,
data = [target],
python_version = "PY3",
**{key: kwargs[key] for key in COMMON_TEST_ATTR_NAMES if key in kwargs}
)
69 changes: 69 additions & 0 deletions tests/verify_archive/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2025 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -*- coding: utf-8 -*-
"""Tests for verify_archive."""

load("//pkg:tar.bzl", "pkg_tar")
load("//pkg:verify_archive.bzl", "verify_archive_test")

# Test data

pkg_tar(
name = "loremipsum_tar",
srcs = [
"//tests:loremipsum_txt",
],
)

pkg_tar(
name = "loremipsum_tar_test_only",
testonly = True,
srcs = [
"//tests:loremipsum_txt",
],
)

# Test cases

verify_archive_test(
name = "test_py_test_attrs_is_allowed",
size = "small",
timeout = "moderate",
flaky = True,
target = ":loremipsum_tar",
)

verify_archive_test(
name = "test_testonly_is_allowed",
testonly = True,
target = ":loremipsum_tar_test_only",
)

verify_archive_test(
name = "test_visibility_is_allowed",
target = ":loremipsum_tar",
visibility = ["//visibility:private"],
)

verify_archive_test(
name = "test_target_compatible_with_is_allowed",
target = ":loremipsum_tar",
target_compatible_with = ["@platforms//cpu:x86_64"],
)

verify_archive_test(
name = "test_tags_are_allowed",
tags = ["test_tag"],
target = ":loremipsum_tar",
)

0 comments on commit 12fed4b

Please sign in to comment.