• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 relative_path computation"
16
17load("@rules_testing//lib:test_suite.bzl", "test_suite")
18load("//python/private:py_executable_bazel.bzl", "relative_path")  # buildifier: disable=bzl-visibility
19
20_tests = []
21
22def _relative_path_test(env):
23    # Basic test cases
24
25    env.expect.that_str(
26        relative_path(
27            from_ = "a/b",
28            to = "c/d",
29        ),
30    ).equals("../../c/d")
31
32    env.expect.that_str(
33        relative_path(
34            from_ = "a/b/c",
35            to = "a/d",
36        ),
37    ).equals("../../d")
38    env.expect.that_str(
39        relative_path(
40            from_ = "a/b/c",
41            to = "a/b/c/d/e",
42        ),
43    ).equals("d/e")
44
45    # Real examples
46
47    # external py_binary uses external python runtime
48    env.expect.that_str(
49        relative_path(
50            from_ = "other_repo~/python/private/_py_console_script_gen_py.venv/bin",
51            to = "rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
52        ),
53    ).equals(
54        "../../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
55    )
56
57    # internal py_binary uses external python runtime
58    env.expect.that_str(
59        relative_path(
60            from_ = "_main/test/version_default.venv/bin",
61            to = "rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
62        ),
63    ).equals(
64        "../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
65    )
66
67    # external py_binary uses internal python runtime
68    env.expect.that_str(
69        relative_path(
70            from_ = "other_repo~/python/private/_py_console_script_gen_py.venv/bin",
71            to = "_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
72        ),
73    ).equals(
74        "../../../../../_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
75    )
76
77    # internal py_binary uses internal python runtime
78    env.expect.that_str(
79        relative_path(
80            from_ = "_main/scratch/main.venv/bin",
81            to = "_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
82        ),
83    ).equals(
84        "../../../python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
85    )
86
87_tests.append(_relative_path_test)
88
89def relative_path_test_suite(*, name):
90    test_suite(name = name, basic_tests = _tests)
91