xref: /aosp_15_r20/external/bazel-skylib/tests/versions_tests.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1*bcb5dc79SHONG Yifan# Copyright 2018 The Bazel Authors. All rights reserved.
2*bcb5dc79SHONG Yifan#
3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License");
4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License.
5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at
6*bcb5dc79SHONG Yifan#
7*bcb5dc79SHONG Yifan#    http://www.apache.org/licenses/LICENSE-2.0
8*bcb5dc79SHONG Yifan#
9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software
10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS,
11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and
13*bcb5dc79SHONG Yifan# limitations under the License.
14*bcb5dc79SHONG Yifan
15*bcb5dc79SHONG Yifan"""Unit tests for versions.bzl."""
16*bcb5dc79SHONG Yifan
17*bcb5dc79SHONG Yifanload("//lib:unittest.bzl", "asserts", "unittest")
18*bcb5dc79SHONG Yifanload("//lib:versions.bzl", "versions")
19*bcb5dc79SHONG Yifan
20*bcb5dc79SHONG Yifandef _parse_test(ctx):
21*bcb5dc79SHONG Yifan    """Unit tests for versions.parse"""
22*bcb5dc79SHONG Yifan    env = unittest.begin(ctx)
23*bcb5dc79SHONG Yifan
24*bcb5dc79SHONG Yifan    asserts.equals(env, (0, 10, 0), versions.parse("0.10.0rc1 abcd123"))
25*bcb5dc79SHONG Yifan    asserts.equals(env, (0, 4, 0), versions.parse("0.4.0 abcd123"))
26*bcb5dc79SHONG Yifan    asserts.equals(env, (0, 4, 0), versions.parse("0.4.0"))
27*bcb5dc79SHONG Yifan    asserts.equals(env, (0, 4, 0), versions.parse("0.4.0rc"))
28*bcb5dc79SHONG Yifan
29*bcb5dc79SHONG Yifan    # Verify that this doesn't fail - it corresponds to a dev build of Bazel.
30*bcb5dc79SHONG Yifan    versions.parse("")
31*bcb5dc79SHONG Yifan
32*bcb5dc79SHONG Yifan    return unittest.end(env)
33*bcb5dc79SHONG Yifan
34*bcb5dc79SHONG Yifandef _version_comparison_test(ctx):
35*bcb5dc79SHONG Yifan    """Unit tests for versions.is_at_least and is_at_most"""
36*bcb5dc79SHONG Yifan    env = unittest.begin(ctx)
37*bcb5dc79SHONG Yifan
38*bcb5dc79SHONG Yifan    asserts.false(env, versions.is_at_least("0.11.0 123abcd", "0.10.0rc1 abcd123"))
39*bcb5dc79SHONG Yifan    asserts.true(env, versions.is_at_least("0.9.0", "0.10.0rc2"))
40*bcb5dc79SHONG Yifan    asserts.true(env, versions.is_at_least("0.9.0", "0.9.0rc3"))
41*bcb5dc79SHONG Yifan    asserts.true(env, versions.is_at_least("0.9.0", "1.2.3"))
42*bcb5dc79SHONG Yifan    asserts.true(env, versions.is_at_least("0.9.0", ""))
43*bcb5dc79SHONG Yifan
44*bcb5dc79SHONG Yifan    asserts.false(env, versions.is_at_most("0.4.0 123abcd", "0.10.0rc1 abcd123"))
45*bcb5dc79SHONG Yifan    asserts.true(env, versions.is_at_most("0.4.0", "0.3.0rc2"))
46*bcb5dc79SHONG Yifan    asserts.true(env, versions.is_at_most("0.4.0", "0.4.0rc3"))
47*bcb5dc79SHONG Yifan    asserts.true(env, versions.is_at_most("1.4.0", "0.4.0rc3"))
48*bcb5dc79SHONG Yifan    asserts.false(env, versions.is_at_most("1.4.0", ""))
49*bcb5dc79SHONG Yifan
50*bcb5dc79SHONG Yifan    return unittest.end(env)
51*bcb5dc79SHONG Yifan
52*bcb5dc79SHONG Yifandef _check_test(ctx):
53*bcb5dc79SHONG Yifan    """Unit tests for versions.check"""
54*bcb5dc79SHONG Yifan    env = unittest.begin(ctx)
55*bcb5dc79SHONG Yifan
56*bcb5dc79SHONG Yifan    asserts.equals(env, None, versions.check("0.4.5 abcdef", bazel_version = "0.10.0rc1 abcd123"))
57*bcb5dc79SHONG Yifan    asserts.equals(env, None, versions.check("0.4.5", bazel_version = "0.4.5"))
58*bcb5dc79SHONG Yifan    asserts.equals(env, None, versions.check("0.4.5", bazel_version = "0.10.0rc1 abcd123"))
59*bcb5dc79SHONG Yifan    asserts.equals(env, None, versions.check("0.4.5", maximum_bazel_version = "1.0.0", bazel_version = "0.10.0rc1 abcd123"))
60*bcb5dc79SHONG Yifan
61*bcb5dc79SHONG Yifan    return unittest.end(env)
62*bcb5dc79SHONG Yifan
63*bcb5dc79SHONG Yifanparse_test = unittest.make(_parse_test)
64*bcb5dc79SHONG Yifanversion_comparison_test = unittest.make(_version_comparison_test)
65*bcb5dc79SHONG Yifancheck_test = unittest.make(_check_test)
66*bcb5dc79SHONG Yifan
67*bcb5dc79SHONG Yifandef versions_test_suite():
68*bcb5dc79SHONG Yifan    """Creates the test targets and test suite for versions.bzl tests."""
69*bcb5dc79SHONG Yifan    unittest.suite(
70*bcb5dc79SHONG Yifan        "versions_tests",
71*bcb5dc79SHONG Yifan        parse_test,
72*bcb5dc79SHONG Yifan        version_comparison_test,
73*bcb5dc79SHONG Yifan        check_test,
74*bcb5dc79SHONG Yifan    )
75