Skip to content

Commit 57c36d0

Browse files
committed
feat: add a version_label for consistent label generation
1 parent 5c37fa7 commit 57c36d0

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

python/private/version_label.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
17+
def version_label(version, *, sep = ""):
18+
"""A version fragment derived from python minor version
19+
20+
Examples:
21+
version_label("3.9") == "39"
22+
version_label("3.9.12", sep="_") == "3_9"
23+
version_label("3.11") == "311"
24+
25+
Args:
26+
version: Python version.
27+
sep: The separator between major and minor version numbers, defaults
28+
to an empty string.
29+
30+
Returns:
31+
The fragment of the version.
32+
"""
33+
major, _, version = version.partition(".")
34+
minor, _, _ = version.partition(".")
35+
36+
return major + sep + minor

tests/version_label/BUILD.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
load(":version_label_test.bzl", "version_label_test_suite")
16+
17+
version_label_test_suite(name = "version_label_tests")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
17+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
18+
load("//python/private:version_label.bzl", "version_label") # buildifier: disable=bzl-visibility
19+
20+
_tests = []
21+
22+
def _test_version_label_from_major_minor_version(env):
23+
actual = version_label("3.9")
24+
env.expect.that_str(actual).equals("39")
25+
26+
_tests.append(_test_version_label_from_major_minor_version)
27+
28+
def _test_version_label_from_major_minor_patch_version(env):
29+
actual = version_label("3.9.3")
30+
env.expect.that_str(actual).equals("39")
31+
32+
_tests.append(_test_version_label_from_major_minor_patch_version)
33+
34+
def _test_version_label_from_major_minor_version_custom_sep(env):
35+
actual = version_label("3.9", sep = "_")
36+
env.expect.that_str(actual).equals("3_9")
37+
38+
_tests.append(_test_version_label_from_major_minor_version_custom_sep)
39+
40+
def _test_version_label_from_complex_version(env):
41+
actual = version_label("3.9.3-rc.0")
42+
env.expect.that_str(actual).equals("39")
43+
44+
_tests.append(_test_version_label_from_complex_version)
45+
46+
def version_label_test_suite(name):
47+
"""Create the test suite.
48+
49+
Args:
50+
name: the name of the test suite
51+
"""
52+
test_suite(name = name, basic_tests = _tests)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy