From b724a6391b133dd55b34e5dd12c647a793c9cf46 Mon Sep 17 00:00:00 2001 From: John Cater Date: Mon, 22 May 2023 13:56:29 -0700 Subject: [PATCH] Add Android revision checking. Part of https://github.com/bazelbuild/rules_android/issues/76. PiperOrigin-RevId: 534173459 Change-Id: I3185985adee6256b81bbe7d9106641fe46894c48 --- rules/BUILD | 10 ++- rules/android_revision.bzl | 88 +++++++++++++++++++++++ test/rules/android_revision/BUILD | 84 ++++++++++++++++++++++ test/rules/android_revision/test.bzl | 103 +++++++++++++++++++++++++++ 4 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 rules/android_revision.bzl create mode 100644 test/rules/android_revision/BUILD create mode 100644 test/rules/android_revision/test.bzl diff --git a/rules/BUILD b/rules/BUILD index 33ce74ac..3648349f 100644 --- a/rules/BUILD +++ b/rules/BUILD @@ -13,6 +13,14 @@ alias( visibility = ["//visibility:public"], ) +bzl_library( + name = "android_revision_bzl", + srcs = [ + "android_revision.bzl", + ], + visibility = ["//:__subpackages__"], +) + bzl_library( name = "common_bzl", srcs = [ @@ -36,10 +44,10 @@ bzl_library( "resources.bzl", "utils.bzl", ], + visibility = ["//:__subpackages__"], deps = [ "//rules/acls:bzl", "//rules/android_common:bzl", "//rules/flags:bzl", ], - visibility = ["//:__subpackages__"], ) diff --git a/rules/android_revision.bzl b/rules/android_revision.bzl new file mode 100644 index 00000000..28b45dde --- /dev/null +++ b/rules/android_revision.bzl @@ -0,0 +1,88 @@ +# Copyright 2023 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Parse and compare Android revision strings.""" + +# TODO(katre): support preview versions. +AndroidRevisionInfo = provider( + "Information about Android revision specifications.", + fields = { + "major": "The major version number", + "minor": "The minor version number, or 0 if unset.", + "micro": "The micro version number, or 0 if unset.", + "version": "The version string.", + "dir": "The directory where the revision would exist in an Android SDK.", + }, +) + +def parse_android_revision(input): + """Parse and Android revision string and return an AndroidRevisionInfo. + + Args: + input: The raw revision string to parse. + + Returns: + An AndroidRevisionInfo provider representing the input. + """ + input = input.strip() + parts = input.split(".") + if len(parts) < 1: + fail("Invalid Android revision %s" % input) + major = int(parts[0]) if len(parts) >= 1 else 0 + minor = int(parts[1]) if len(parts) >= 2 else 0 + micro = int(parts[2]) if len(parts) >= 3 else 0 + + return AndroidRevisionInfo( + version = input, + dir = input, + major = major, + minor = minor, + micro = micro, + ) + +def _compare_android_revision_field(first, second, name): + first_val = getattr(first, name) + second_val = getattr(second, name) + if first_val > second_val: + return first + elif first_val < second_val: + return second + return None + +def compare_android_revisions(first, second): + """Compares two AndroidRevisionInfo providers and returns the one with the highest version. + + Args: + first: The first revision to compare. + second: The first revision to compare. + + Returns: + The revision with the higher version number, or the first if they are equal. + """ + if first == None and second == None: + return None + if first != None and second == None: + return first + if first == None and second != None: + return second + highest = _compare_android_revision_field(first, second, "major") + if highest != None: + return highest + highest = _compare_android_revision_field(first, second, "minor") + if highest != None: + return highest + highest = _compare_android_revision_field(first, second, "micro") + if highest != None: + return highest + return first diff --git a/test/rules/android_revision/BUILD b/test/rules/android_revision/BUILD new file mode 100644 index 00000000..bf1b5de2 --- /dev/null +++ b/test/rules/android_revision/BUILD @@ -0,0 +1,84 @@ +# Description: +# Tests for the AndroidRevisionInfo provider. + +load(":test.bzl", "android_revision_comparision_test", "android_revision_test") +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +licenses(["notice"]) + +# Test revision string parsing. +# buildifier: leave-alone +android_revision_test( + name = "version_1.2.3", + input = "1.2.3", + expected_major = 1, + expected_minor = 2, + expected_micro = 3, + expected_version = "1.2.3", + expected_dir = "1.2.3", +) + +# buildifier: leave-alone +android_revision_test( + name = "micro_missing", + input = "1.2", + expected_major = 1, + expected_minor = 2, + expected_micro = 0, + expected_version = "1.2", + expected_dir = "1.2", +) + +# buildifier: leave-alone +android_revision_test( + name = "minor_missing", + input = "1", + expected_major = 1, + expected_minor = 0, + expected_micro = 0, + expected_version = "1", + expected_dir = "1", +) + +# Test revision comparisions. +VERSIONS = [ + ("2.0.0", "1.0.0"), + ("12.0.0", "11.0.0"), + ("1.1.0", "1.0.0"), + ("1.0.1", "1.0.0"), + ("1.1.1", "1.0.1"), + ("2", "1"), + ("2.1", "2"), + ("2", "1.0"), + # TODO(katre): Re-add when previews are supported. + #("1.1.0-rc1", "1.0.0-rc1"), + #("1.1.0-alpha1", "1.0.0-rc1"), + #("1.0.0", "1.0.0-rc1"), + #("1.0.0", "1.0.0-rc2"), + #("1.0.0", "1.0.0-alpha1"), + #("1.0.0", "1.0.0-alpha2"), + #("1.0.0", "1.0.0-beta1"), + #("1.0.0", "1.0.0-beta2"), + #("1.0.0-rc1", "1.0.0-beta1"), + #("1.0.0-beta1", "1.0.0-alpha1"), + #("1.0.0-beta1", "1.0.0-alpha2"), + #("1.0.0-rc2", "1.0.0-rc1"), + #("1.0.0-beta2", "1.0.0-beta1"), + #("1.0.0-alpha2", "1.0.0-alpha1"), + #("1 rc1", "1 beta1"), +] + +[ + android_revision_comparision_test( + name = "compare_%s_%s" % (higher, lower), + higher = higher, + lower = lower, + ) + for (higher, lower) in VERSIONS +] + +bzl_library( + name = "bzl", + srcs = glob(["*.bzl"]), + visibility = ["//visibility:private"], +) diff --git a/test/rules/android_revision/test.bzl b/test/rules/android_revision/test.bzl new file mode 100644 index 00000000..714d86c6 --- /dev/null +++ b/test/rules/android_revision/test.bzl @@ -0,0 +1,103 @@ +# Copyright 2023 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Bazel rules that test the Android revision parsing. + +The following are test rules that can be used to test the AndroidRevisionInfo provider. + +android_revision_test: Inspect providers with the given set of expected values. +""" + +load( + "//rules:android_revision.bzl", + "compare_android_revisions", + "parse_android_revision", +) +load( + "//test/utils:lib.bzl", + "asserts", + "unittest", +) + +def _android_revision_test_impl(ctx): + env = unittest.begin(ctx) + input = ctx.attr.input + revision = parse_android_revision(input) + + asserts.equals( + env, + ctx.attr.expected_major, + revision.major, + ) + asserts.equals( + env, + ctx.attr.expected_minor, + revision.minor, + ) + asserts.equals( + env, + ctx.attr.expected_micro, + revision.micro, + ) + asserts.equals( + env, + ctx.attr.expected_version, + revision.version, + ) + asserts.equals( + env, + ctx.attr.expected_dir, + revision.dir, + ) + + return unittest.end(env) + +android_revision_test = unittest.make( + impl = _android_revision_test_impl, + attrs = { + "input": attr.string(), + "expected_major": attr.int(), + "expected_minor": attr.int(), + "expected_micro": attr.int(), + "expected_version": attr.string(), + "expected_dir": attr.string(), + }, +) + +def _assert_revisions_equal(env, expected, value): + asserts.equals(env, expected.major, value.major) + asserts.equals(env, expected.minor, value.minor) + asserts.equals(env, expected.major, value.major) + +def _android_revision_comparision_test_impl(ctx): + env = unittest.begin(ctx) + higher = parse_android_revision(ctx.attr.higher) + lower = parse_android_revision(ctx.attr.lower) + + result = compare_android_revisions(higher, lower) + _assert_revisions_equal( + env, + higher, + result, + ) + + return unittest.end(env) + +android_revision_comparision_test = unittest.make( + impl = _android_revision_comparision_test_impl, + attrs = { + "higher": attr.string(), + "lower": attr.string(), + }, +)