1# Copyright 2024 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Unit tests for bzl_library""" 16 17load("//:bzl_library.bzl", "StarlarkLibraryInfo") 18load("//lib:sets.bzl", "sets") 19load("//lib:unittest.bzl", "analysistest", "asserts") 20 21def _assert_same_files(env, expected_file_targets, actual_files): 22 """Assertion that a list of expected file targets and an actual list or depset of files contain the same files""" 23 expected_files = [] 24 for target in expected_file_targets: 25 target_files = target[DefaultInfo].files.to_list() 26 asserts.true(env, len(target_files) == 1, "expected_file_targets must contain only file targets") 27 expected_files.append(target_files[0]) 28 if type(actual_files) == "depset": 29 actual_files = actual_files.to_list() 30 asserts.set_equals(env = env, expected = sets.make(expected_files), actual = sets.make(actual_files)) 31 32def _bzl_library_test_impl(ctx): 33 env = analysistest.begin(ctx) 34 target_under_test = analysistest.target_under_test(env) 35 _assert_same_files(env, ctx.attr.expected_srcs, target_under_test[StarlarkLibraryInfo].srcs) 36 _assert_same_files(env, ctx.attr.expected_transitive_srcs, target_under_test[StarlarkLibraryInfo].transitive_srcs) 37 _assert_same_files(env, ctx.attr.expected_transitive_srcs, target_under_test[DefaultInfo].files) 38 return analysistest.end(env) 39 40bzl_library_test = analysistest.make( 41 impl = _bzl_library_test_impl, 42 attrs = { 43 "expected_srcs": attr.label_list( 44 mandatory = True, 45 allow_files = True, 46 doc = "Expected direct srcs in target_under_test's providers", 47 ), 48 "expected_transitive_srcs": attr.label_list( 49 mandatory = True, 50 allow_files = True, 51 doc = "Expected transitive srcs in target_under_test's providers", 52 ), 53 }, 54) 55