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 "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 sha256 = "", 105 ), 106 ) 107 108 # Values in //rust:known_shas.bzl override sha256 arg 109 asserts.equals( 110 env, 111 "62b89786e195fc5a8a262f83118d6689832b24228c9d303cba8ac14dc1e9adc8", 112 lookup_tool_sha256( 113 ctx, 114 tool_name = "rustc", 115 target_triple = _PLATFORM_TRIPLE, 116 version = "1.65.0", 117 iso_date = "2022-11-02", 118 sha256 = "FAKE_SHA256_FROM_ARG", 119 ), 120 ) 121 122 # Nightly version included in //rust:known_shas.bzl 123 asserts.equals( 124 env, 125 "ea01d3cd6c6729cd8ebb55a7702eda2347451e304b58807361e020065a579d96", 126 lookup_tool_sha256( 127 ctx, 128 tool_name = "rust-std", 129 target_triple = _PLATFORM_TRIPLE, 130 version = "nightly", 131 iso_date = "2022-11-02", 132 sha256 = "", 133 ), 134 ) 135 136 # Lookup failure (returns "") for a nightly version not included in //rust:known_shas.bzl 137 asserts.equals( 138 env, 139 "", 140 lookup_tool_sha256( 141 ctx, 142 tool_name = "rust-std", 143 target_triple = _PLATFORM_TRIPLE, 144 version = "nightly", 145 iso_date = "2022-11-01", 146 sha256 = "", 147 ), 148 ) 149 150 # A nightly version not included in //rust:known_shas.bzl falls back to sha256 arg 151 asserts.equals( 152 env, 153 "FAKE_SHA256_FROM_ARG", 154 lookup_tool_sha256( 155 ctx, 156 tool_name = "rust-std", 157 target_triple = _PLATFORM_TRIPLE, 158 version = "nightly", 159 iso_date = "2022-11-01", 160 sha256 = "FAKE_SHA256_FROM_ARG", 161 ), 162 ) 163 return unittest.end(env) 164 165def _select_rust_version_test_impl(ctx): 166 env = unittest.begin(ctx) 167 168 # Show stable releases take highest priority 169 asserts.equals( 170 env, 171 "1.66.0", 172 select_rust_version( 173 versions = [ 174 "1.66.0", 175 "beta/2022-12-15", 176 "nightly/2022-12-15", 177 ], 178 ), 179 ) 180 181 # Show nightly releases take priority over beta 182 asserts.equals( 183 env, 184 "nightly/2022-12-15", 185 select_rust_version( 186 versions = [ 187 "beta/2022-12-15", 188 "nightly/2022-12-15", 189 ], 190 ), 191 ) 192 193 # Show single versions are safely used. 194 for version in ["1.66.0", "beta/2022-12-15", "nightly/2022-12-15"]: 195 asserts.equals( 196 env, 197 version, 198 select_rust_version( 199 versions = [version], 200 ), 201 ) 202 203 return unittest.end(env) 204 205produce_tool_suburl_test = unittest.make(_produce_tool_suburl_test_impl) 206produce_tool_path_test = unittest.make(_produce_tool_path_test_impl) 207lookup_tool_sha256_test = unittest.make(_lookup_tool_sha256_test_impl) 208select_rust_version_test = unittest.make(_select_rust_version_test_impl) 209 210def repository_utils_test_suite(name): 211 unittest.suite( 212 name, 213 produce_tool_suburl_test, 214 produce_tool_path_test, 215 lookup_tool_sha256_test, 216 select_rust_version_test, 217 ) 218