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