xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/envsubst/envsubst_tests.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
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"""Test for py_wheel."""
15
16load("@rules_testing//lib:analysis_test.bzl", "test_suite")
17load("//python/private:envsubst.bzl", "envsubst")  # buildifier: disable=bzl-visibility
18
19_basic_tests = []
20
21def _test_envsubst_braceless(env):
22    env.expect.that_str(
23        envsubst("--retries=$PIP_RETRIES", ["PIP_RETRIES"], {"PIP_RETRIES": "5"}.get),
24    ).equals("--retries=5")
25
26    env.expect.that_str(
27        envsubst("--retries=$PIP_RETRIES", [], {"PIP_RETRIES": "5"}.get),
28    ).equals("--retries=$PIP_RETRIES")
29
30    env.expect.that_str(
31        envsubst("--retries=$PIP_RETRIES", ["PIP_RETRIES"], {}.get),
32    ).equals("--retries=")
33
34_basic_tests.append(_test_envsubst_braceless)
35
36def _test_envsubst_braces_without_default(env):
37    env.expect.that_str(
38        envsubst("--retries=${PIP_RETRIES}", ["PIP_RETRIES"], {"PIP_RETRIES": "5"}.get),
39    ).equals("--retries=5")
40
41    env.expect.that_str(
42        envsubst("--retries=${PIP_RETRIES}", [], {"PIP_RETRIES": "5"}.get),
43    ).equals("--retries=${PIP_RETRIES}")
44
45    env.expect.that_str(
46        envsubst("--retries=${PIP_RETRIES}", ["PIP_RETRIES"], {}.get),
47    ).equals("--retries=")
48
49_basic_tests.append(_test_envsubst_braces_without_default)
50
51def _test_envsubst_braces_with_default(env):
52    env.expect.that_str(
53        envsubst("--retries=${PIP_RETRIES:-6}", ["PIP_RETRIES"], {"PIP_RETRIES": "5"}.get),
54    ).equals("--retries=5")
55
56    env.expect.that_str(
57        envsubst("--retries=${PIP_RETRIES:-6}", [], {"PIP_RETRIES": "5"}.get),
58    ).equals("--retries=${PIP_RETRIES:-6}")
59
60    env.expect.that_str(
61        envsubst("--retries=${PIP_RETRIES:-6}", ["PIP_RETRIES"], {}.get),
62    ).equals("--retries=6")
63
64_basic_tests.append(_test_envsubst_braces_with_default)
65
66def _test_envsubst_nested_both_vars(env):
67    env.expect.that_str(
68        envsubst(
69            "${HOME:-/home/$USER}",
70            ["HOME", "USER"],
71            {"HOME": "/home/testuser", "USER": "mockuser"}.get,
72        ),
73    ).equals("/home/testuser")
74
75_basic_tests.append(_test_envsubst_nested_both_vars)
76
77def _test_envsubst_nested_outer_var(env):
78    env.expect.that_str(
79        envsubst(
80            "${HOME:-/home/$USER}",
81            ["HOME"],
82            {"HOME": "/home/testuser", "USER": "mockuser"}.get,
83        ),
84    ).equals("/home/testuser")
85
86_basic_tests.append(_test_envsubst_nested_outer_var)
87
88def _test_envsubst_nested_no_vars(env):
89    env.expect.that_str(
90        envsubst(
91            "${HOME:-/home/$USER}",
92            [],
93            {"HOME": "/home/testuser", "USER": "mockuser"}.get,
94        ),
95    ).equals("${HOME:-/home/$USER}")
96
97    env.expect.that_str(
98        envsubst("${HOME:-/home/$USER}", ["HOME", "USER"], {}.get),
99    ).equals("/home/")
100
101_basic_tests.append(_test_envsubst_nested_no_vars)
102
103def _test_envsubst_nested_braces_inner_var(env):
104    env.expect.that_str(
105        envsubst(
106            "Home directory is ${HOME:-/home/$USER}.",
107            ["HOME", "USER"],
108            {"USER": "mockuser"}.get,
109        ),
110    ).equals("Home directory is /home/mockuser.")
111
112    env.expect.that_str(
113        envsubst(
114            "Home directory is ${HOME:-/home/$USER}.",
115            ["USER"],
116            {"USER": "mockuser"}.get,
117        ),
118    ).equals("Home directory is ${HOME:-/home/mockuser}.")
119
120_basic_tests.append(_test_envsubst_nested_braces_inner_var)
121
122def envsubst_test_suite(name):
123    test_suite(
124        name = name,
125        basic_tests = _basic_tests,
126    )
127