|
15 | 15 | """Test usage of modules.bzl."""
|
16 | 16 |
|
17 | 17 | load("//lib:modules.bzl", "modules")
|
| 18 | +load("//lib:unittest.bzl", "asserts", "unittest") |
18 | 19 | load("//rules:build_test.bzl", "build_test")
|
19 | 20 |
|
| 21 | +_is_bzlmod_enabled = str(Label("//tests:module_tests.bzl")).startswith("@@") |
| 22 | + |
20 | 23 | def _repo_rule_impl(repository_ctx):
|
21 | 24 | repository_ctx.file("WORKSPACE")
|
22 | 25 | repository_ctx.file("BUILD", """exports_files(["hello"])""")
|
@@ -45,12 +48,158 @@ use_all_repos_test_ext = module_extension(
|
45 | 48 | doc = "Only used for testing modules.use_all_repos().",
|
46 | 49 | )
|
47 | 50 |
|
| 51 | +def _apparent_repo_name_test(ctx): |
| 52 | + """Unit tests for modules.apparent_repo_name.""" |
| 53 | + env = unittest.begin(ctx) |
| 54 | + |
| 55 | + asserts.equals( |
| 56 | + env, "", modules.apparent_repo_name(""), |
| 57 | + msg = "Handles the empty string as input", |
| 58 | + ) |
| 59 | + |
| 60 | + asserts.equals( |
| 61 | + env, "foo", modules.apparent_repo_name("foo"), |
| 62 | + msg = ( |
| 63 | + "Return the original name unchanged if it doesn't start with `@`.", |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + asserts.equals( |
| 68 | + env, "foo", modules.apparent_repo_name("@foo"), |
| 69 | + msg = "Return the original name without `@` if already apparent.", |
| 70 | + ) |
| 71 | + |
| 72 | + asserts.equals( |
| 73 | + env, "foo", modules.apparent_repo_name(Label("@foo").repo_name), |
| 74 | + msg = "Return the apparent name from a canonical name string.", |
| 75 | + ) |
| 76 | + |
| 77 | + asserts.equals( |
| 78 | + env, "", modules.apparent_repo_name(Label("@@//:all")), |
| 79 | + msg = "Returns the empty string for a main repository Label.", |
| 80 | + ) |
| 81 | + |
| 82 | + asserts.equals( |
| 83 | + env, "", modules.apparent_repo_name(Label("@bazel_skylib//:all")), |
| 84 | + msg = " ".join([ |
| 85 | + "Returns the empty string for a Label containing the main", |
| 86 | + "repository's module name.", |
| 87 | + ]), |
| 88 | + ) |
| 89 | + |
| 90 | + asserts.equals( |
| 91 | + env, "foo", modules.apparent_repo_name(Label("@foo")), |
| 92 | + msg = "Return the apparent name from a Label.", |
| 93 | + ) |
| 94 | + |
| 95 | + asserts.equals( |
| 96 | + env, "rules_pkg", modules.apparent_repo_name(Label("@rules_pkg")), |
| 97 | + msg = " ".join([ |
| 98 | + "Top level module repos have the canonical name delimiter at the", |
| 99 | + "end. Therefore, this should not return the empty string, but the", |
| 100 | + "name without the leading `@` and trailing delimiter.", |
| 101 | + ]), |
| 102 | + ) |
| 103 | + |
| 104 | + asserts.equals( |
| 105 | + env, |
| 106 | + "stardoc" if _is_bzlmod_enabled else "io_bazel_stardoc", |
| 107 | + modules.apparent_repo_name(Label("@io_bazel_stardoc")), |
| 108 | + msg = " ".join([ |
| 109 | + "Label values will already map bazel_dep repo_names to", |
| 110 | + "actual repo names under Bzlmod (no-op under WORKSPACE)." |
| 111 | + ]) |
| 112 | + ) |
| 113 | + |
| 114 | + asserts.equals( |
| 115 | + env, "foo", modules.apparent_repo_name("foo+1.2.3"), |
| 116 | + msg = "Ignores version numbers in canonical repo names", |
| 117 | + ) |
| 118 | + |
| 119 | + return unittest.end(env) |
| 120 | + |
| 121 | +apparent_repo_name_test = unittest.make(_apparent_repo_name_test) |
| 122 | + |
| 123 | +def _apparent_repo_label_string_test(ctx): |
| 124 | + """Unit tests for modules.apparent_repo_label_string.""" |
| 125 | + env = unittest.begin(ctx) |
| 126 | + |
| 127 | + main_repo = str(Label("//:all")) |
| 128 | + asserts.equals( |
| 129 | + env, main_repo, modules.apparent_repo_label_string(Label("//:all")), |
| 130 | + msg = "Returns top level target with leading `@` or `@@`", |
| 131 | + ) |
| 132 | + |
| 133 | + main_module_label = Label("@bazel_skylib//:all") |
| 134 | + asserts.equals( |
| 135 | + env, main_repo, modules.apparent_repo_label_string(main_module_label), |
| 136 | + msg = " ".join([ |
| 137 | + "Returns top level target with leading `@` or `@@`", |
| 138 | + "for a Label containing the main module's name", |
| 139 | + ]), |
| 140 | + ) |
| 141 | + |
| 142 | + rules_pkg = "@rules_pkg//:all" |
| 143 | + asserts.equals( |
| 144 | + env, rules_pkg, modules.apparent_repo_label_string(Label(rules_pkg)), |
| 145 | + msg = "Returns original repo name", |
| 146 | + ) |
| 147 | + |
| 148 | + asserts.equals( |
| 149 | + env, |
| 150 | + "@%s//:all" % ("stardoc" if _is_bzlmod_enabled else "io_bazel_stardoc"), |
| 151 | + modules.apparent_repo_label_string(Label("@io_bazel_stardoc//:all")), |
| 152 | + msg = " ".join([ |
| 153 | + "Returns the actual module name instead of", |
| 154 | + "repo_name from bazel_dep() (no-op under WORKSPACE).", |
| 155 | + ]), |
| 156 | + ) |
| 157 | + |
| 158 | + return unittest.end(env) |
| 159 | + |
| 160 | +apparent_repo_label_string_test = unittest.make( |
| 161 | + _apparent_repo_label_string_test |
| 162 | +) |
| 163 | + |
| 164 | +def _adjust_main_repo_prefix_test(ctx): |
| 165 | + """Unit tests for modules.apparent_repo_label_string.""" |
| 166 | + env = unittest.begin(ctx) |
| 167 | + |
| 168 | + expected = modules.main_repo_prefix + ":all" |
| 169 | + asserts.equals( |
| 170 | + env, expected, modules.adjust_main_repo_prefix("@//:all"), |
| 171 | + msg = "Normalizes a target pattern starting with `@//`.", |
| 172 | + ) |
| 173 | + |
| 174 | + asserts.equals( |
| 175 | + env, expected, modules.adjust_main_repo_prefix("@@//:all"), |
| 176 | + msg = "Normalizes a target pattern starting with `@@//`.", |
| 177 | + ) |
| 178 | + |
| 179 | + original = "@not_the_main_repo" |
| 180 | + asserts.equals( |
| 181 | + env, original, modules.adjust_main_repo_prefix(original), |
| 182 | + msg = "Returns non main repo target patterns unchanged.", |
| 183 | + ) |
| 184 | + |
| 185 | + return unittest.end(env) |
| 186 | + |
| 187 | +adjust_main_repo_prefix_test = unittest.make( |
| 188 | + _adjust_main_repo_prefix_test |
| 189 | +) |
| 190 | + |
48 | 191 | # buildifier: disable=unnamed-macro
|
49 | 192 | def modules_test_suite():
|
50 | 193 | """Creates the tests for modules.bzl if Bzlmod is enabled."""
|
51 | 194 |
|
52 |
| - is_bzlmod_enabled = str(Label("//tests:module_tests.bzl")).startswith("@@") |
53 |
| - if not is_bzlmod_enabled: |
| 195 | + unittest.suite( |
| 196 | + "modules_tests", |
| 197 | + apparent_repo_name_test, |
| 198 | + apparent_repo_label_string_test, |
| 199 | + adjust_main_repo_prefix_test, |
| 200 | + ) |
| 201 | + |
| 202 | + if not _is_bzlmod_enabled: |
54 | 203 | return
|
55 | 204 |
|
56 | 205 | build_test(
|
|
0 commit comments