Skip to content

Commit

Permalink
Implement bzl_library test using standard analysistest machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
tetromino committed Apr 24, 2024
1 parent 5badc99 commit 015248d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
20 changes: 10 additions & 10 deletions tests/bzl_library/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("//:bzl_library.bzl", "bzl_library")
load("//rules:build_test.bzl", "build_test")
load(":bzl_library_consumer.bzl", "bzl_library_consumer")
load(":bzl_library_test.bzl", "bzl_library_test")

filegroup(
name = "a",
Expand All @@ -21,12 +20,13 @@ bzl_library(
],
)

bzl_library_consumer(
name = "consumer",
target = ":c",
)

build_test(
name = "test_bzl_library",
targets = [":consumer"],
bzl_library_test(
name = "bzl_library_test",
expected_srcs = ["c.bzl"],
expected_transitive_srcs = [
"a.bzl",
"b.bzl",
"c.bzl",
],
target_under_test = ":c",
)
20 changes: 0 additions & 20 deletions tests/bzl_library/bzl_library_consumer.bzl

This file was deleted.

40 changes: 40 additions & 0 deletions tests/bzl_library/bzl_library_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Unit tests for bzl_library"""

load("//:bzl_library.bzl", "StarlarkLibraryInfo")
load("//lib:sets.bzl", "sets")
load("//lib:unittest.bzl", "analysistest", "asserts")

def _assert_same_files(env, expected_file_targets, actual_files):
"""Assertion that a list of expected file targets and an actual list or depset of files contain the same files"""
expected_files = []
for target in expected_file_targets:
target_files = target[DefaultInfo].files.to_list()
asserts.true(env, len(target_files) == 1, "expected_file_targets must contain only file targets")
expected_files.append(target_files[0])
if type(actual_files) == "depset":
actual_files = actual_files.to_list()
asserts.set_equals(env = env, expected = sets.make(expected_files), actual = sets.make(actual_files))

def _bzl_library_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)
_assert_same_files(env, ctx.attr.expected_srcs, target_under_test[StarlarkLibraryInfo].srcs)
_assert_same_files(env, ctx.attr.expected_transitive_srcs, target_under_test[StarlarkLibraryInfo].transitive_srcs)
_assert_same_files(env, ctx.attr.expected_transitive_srcs, target_under_test[DefaultInfo].files)
return analysistest.end(env)

bzl_library_test = analysistest.make(
impl = _bzl_library_test_impl,
attrs = {
"expected_srcs": attr.label_list(
mandatory = True,
allow_files = True,
doc = "Expected direct srcs in target_under_test's providers",
),
"expected_transitive_srcs": attr.label_list(
mandatory = True,
allow_files = True,
doc = "Expected transitive srcs in target_under_test's providers",
),
},
)

0 comments on commit 015248d

Please sign in to comment.