Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Buzz-Lightyear authored and Srinivas Muthu committed Oct 9, 2024
1 parent acb6e24 commit cdfa3d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
22 changes: 6 additions & 16 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ load(
"format_rule_call",
"get_directive_value",
"with_replaced_or_new_fields",
"repo_name",
)

visibility("//")
Expand Down Expand Up @@ -163,17 +164,6 @@ def _get_patch_args(path, module_overrides):
override = _get_override_or_default(module_overrides, struct(), {}, path, None, "patch_strip")
return ["-p{}".format(override)] if override else []

def _repo_name(importpath):
path_segments = importpath.split("/")
segments = reversed(path_segments[0].split(".")) + path_segments[1:]
candidate_name = "_".join(segments).replace("-", "_")

def _encode_case(c):
"""Repo names end up as directory names, therefore we can't rely on case to distinguish importpaths that only differ in case"""
return "_" + c.lower() if c.isupper() else c

return "".join([_encode_case(c) if c.isalnum() else "_" for c in candidate_name.elems()])

def _is_dev_dependency(module_ctx, tag):
if hasattr(tag, "_is_dev_dependency"):
# Synthetic tags generated from go_deps.from_file have this "hidden" attribute.
Expand Down Expand Up @@ -504,9 +494,9 @@ def _go_deps_impl(module_ctx):
if module.is_root and not module_tag.indirect:
root_versions[module_tag.path] = raw_version
if _is_dev_dependency(module_ctx, module_tag):
root_module_direct_dev_deps[_repo_name(module_tag.path)] = None
root_module_direct_dev_deps[repo_name(module_tag.path)] = None
else:
root_module_direct_deps[_repo_name(module_tag.path)] = None
root_module_direct_deps[repo_name(module_tag.path)] = None

version = semver.to_comparable(raw_version)
previous = paths.get(module_tag.path)
Expand All @@ -528,7 +518,7 @@ def _go_deps_impl(module_ctx):
local_path = replacement.local_path

module_resolutions[module_tag.path] = struct(
repo_name = _repo_name(module_tag.path),
repo_name = repo_name(module_tag.path),
version = version,
raw_version = raw_version,
to_path = to_path,
Expand Down Expand Up @@ -600,8 +590,8 @@ def _go_deps_impl(module_ctx):
for path, module in module_resolutions.items():
if hasattr(module, "module_name"):
# Do not create a go_repository for a Go module provided by a bazel_dep.
root_module_direct_deps.pop(_repo_name(path), default = None)
root_module_direct_dev_deps.pop(_repo_name(path), default = None)
root_module_direct_deps.pop(repo_name(path), default = None)
root_module_direct_dev_deps.pop(repo_name(path), default = None)
continue
if getattr(module_ctx, "is_isolated", False) and path in _SHARED_REPOS:
# Do not create a go_repository for a dep shared with the non-isolated instance of
Expand Down
11 changes: 11 additions & 0 deletions internal/bzlmod/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ def with_replaced_or_new_fields(_struct, **replacements):

return struct(**new_struct_assignments)

def repo_name(importpath):
path_segments = importpath.split("/")
segments = reversed(path_segments[0].split(".")) + path_segments[1:]
candidate_name = "_".join(segments).replace("-", "_")

def _encode_case(c):
"""Repo names end up as directory names, therefore we can't rely on case to distinguish importpaths that only differ in case"""
return "_" + c.lower() if c.isupper() else c

return "".join([_encode_case(c) if c.isalnum() else "_" for c in candidate_name.elems()])

def extension_metadata(
module_ctx,
*,
Expand Down
2 changes: 1 addition & 1 deletion tests/bcr/go_mod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ use_repo(
go_deps,
"com_github_bazelbuild_buildtools",
"com_github_bmatcuk_doublestar_v4",
"com_github_datadog_sketches_go",
"com_github__data_dog_sketches_go",
"com_github_envoyproxy_protoc_gen_validate",
"com_github_fmeum_dep_on_gazelle",
"com_github_google_go_jsonnet",
Expand Down
2 changes: 1 addition & 1 deletion tests/bcr/go_work/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ use_repo(
"com_github_99designs_gqlgen",
"com_github_bazelbuild_buildtools",
"com_github_bmatcuk_doublestar_v4",
"com_github_datadog_sketches_go",
"com_github__data_dog_sketches_go",
"com_github_envoyproxy_protoc_gen_validate",
"com_github_fmeum_dep_on_gazelle",
"com_github_google_safetext",
Expand Down
12 changes: 11 additions & 1 deletion tests/bzlmod/utils_test.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//internal/bzlmod:utils.bzl", "with_replaced_or_new_fields")
load("//internal/bzlmod:utils.bzl", "with_replaced_or_new_fields", "repo_name")

_BEFORE_STRUCT = struct(
direct = True,
Expand All @@ -25,8 +25,18 @@ def _with_replaced_or_new_fields_test_impl(ctx):

with_replaced_or_new_fields_test = unittest.make(_with_replaced_or_new_fields_test_impl)

def _repo_name_test_impl(ctx):
env = unittest.begin(ctx)
asserts.equals(env, "com_github_shurcoo_l_githubv4", repo_name("github.com/shurcooL/githubv4"))
asserts.equals(env, "com_github_shurcool_githubv4", repo_name("github.com/shurcool/githubv4"))
asserts.equals(env, "com_github__d_a_t_a__d_o_g_go_sqlmock", repo_name("github.com/DATA-DOG/go-sqlmock"))
return unittest.end(env)

repo_name_test = unittest.make(_repo_name_test_impl)

def utils_test_suite(name):
unittest.suite(
name,
with_replaced_or_new_fields_test,
repo_name_test,
)

0 comments on commit cdfa3d5

Please sign in to comment.