| 
 | 1 | +# Copyright 2023 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 yaml.bzl"  | 
 | 16 | + | 
 | 17 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest")  | 
 | 18 | +load("//python/private:py_executable_bazel.bzl", "relative_path")  # buildifier: disable=bzl-visibility  | 
 | 19 | + | 
 | 20 | +def _relative_path_test_impl(ctx):  | 
 | 21 | +    env = unittest.begin(ctx)  | 
 | 22 | + | 
 | 23 | +    # Basic test cases  | 
 | 24 | + | 
 | 25 | +    asserts.equals(  | 
 | 26 | +        env,  | 
 | 27 | +        "../../c/d",  | 
 | 28 | +        relative_path(  | 
 | 29 | +            from_ = "a/b",  | 
 | 30 | +            to = "c/d",  | 
 | 31 | +        ),  | 
 | 32 | +    )  | 
 | 33 | + | 
 | 34 | +    asserts.equals(  | 
 | 35 | +        env,  | 
 | 36 | +        "../../c/d",  | 
 | 37 | +        relative_path(  | 
 | 38 | +            from_ = "../a/b",  | 
 | 39 | +            to = "../c/d",  | 
 | 40 | +        ),  | 
 | 41 | +    )  | 
 | 42 | + | 
 | 43 | +    asserts.equals(  | 
 | 44 | +        env,  | 
 | 45 | +        "../../../c/d",  | 
 | 46 | +        relative_path(  | 
 | 47 | +            from_ = "../a/b",  | 
 | 48 | +            to = "../../c/d",  | 
 | 49 | +        ),  | 
 | 50 | +    )  | 
 | 51 | + | 
 | 52 | +    asserts.equals(  | 
 | 53 | +        env,  | 
 | 54 | +        "../../d",  | 
 | 55 | +        relative_path(  | 
 | 56 | +            from_ = "a/b/c",  | 
 | 57 | +            to = "a/d",  | 
 | 58 | +        ),  | 
 | 59 | +    )  | 
 | 60 | + | 
 | 61 | +    asserts.equals(  | 
 | 62 | +        env,  | 
 | 63 | +        "d/e",  | 
 | 64 | +        relative_path(  | 
 | 65 | +            from_ = "a/b/c",  | 
 | 66 | +            to = "a/b/c/d/e",  | 
 | 67 | +        ),  | 
 | 68 | +    )  | 
 | 69 | + | 
 | 70 | +    # Real examples  | 
 | 71 | + | 
 | 72 | +    # external py_binary uses external python runtime  | 
 | 73 | +    asserts.equals(  | 
 | 74 | +        env,  | 
 | 75 | +        "../../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",  | 
 | 76 | +        relative_path(  | 
 | 77 | +            from_ = "../rules_python~/python/private/_py_console_script_gen_py.venv/bin",  | 
 | 78 | +            to = "../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",  | 
 | 79 | +        ),  | 
 | 80 | +    )  | 
 | 81 | + | 
 | 82 | +    # internal py_binary uses external python runtime  | 
 | 83 | +    asserts.equals(  | 
 | 84 | +        env,  | 
 | 85 | +        "../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",  | 
 | 86 | +        relative_path(  | 
 | 87 | +            from_ = "test/version_default.venv/bin",  | 
 | 88 | +            to = "../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",  | 
 | 89 | +        ),  | 
 | 90 | +    )  | 
 | 91 | + | 
 | 92 | +    # external py_binary uses internal python runtime  | 
 | 93 | +    # asserts.equals(  | 
 | 94 | +    #    env,  | 
 | 95 | +    #    "???",  | 
 | 96 | +    #    relative_path(  | 
 | 97 | +    #        from_ = "../rules_python~/python/private/_py_console_script_gen_py.venv/bin",  | 
 | 98 | +    #        to = "python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",  | 
 | 99 | +    #    ),  | 
 | 100 | +    #)  | 
 | 101 | +    # ^ TODO: Technically we can infer ".." to be the workspace name?  | 
 | 102 | + | 
 | 103 | +    # internal py_binary uses internal python runtime  | 
 | 104 | +    asserts.equals(  | 
 | 105 | +        env,  | 
 | 106 | +        "../../../python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",  | 
 | 107 | +        relative_path(  | 
 | 108 | +            from_ = "scratch/main.venv/bin",  | 
 | 109 | +            to = "python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",  | 
 | 110 | +        ),  | 
 | 111 | +    )  | 
 | 112 | + | 
 | 113 | +    return unittest.end(env)  | 
 | 114 | + | 
 | 115 | +relative_path_test = unittest.make(  | 
 | 116 | +    _relative_path_test_impl,  | 
 | 117 | +    attrs = {},  | 
 | 118 | +)  | 
 | 119 | + | 
 | 120 | +def relative_path_test_suite(name):  | 
 | 121 | +    unittest.suite(name, relative_path_test)  | 
0 commit comments