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"""Tests for py_info.""" 15 16load("@rules_testing//lib:analysis_test.bzl", "analysis_test") 17load("@rules_testing//lib:test_suite.bzl", "test_suite") 18load("@rules_testing//lib:truth.bzl", "subjects") 19load("@rules_testing//lib:util.bzl", rt_util = "util") 20load("//python/private:builders.bzl", "builders") # buildifier: disable=bzl-visibility 21 22_tests = [] 23 24def _test_depset_builder(name): 25 rt_util.helper_target( 26 native.filegroup, 27 name = name + "_files", 28 ) 29 analysis_test( 30 name = name, 31 target = name + "_files", 32 impl = _test_depset_builder_impl, 33 ) 34 35def _test_depset_builder_impl(env, target): 36 _ = target # @unused 37 builder = builders.DepsetBuilder() 38 builder.set_order("preorder") 39 builder.add("one") 40 builder.add(["two"]) 41 builder.add(depset(["three"])) 42 builder.add([depset(["four"])]) 43 44 env.expect.that_str(builder.get_order()).equals("preorder") 45 46 actual = builder.build() 47 48 env.expect.that_collection(actual).contains_exactly([ 49 "one", 50 "two", 51 "three", 52 "four", 53 ]).in_order() 54 55_tests.append(_test_depset_builder) 56 57def _test_runfiles_builder(name): 58 rt_util.helper_target( 59 native.filegroup, 60 name = name + "_files", 61 srcs = ["f1.txt", "f2.txt", "f3.txt", "f4.txt", "f5.txt"], 62 ) 63 rt_util.helper_target( 64 native.filegroup, 65 name = name + "_runfiles", 66 data = ["runfile.txt"], 67 ) 68 analysis_test( 69 name = name, 70 impl = _test_runfiles_builder_impl, 71 targets = { 72 "files": name + "_files", 73 "runfiles": name + "_runfiles", 74 }, 75 ) 76 77def _test_runfiles_builder_impl(env, targets): 78 ctx = env.ctx 79 80 f1, f2, f3, f4, f5 = targets.files[DefaultInfo].files.to_list() 81 builder = builders.RunfilesBuilder() 82 builder.add(f1) 83 builder.add([f2]) 84 builder.add(depset([f3])) 85 86 rf1 = ctx.runfiles([f4]) 87 rf2 = ctx.runfiles([f5]) 88 builder.add(rf1) 89 builder.add([rf2]) 90 91 builder.add_targets([targets.runfiles]) 92 93 builder.root_symlinks["root_link"] = f1 94 builder.symlinks["regular_link"] = f1 95 96 actual = builder.build(ctx) 97 98 subject = subjects.runfiles(actual, meta = env.expect.meta) 99 subject.contains_exactly([ 100 "root_link", 101 "{workspace}/regular_link", 102 "{workspace}/tests/builders/f1.txt", 103 "{workspace}/tests/builders/f2.txt", 104 "{workspace}/tests/builders/f3.txt", 105 "{workspace}/tests/builders/f4.txt", 106 "{workspace}/tests/builders/f5.txt", 107 "{workspace}/tests/builders/runfile.txt", 108 ]) 109 110_tests.append(_test_runfiles_builder) 111 112def builders_test_suite(name): 113 test_suite( 114 name = name, 115 tests = _tests, 116 ) 117