xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/envsubst/envsubst_tests.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Copyright 2024 The Bazel Authors. All rights reserved.
2*60517a1eSAndroid Build Coastguard Worker#
3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*60517a1eSAndroid Build Coastguard Worker#
7*60517a1eSAndroid Build Coastguard Worker#    http://www.apache.org/licenses/LICENSE-2.0
8*60517a1eSAndroid Build Coastguard Worker#
9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
14*60517a1eSAndroid Build Coastguard Worker"""Test for py_wheel."""
15*60517a1eSAndroid Build Coastguard Worker
16*60517a1eSAndroid Build Coastguard Workerload("@rules_testing//lib:analysis_test.bzl", "test_suite")
17*60517a1eSAndroid Build Coastguard Workerload("//python/private:envsubst.bzl", "envsubst")  # buildifier: disable=bzl-visibility
18*60517a1eSAndroid Build Coastguard Worker
19*60517a1eSAndroid Build Coastguard Worker_basic_tests = []
20*60517a1eSAndroid Build Coastguard Worker
21*60517a1eSAndroid Build Coastguard Workerdef _test_envsubst_braceless(env):
22*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
23*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=$PIP_RETRIES", ["PIP_RETRIES"], {"PIP_RETRIES": "5"}.get),
24*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=5")
25*60517a1eSAndroid Build Coastguard Worker
26*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
27*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=$PIP_RETRIES", [], {"PIP_RETRIES": "5"}.get),
28*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=$PIP_RETRIES")
29*60517a1eSAndroid Build Coastguard Worker
30*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
31*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=$PIP_RETRIES", ["PIP_RETRIES"], {}.get),
32*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=")
33*60517a1eSAndroid Build Coastguard Worker
34*60517a1eSAndroid Build Coastguard Worker_basic_tests.append(_test_envsubst_braceless)
35*60517a1eSAndroid Build Coastguard Worker
36*60517a1eSAndroid Build Coastguard Workerdef _test_envsubst_braces_without_default(env):
37*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
38*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=${PIP_RETRIES}", ["PIP_RETRIES"], {"PIP_RETRIES": "5"}.get),
39*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=5")
40*60517a1eSAndroid Build Coastguard Worker
41*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
42*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=${PIP_RETRIES}", [], {"PIP_RETRIES": "5"}.get),
43*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=${PIP_RETRIES}")
44*60517a1eSAndroid Build Coastguard Worker
45*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
46*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=${PIP_RETRIES}", ["PIP_RETRIES"], {}.get),
47*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=")
48*60517a1eSAndroid Build Coastguard Worker
49*60517a1eSAndroid Build Coastguard Worker_basic_tests.append(_test_envsubst_braces_without_default)
50*60517a1eSAndroid Build Coastguard Worker
51*60517a1eSAndroid Build Coastguard Workerdef _test_envsubst_braces_with_default(env):
52*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
53*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=${PIP_RETRIES:-6}", ["PIP_RETRIES"], {"PIP_RETRIES": "5"}.get),
54*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=5")
55*60517a1eSAndroid Build Coastguard Worker
56*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
57*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=${PIP_RETRIES:-6}", [], {"PIP_RETRIES": "5"}.get),
58*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=${PIP_RETRIES:-6}")
59*60517a1eSAndroid Build Coastguard Worker
60*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
61*60517a1eSAndroid Build Coastguard Worker        envsubst("--retries=${PIP_RETRIES:-6}", ["PIP_RETRIES"], {}.get),
62*60517a1eSAndroid Build Coastguard Worker    ).equals("--retries=6")
63*60517a1eSAndroid Build Coastguard Worker
64*60517a1eSAndroid Build Coastguard Worker_basic_tests.append(_test_envsubst_braces_with_default)
65*60517a1eSAndroid Build Coastguard Worker
66*60517a1eSAndroid Build Coastguard Workerdef _test_envsubst_nested_both_vars(env):
67*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
68*60517a1eSAndroid Build Coastguard Worker        envsubst(
69*60517a1eSAndroid Build Coastguard Worker            "${HOME:-/home/$USER}",
70*60517a1eSAndroid Build Coastguard Worker            ["HOME", "USER"],
71*60517a1eSAndroid Build Coastguard Worker            {"HOME": "/home/testuser", "USER": "mockuser"}.get,
72*60517a1eSAndroid Build Coastguard Worker        ),
73*60517a1eSAndroid Build Coastguard Worker    ).equals("/home/testuser")
74*60517a1eSAndroid Build Coastguard Worker
75*60517a1eSAndroid Build Coastguard Worker_basic_tests.append(_test_envsubst_nested_both_vars)
76*60517a1eSAndroid Build Coastguard Worker
77*60517a1eSAndroid Build Coastguard Workerdef _test_envsubst_nested_outer_var(env):
78*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
79*60517a1eSAndroid Build Coastguard Worker        envsubst(
80*60517a1eSAndroid Build Coastguard Worker            "${HOME:-/home/$USER}",
81*60517a1eSAndroid Build Coastguard Worker            ["HOME"],
82*60517a1eSAndroid Build Coastguard Worker            {"HOME": "/home/testuser", "USER": "mockuser"}.get,
83*60517a1eSAndroid Build Coastguard Worker        ),
84*60517a1eSAndroid Build Coastguard Worker    ).equals("/home/testuser")
85*60517a1eSAndroid Build Coastguard Worker
86*60517a1eSAndroid Build Coastguard Worker_basic_tests.append(_test_envsubst_nested_outer_var)
87*60517a1eSAndroid Build Coastguard Worker
88*60517a1eSAndroid Build Coastguard Workerdef _test_envsubst_nested_no_vars(env):
89*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
90*60517a1eSAndroid Build Coastguard Worker        envsubst(
91*60517a1eSAndroid Build Coastguard Worker            "${HOME:-/home/$USER}",
92*60517a1eSAndroid Build Coastguard Worker            [],
93*60517a1eSAndroid Build Coastguard Worker            {"HOME": "/home/testuser", "USER": "mockuser"}.get,
94*60517a1eSAndroid Build Coastguard Worker        ),
95*60517a1eSAndroid Build Coastguard Worker    ).equals("${HOME:-/home/$USER}")
96*60517a1eSAndroid Build Coastguard Worker
97*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
98*60517a1eSAndroid Build Coastguard Worker        envsubst("${HOME:-/home/$USER}", ["HOME", "USER"], {}.get),
99*60517a1eSAndroid Build Coastguard Worker    ).equals("/home/")
100*60517a1eSAndroid Build Coastguard Worker
101*60517a1eSAndroid Build Coastguard Worker_basic_tests.append(_test_envsubst_nested_no_vars)
102*60517a1eSAndroid Build Coastguard Worker
103*60517a1eSAndroid Build Coastguard Workerdef _test_envsubst_nested_braces_inner_var(env):
104*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
105*60517a1eSAndroid Build Coastguard Worker        envsubst(
106*60517a1eSAndroid Build Coastguard Worker            "Home directory is ${HOME:-/home/$USER}.",
107*60517a1eSAndroid Build Coastguard Worker            ["HOME", "USER"],
108*60517a1eSAndroid Build Coastguard Worker            {"USER": "mockuser"}.get,
109*60517a1eSAndroid Build Coastguard Worker        ),
110*60517a1eSAndroid Build Coastguard Worker    ).equals("Home directory is /home/mockuser.")
111*60517a1eSAndroid Build Coastguard Worker
112*60517a1eSAndroid Build Coastguard Worker    env.expect.that_str(
113*60517a1eSAndroid Build Coastguard Worker        envsubst(
114*60517a1eSAndroid Build Coastguard Worker            "Home directory is ${HOME:-/home/$USER}.",
115*60517a1eSAndroid Build Coastguard Worker            ["USER"],
116*60517a1eSAndroid Build Coastguard Worker            {"USER": "mockuser"}.get,
117*60517a1eSAndroid Build Coastguard Worker        ),
118*60517a1eSAndroid Build Coastguard Worker    ).equals("Home directory is ${HOME:-/home/mockuser}.")
119*60517a1eSAndroid Build Coastguard Worker
120*60517a1eSAndroid Build Coastguard Worker_basic_tests.append(_test_envsubst_nested_braces_inner_var)
121*60517a1eSAndroid Build Coastguard Worker
122*60517a1eSAndroid Build Coastguard Workerdef envsubst_test_suite(name):
123*60517a1eSAndroid Build Coastguard Worker    test_suite(
124*60517a1eSAndroid Build Coastguard Worker        name = name,
125*60517a1eSAndroid Build Coastguard Worker        basic_tests = _basic_tests,
126*60517a1eSAndroid Build Coastguard Worker    )
127