1*60517a1eSAndroid Build Coastguard Worker# Copyright 2023 The Bazel Authors. All rights reserved. 2*60517a1eSAndroid Build Coastguard Worker# 3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*60517a1eSAndroid Build Coastguard Worker# 7*60517a1eSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*60517a1eSAndroid Build Coastguard Worker# 9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*60517a1eSAndroid Build Coastguard Worker# limitations under the License. 14*60517a1eSAndroid Build Coastguard Worker 15*60517a1eSAndroid Build Coastguard Worker"" 16*60517a1eSAndroid Build Coastguard Worker 17*60517a1eSAndroid Build Coastguard Workerload("@rules_testing//lib:test_suite.bzl", "test_suite") 18*60517a1eSAndroid Build Coastguard Workerload("//python/private:semver.bzl", "semver") # buildifier: disable=bzl-visibility 19*60517a1eSAndroid Build Coastguard Worker 20*60517a1eSAndroid Build Coastguard Worker_tests = [] 21*60517a1eSAndroid Build Coastguard Worker 22*60517a1eSAndroid Build Coastguard Workerdef _test_semver_from_major(env): 23*60517a1eSAndroid Build Coastguard Worker actual = semver("3") 24*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.major).equals(3) 25*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.minor).equals(None) 26*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.patch).equals(None) 27*60517a1eSAndroid Build Coastguard Worker env.expect.that_str(actual.build).equals("") 28*60517a1eSAndroid Build Coastguard Worker 29*60517a1eSAndroid Build Coastguard Worker_tests.append(_test_semver_from_major) 30*60517a1eSAndroid Build Coastguard Worker 31*60517a1eSAndroid Build Coastguard Workerdef _test_semver_from_major_minor_version(env): 32*60517a1eSAndroid Build Coastguard Worker actual = semver("4.9") 33*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.major).equals(4) 34*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.minor).equals(9) 35*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.patch).equals(None) 36*60517a1eSAndroid Build Coastguard Worker env.expect.that_str(actual.build).equals("") 37*60517a1eSAndroid Build Coastguard Worker 38*60517a1eSAndroid Build Coastguard Worker_tests.append(_test_semver_from_major_minor_version) 39*60517a1eSAndroid Build Coastguard Worker 40*60517a1eSAndroid Build Coastguard Workerdef _test_semver_with_build_info(env): 41*60517a1eSAndroid Build Coastguard Worker actual = semver("1.2.3+mybuild") 42*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.major).equals(1) 43*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.minor).equals(2) 44*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.patch).equals(3) 45*60517a1eSAndroid Build Coastguard Worker env.expect.that_str(actual.build).equals("mybuild") 46*60517a1eSAndroid Build Coastguard Worker 47*60517a1eSAndroid Build Coastguard Worker_tests.append(_test_semver_with_build_info) 48*60517a1eSAndroid Build Coastguard Worker 49*60517a1eSAndroid Build Coastguard Workerdef _test_semver_with_build_info_multiple_pluses(env): 50*60517a1eSAndroid Build Coastguard Worker actual = semver("1.2.3-rc0+build+info") 51*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.major).equals(1) 52*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.minor).equals(2) 53*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.patch).equals(3) 54*60517a1eSAndroid Build Coastguard Worker env.expect.that_str(actual.pre_release).equals("rc0") 55*60517a1eSAndroid Build Coastguard Worker env.expect.that_str(actual.build).equals("build+info") 56*60517a1eSAndroid Build Coastguard Worker 57*60517a1eSAndroid Build Coastguard Worker_tests.append(_test_semver_with_build_info_multiple_pluses) 58*60517a1eSAndroid Build Coastguard Worker 59*60517a1eSAndroid Build Coastguard Workerdef _test_semver_alpha_beta(env): 60*60517a1eSAndroid Build Coastguard Worker actual = semver("1.2.3-alpha.beta") 61*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.major).equals(1) 62*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.minor).equals(2) 63*60517a1eSAndroid Build Coastguard Worker env.expect.that_int(actual.patch).equals(3) 64*60517a1eSAndroid Build Coastguard Worker env.expect.that_str(actual.pre_release).equals("alpha.beta") 65*60517a1eSAndroid Build Coastguard Worker 66*60517a1eSAndroid Build Coastguard Worker_tests.append(_test_semver_alpha_beta) 67*60517a1eSAndroid Build Coastguard Worker 68*60517a1eSAndroid Build Coastguard Workerdef _test_semver_sort(env): 69*60517a1eSAndroid Build Coastguard Worker want = [ 70*60517a1eSAndroid Build Coastguard Worker semver(item) 71*60517a1eSAndroid Build Coastguard Worker for item in [ 72*60517a1eSAndroid Build Coastguard Worker # The items are sorted from lowest to highest version 73*60517a1eSAndroid Build Coastguard Worker "0.0.1", 74*60517a1eSAndroid Build Coastguard Worker "0.1.0-rc", 75*60517a1eSAndroid Build Coastguard Worker "0.1.0", 76*60517a1eSAndroid Build Coastguard Worker "0.9.11", 77*60517a1eSAndroid Build Coastguard Worker "0.9.12", 78*60517a1eSAndroid Build Coastguard Worker "1.0.0-alpha", 79*60517a1eSAndroid Build Coastguard Worker "1.0.0-alpha.1", 80*60517a1eSAndroid Build Coastguard Worker "1.0.0-alpha.beta", 81*60517a1eSAndroid Build Coastguard Worker "1.0.0-beta", 82*60517a1eSAndroid Build Coastguard Worker "1.0.0-beta.2", 83*60517a1eSAndroid Build Coastguard Worker "1.0.0-beta.11", 84*60517a1eSAndroid Build Coastguard Worker "1.0.0-rc.1", 85*60517a1eSAndroid Build Coastguard Worker "1.0.0-rc.2", 86*60517a1eSAndroid Build Coastguard Worker "1.0.0", 87*60517a1eSAndroid Build Coastguard Worker # Also handle missing minor and patch version strings 88*60517a1eSAndroid Build Coastguard Worker "2.0", 89*60517a1eSAndroid Build Coastguard Worker "3", 90*60517a1eSAndroid Build Coastguard Worker # Alphabetic comparison for different builds 91*60517a1eSAndroid Build Coastguard Worker "3.0.0+build0", 92*60517a1eSAndroid Build Coastguard Worker "3.0.0+build1", 93*60517a1eSAndroid Build Coastguard Worker ] 94*60517a1eSAndroid Build Coastguard Worker ] 95*60517a1eSAndroid Build Coastguard Worker actual = sorted(want, key = lambda x: x.key()) 96*60517a1eSAndroid Build Coastguard Worker env.expect.that_collection(actual).contains_exactly(want).in_order() 97*60517a1eSAndroid Build Coastguard Worker for i, greater in enumerate(want[1:]): 98*60517a1eSAndroid Build Coastguard Worker smaller = actual[i] 99*60517a1eSAndroid Build Coastguard Worker if greater.key() <= smaller.key(): 100*60517a1eSAndroid Build Coastguard Worker env.fail("Expected '{}' to be smaller than '{}', but got otherwise".format( 101*60517a1eSAndroid Build Coastguard Worker smaller.str(), 102*60517a1eSAndroid Build Coastguard Worker greater.str(), 103*60517a1eSAndroid Build Coastguard Worker )) 104*60517a1eSAndroid Build Coastguard Worker 105*60517a1eSAndroid Build Coastguard Worker_tests.append(_test_semver_sort) 106*60517a1eSAndroid Build Coastguard Worker 107*60517a1eSAndroid Build Coastguard Workerdef semver_test_suite(name): 108*60517a1eSAndroid Build Coastguard Worker """Create the test suite. 109*60517a1eSAndroid Build Coastguard Worker 110*60517a1eSAndroid Build Coastguard Worker Args: 111*60517a1eSAndroid Build Coastguard Worker name: the name of the test suite 112*60517a1eSAndroid Build Coastguard Worker """ 113*60517a1eSAndroid Build Coastguard Worker test_suite(name = name, basic_tests = _tests) 114