xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/repository_utils/repository_utils_test.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Unit tests for repository_utils.bzl."""
2
3load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
4load("//rust/platform:triple.bzl", "triple")
5
6# buildifier: disable=bzl-visibility
7load(
8    "//rust/private:repository_utils.bzl",
9    "lookup_tool_sha256",
10    "produce_tool_path",
11    "produce_tool_suburl",
12    "select_rust_version",
13)
14
15_PLATFORM_TRIPLE = triple("x86_64-unknown-linux-gnu")
16
17def _produce_tool_suburl_test_impl(ctx):
18    env = unittest.begin(ctx)
19    asserts.equals(
20        env,
21        "2020-05-22/rust-std-nightly-x86_64-unknown-linux-gnu",
22        produce_tool_suburl(
23            iso_date = "2020-05-22",
24            tool_name = "rust-std",
25            version = "nightly",
26            target_triple = _PLATFORM_TRIPLE,
27        ),
28    )
29    asserts.equals(
30        env,
31        "rust-std-nightly-x86_64-unknown-linux-gnu",
32        produce_tool_suburl(
33            tool_name = "rust-std",
34            version = "nightly",
35            target_triple = _PLATFORM_TRIPLE,
36        ),
37    )
38    asserts.equals(
39        env,
40        "2020-05-22/rust-src-nightly",
41        produce_tool_suburl(
42            iso_date = "2020-05-22",
43            tool_name = "rust-src",
44            version = "nightly",
45            target_triple = None,
46        ),
47    )
48    asserts.equals(
49        env,
50        "rust-src-nightly",
51        produce_tool_suburl(
52            tool_name = "rust-src",
53            version = "nightly",
54            target_triple = None,
55        ),
56    )
57    asserts.equals(
58        env,
59        "rust-src-1.54.0",
60        produce_tool_suburl(
61            iso_date = "2021-08-15",
62            tool_name = "rust-src",
63            version = "1.54.0",
64            target_triple = None,
65        ),
66    )
67    return unittest.end(env)
68
69def _produce_tool_path_test_impl(ctx):
70    env = unittest.begin(ctx)
71    asserts.equals(
72        env,
73        "rust-std-nightly-x86_64-unknown-linux-gnu",
74        produce_tool_path(
75            tool_name = "rust-std",
76            version = "nightly",
77            target_triple = _PLATFORM_TRIPLE,
78        ),
79    )
80    asserts.equals(
81        env,
82        "rust-src-nightly",
83        produce_tool_path(
84            tool_name = "rust-src",
85            version = "nightly",
86            target_triple = None,
87        ),
88    )
89    return unittest.end(env)
90
91def _lookup_tool_sha256_test_impl(ctx):
92    env = unittest.begin(ctx)
93
94    # Release version included in //rust:known_shas.bzl
95    asserts.equals(
96        env,
97        ("rustc-1.65.0-x86_64-unknown-linux-gnu.tar.xz", "62b89786e195fc5a8a262f83118d6689832b24228c9d303cba8ac14dc1e9adc8"),
98        lookup_tool_sha256(
99            ctx,
100            tool_name = "rustc",
101            target_triple = _PLATFORM_TRIPLE,
102            version = "1.65.0",
103            iso_date = "2022-11-02",
104        ),
105    )
106
107    # Values in //rust:known_shas.bzl override sha256 arg
108    asserts.equals(
109        env,
110        ("rustc-1.65.0-x86_64-unknown-linux-gnu.tar.xz", "62b89786e195fc5a8a262f83118d6689832b24228c9d303cba8ac14dc1e9adc8"),
111        lookup_tool_sha256(
112            ctx,
113            tool_name = "rustc",
114            target_triple = _PLATFORM_TRIPLE,
115            version = "1.65.0",
116            iso_date = "2022-11-02",
117        ),
118    )
119
120    # Nightly version included in //rust:known_shas.bzl
121    asserts.equals(
122        env,
123        ("2022-11-02/rust-std-nightly-x86_64-unknown-linux-gnu.tar.xz", "ea01d3cd6c6729cd8ebb55a7702eda2347451e304b58807361e020065a579d96"),
124        lookup_tool_sha256(
125            ctx,
126            tool_name = "rust-std",
127            target_triple = _PLATFORM_TRIPLE,
128            version = "nightly",
129            iso_date = "2022-11-02",
130        ),
131    )
132
133    # Lookup failure (returns "") for a nightly version not included in //rust:known_shas.bzl
134    asserts.equals(
135        env,
136        ("2022-11-01/rust-std-nightly-x86_64-unknown-linux-gnu.tar.xz", ""),
137        lookup_tool_sha256(
138            ctx,
139            tool_name = "rust-std",
140            target_triple = _PLATFORM_TRIPLE,
141            version = "nightly",
142            iso_date = "2022-11-01",
143        ),
144    )
145
146    # A nightly version not included in //rust:known_shas.bzl falls back to sha256 arg
147    asserts.equals(
148        env,
149        ("2022-11-01/rust-std-nightly-x86_64-unknown-linux-gnu.tar.xz", ""),
150        lookup_tool_sha256(
151            ctx,
152            tool_name = "rust-std",
153            target_triple = _PLATFORM_TRIPLE,
154            version = "nightly",
155            iso_date = "2022-11-01",
156        ),
157    )
158    return unittest.end(env)
159
160def _select_rust_version_test_impl(ctx):
161    env = unittest.begin(ctx)
162
163    # Show stable releases take highest priority
164    asserts.equals(
165        env,
166        "1.66.0",
167        select_rust_version(
168            versions = [
169                "1.66.0",
170                "beta/2022-12-15",
171                "nightly/2022-12-15",
172            ],
173        ),
174    )
175
176    # Show nightly releases take priority over beta
177    asserts.equals(
178        env,
179        "nightly/2022-12-15",
180        select_rust_version(
181            versions = [
182                "beta/2022-12-15",
183                "nightly/2022-12-15",
184            ],
185        ),
186    )
187
188    # Show single versions are safely used.
189    for version in ["1.66.0", "beta/2022-12-15", "nightly/2022-12-15"]:
190        asserts.equals(
191            env,
192            version,
193            select_rust_version(
194                versions = [version],
195            ),
196        )
197
198    return unittest.end(env)
199
200produce_tool_suburl_test = unittest.make(_produce_tool_suburl_test_impl)
201produce_tool_path_test = unittest.make(_produce_tool_path_test_impl)
202lookup_tool_sha256_test = unittest.make(_lookup_tool_sha256_test_impl)
203select_rust_version_test = unittest.make(_select_rust_version_test_impl)
204
205def repository_utils_test_suite(name):
206    """Entry-point macro called from the BUILD file.
207
208    Args:
209        name (str): Name of the macro.
210    """
211    produce_tool_suburl_test(
212        name = "produce_tool_suburl_test",
213    )
214    produce_tool_path_test(
215        name = "produce_tool_path_test",
216    )
217    lookup_tool_sha256_test(
218        name = "lookup_tool_sha256_test",
219    )
220    select_rust_version_test(
221        name = "select_rust_version_test",
222    )
223
224    native.test_suite(
225        name = name,
226        tests = [
227            "produce_tool_suburl_test",
228            "produce_tool_path_test",
229            "lookup_tool_sha256_test",
230            "select_rust_version_test",
231        ],
232    )
233