-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #76. PiperOrigin-RevId: 534173459 Change-Id: I3185985adee6256b81bbe7d9106641fe46894c48
- Loading branch information
1 parent
e999580
commit c9f5bca
Showing
4 changed files
with
284 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
}, | ||
) |