Skip to content

Commit

Permalink
fix: correct bundler version check
Browse files Browse the repository at this point in the history
The current implementation was incorrectly handling newer versions such
as 2.3.0. The commit also adds unit tests for the function.
  • Loading branch information
p0deje committed Jan 16, 2024
1 parent f68794b commit 3b2f7a5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
12 changes: 2 additions & 10 deletions ruby/private/bundle_fetch.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load(
"//ruby/private:utils.bzl",
_is_version_older = "is_version_older",
_join_and_indent = "join_and_indent",
_normalize_bzlmod_repository_name = "normalize_bzlmod_repository_name",
)
Expand Down Expand Up @@ -36,15 +37,6 @@ See https://github.com/rubygems/rubygems/issues/4620 for more details.
"""

def _is_outdated_bundler(version):
"""Checks that Bundler version is 2.2.19+.
Older versions don't work with cached gems properly.
See https://github.com/rubygems/rubygems/issues/4620 for more details.
"""
major, minor, patch = version.split(".")
return (int(major) < 2 or int(minor) < 2 or int(patch) < 19)

def _download_gem(repository_ctx, gem, cache_path):
"""Downloads gem into a predefined vendor/cache location."""
url = "{remote}gems/{filename}".format(remote = gem.remote, filename = gem.filename)
Expand Down Expand Up @@ -100,7 +92,7 @@ def _rb_bundle_fetch_impl(repository_ctx):
repository_ctx.file(src.name, repository_ctx.read(src))

gemfile_lock = parse_gemfile_lock(repository_ctx.read(gemfile_lock_path))
if _is_outdated_bundler(gemfile_lock.bundler.version):
if _is_version_older(gemfile_lock.bundler.version, "2.2.19"):
fail(_OUTDATED_BUNDLER_ERROR)

if len(gemfile_lock.git_packages) > 0:
Expand Down
24 changes: 24 additions & 0 deletions ruby/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,27 @@ def normalize_bzlmod_repository_name(name):
repository name
"""
return name.rpartition("~")[-1]

def is_version_older(current_version, minimum_version):
"""Checks if current version is older than minimum version:
Args:
current_version: version to compare in "major.minor.patch" format
minimum_version: version to compare with in "major.minor.patch" format
Returns:
true if current version is older, otherwise false
"""
current_major, current_minor, current_patch = current_version.split(".")
minimum_major, minimum_minor, minimum_patch = minimum_version.split(".")

if int(current_major) < int(minimum_major):
return True
elif int(current_major) == int(minimum_major):
if int(current_minor) < int(minimum_minor):
return True
elif int(current_minor) == int(minimum_minor):
if int(current_patch) < int(minimum_patch):
return True

return False
3 changes: 3 additions & 0 deletions ruby/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load(":utils_tests.bzl", "utils_test_suite")

utils_test_suite()
23 changes: 23 additions & 0 deletions ruby/tests/utils_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Unit tests for utils."""

load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load(
"//ruby/private:utils.bzl",
_is_version_older = "is_version_older",
)

def _is_version_older_test_impl(ctx):
env = unittest.begin(ctx)
asserts.equals(env, _is_version_older("1.2.19", "2.2.19"), True)
asserts.equals(env, _is_version_older("2.1.19", "2.2.19"), True)
asserts.equals(env, _is_version_older("2.2.18", "2.2.19"), True)
asserts.equals(env, _is_version_older("2.2.19", "2.2.19"), False)
asserts.equals(env, _is_version_older("2.2.20", "2.2.19"), False)
asserts.equals(env, _is_version_older("2.3.1", "2.2.19"), False)
asserts.equals(env, _is_version_older("3.0.0", "2.2.19"), False)
return unittest.end(env)

is_version_older_test = unittest.make(_is_version_older_test_impl)

def utils_test_suite():
unittest.suite("utils_tests", is_version_older_test)

0 comments on commit 3b2f7a5

Please sign in to comment.