1*3a22c0a3SAlix# Copyright 2022 Google LLC. All rights reserved. 2*3a22c0a3SAlix# 3*3a22c0a3SAlix# Licensed under the Apache License, Version 2.0 (the License); 4*3a22c0a3SAlix# you may not use this file except in compliance with the License. 5*3a22c0a3SAlix# You may obtain a copy of the License at 6*3a22c0a3SAlix# 7*3a22c0a3SAlix# http://www.apache.org/licenses/LICENSE-2.0 8*3a22c0a3SAlix# 9*3a22c0a3SAlix# Unless required by applicable law or agreed to in writing, software 10*3a22c0a3SAlix# distributed under the License is distributed on an "AS IS" BASIS, 11*3a22c0a3SAlix# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*3a22c0a3SAlix# See the License for the specific language governing permissions and 13*3a22c0a3SAlix# limitations under the License. 14*3a22c0a3SAlix 15*3a22c0a3SAlix"""is_eligible_friend""" 16*3a22c0a3SAlix 17*3a22c0a3SAlixload("//:visibility.bzl", "RULES_DEFS_THAT_COMPILE_KOTLIN") 18*3a22c0a3SAlix 19*3a22c0a3SAlixvisibility(RULES_DEFS_THAT_COMPILE_KOTLIN) 20*3a22c0a3SAlix 21*3a22c0a3SAlixdef is_eligible_friend(target, friend): 22*3a22c0a3SAlix """ 23*3a22c0a3SAlix Determines if `target` is allowed to use `internal` members of `friend` 24*3a22c0a3SAlix 25*3a22c0a3SAlix To be eligible, one of: 26*3a22c0a3SAlix 1. `target` and `friend` in same pkg 27*3a22c0a3SAlix 2. `target` in `testing/` subpkg of `friend` pkg 28*3a22c0a3SAlix 3. `target` in `javatests/` pkg, `friend` in parallel `java/` pkg 29*3a22c0a3SAlix 4. `target` in `test/java/` pkg, `friend` in parallel `main/java/` pkg 30*3a22c0a3SAlix 31*3a22c0a3SAlix Args: 32*3a22c0a3SAlix target: (Target) The current target 33*3a22c0a3SAlix friend: (Target) A potential friend of `target` 34*3a22c0a3SAlix 35*3a22c0a3SAlix Returns: 36*3a22c0a3SAlix True if `friend` is an eligible friend of `target`. 37*3a22c0a3SAlix """ 38*3a22c0a3SAlix 39*3a22c0a3SAlix target_pkg = target.label.package + "/" 40*3a22c0a3SAlix friend_pkg = friend.label.package + "/" 41*3a22c0a3SAlix 42*3a22c0a3SAlix if target_pkg == friend_pkg: 43*3a22c0a3SAlix # Case 1 44*3a22c0a3SAlix return True 45*3a22c0a3SAlix 46*3a22c0a3SAlix if target_pkg.removesuffix("testing/") == friend_pkg: 47*3a22c0a3SAlix # Case 2 48*3a22c0a3SAlix return True 49*3a22c0a3SAlix 50*3a22c0a3SAlix if "javatests/" in target_pkg and "java/" in friend_pkg: 51*3a22c0a3SAlix # Case 3 52*3a22c0a3SAlix target_java_pkg = target_pkg.rsplit("javatests/", 1)[1] 53*3a22c0a3SAlix friend_java_pkg = friend_pkg.rsplit("java/", 1)[1] 54*3a22c0a3SAlix if target_java_pkg == friend_java_pkg: 55*3a22c0a3SAlix return True 56*3a22c0a3SAlix 57*3a22c0a3SAlix if ("test/java/" in target_pkg and "main/java/" in friend_pkg and 58*3a22c0a3SAlix True): 59*3a22c0a3SAlix # Case 4 60*3a22c0a3SAlix target_split = target_pkg.rsplit("test/java/", 1) 61*3a22c0a3SAlix friend_split = friend_pkg.rsplit("main/java/", 1) 62*3a22c0a3SAlix if target_split == friend_split: 63*3a22c0a3SAlix return True 64*3a22c0a3SAlix 65*3a22c0a3SAlix return False 66