From ad27af1d6fcee231b157320f8044bcdbfac4b9e2 Mon Sep 17 00:00:00 2001 From: Andy Aylward Date: Mon, 25 Nov 2024 20:53:05 -0500 Subject: [PATCH] add unit test --- protoc/private/BUILD.bazel | 3 ++ protoc/private/prebuilt_protoc_toolchain.bzl | 21 ++++++++++--- .../prebuilt_protoc_toolchain_test.bzl | 30 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 protoc/private/prebuilt_protoc_toolchain_test.bzl diff --git a/protoc/private/BUILD.bazel b/protoc/private/BUILD.bazel index 3494256..1fb9812 100644 --- a/protoc/private/BUILD.bazel +++ b/protoc/private/BUILD.bazel @@ -1,7 +1,10 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load(":prebuilt_protoc_toolchain_test.bzl", "release_version_to_artifact_name_test_suite") bzl_library( name = "versions", srcs = ["versions.bzl"], visibility = ["//protoc:__subpackages__"], ) + +release_version_to_artifact_name_test_suite() diff --git a/protoc/private/prebuilt_protoc_toolchain.bzl b/protoc/private/prebuilt_protoc_toolchain.bzl index f5ba5a3..47a24bc 100644 --- a/protoc/private/prebuilt_protoc_toolchain.bzl +++ b/protoc/private/prebuilt_protoc_toolchain.bzl @@ -22,15 +22,28 @@ GOOGLE_PROTOBUF_DEP_EDGES = { "wrappers": [], } +def release_version_to_artifact_name(release_version, platform): + # versions have a "v" prefix like "v28.0" + stripped_version = release_version.removeprefix("v") + + # release candidate versions like "v29.0-rc3" have artifact names + # like "protoc-29.0-rc-3-osx-x86_64.zip" + artifact_version = stripped_version.replace("rc", "rc-") + + return "{}-{}-{}.zip".format( + "protoc", + artifact_version, + platform, + ) + def _prebuilt_protoc_repo_impl(rctx): release_version = rctx.attr.version if release_version == "LATEST": release_version = PROTOC_VERSIONS.keys()[0] - filename = "{}-{}-{}.zip".format( - "protoc", - release_version.removeprefix("v").replace("rc", "rc-"), - rctx.attr.platform, + filename = release_version_to_artifact_name( + release_version, + rctx.attr.platform ) url = "https://github.com/protocolbuffers/protobuf/releases/download/{}/{}".format( release_version, diff --git a/protoc/private/prebuilt_protoc_toolchain_test.bzl b/protoc/private/prebuilt_protoc_toolchain_test.bzl new file mode 100644 index 0000000..37165d1 --- /dev/null +++ b/protoc/private/prebuilt_protoc_toolchain_test.bzl @@ -0,0 +1,30 @@ +"""Unit test suite for version-to-artifact-name resolution helper. + +Based on the example at https://bazel.build/rules/testing#testing-starlark-utilities +""" + +load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") +load(":prebuilt_protoc_toolchain.bzl", "release_version_to_artifact_name") +load(":versions.bzl", "PROTOC_VERSIONS") + +def _release_version_to_artifact_name_test_impl(ctx): + env = unittest.begin(ctx) + count = 0 + + for release_version in PROTOC_VERSIONS: + artifact_name = release_version_to_artifact_name(release_version, "linux-x86_64") + artifact_dict = PROTOC_VERSIONS[release_version] + # there should be a linux-x86_64 artifact in every release + asserts.true(env, artifact_name in artifact_dict, "'{}' not found for release version '{}'".format(artifact_name, release_version)) + count += 1 + + asserts.true(env, count > 0, "no versions tested") + return unittest.end(env) + +release_version_to_artifact_name_test = unittest.make(_release_version_to_artifact_name_test_impl) + +def release_version_to_artifact_name_test_suite(): + unittest.suite( + "release_version_to_artifact_name_tests", + release_version_to_artifact_name_test, + )