Skip to content

Commit 6cdde61

Browse files
authored
Enforce version pattern through unit test (#474)
Enforce version pattern through unit test
1 parent 3e22ddc commit 6cdde61

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

databricks_cli/version.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
# See the License for the specific language governing permissions and
2222
# limitations under the License.
2323

24+
import re
25+
26+
2427
version = '0.16.8.dev0' # NOQA
2528

2629

@@ -30,3 +33,41 @@ def print_version_callback(ctx, param, value): # NOQA
3033
return
3134
click.echo('Version {}'.format(version))
3235
ctx.exit()
36+
37+
38+
def _match_version(value):
39+
# Expect version to be of the form: `X.Y.Z(.suffix)?`
40+
match = re.match(r'^(\d+)\.(\d+)\.(\d+)(\.\w+)?$', value)
41+
if match is None:
42+
raise ValueError("Non-compliant version string: " + value)
43+
return match
44+
45+
46+
def is_release_version(value=None):
47+
"""
48+
Returns whether the current version of databricks-cli is a release version or not.
49+
"""
50+
if value is None:
51+
value = version
52+
53+
# The 4th group is the optional `.devZZZ` suffix.
54+
# If it is non-empty, this is not a release version.
55+
match = _match_version(value)
56+
if match.group(4) is not None:
57+
return False
58+
59+
return True
60+
61+
62+
def next_development_version(value=None):
63+
"""
64+
Returns the hypothetical next development version of databricks-cli.
65+
"""
66+
if value is None:
67+
value = version
68+
69+
match = _match_version(value)
70+
major = int(match.group(1))
71+
minor = int(match.group(2))
72+
patch = int(match.group(3))
73+
return "{}.{}.{}.dev0".format(major, minor, patch + 1)

tests/test_version.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Databricks CLI
2+
# Copyright 2022 Databricks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"), except
5+
# that the use of services to which certain application programming
6+
# interfaces (each, an "API") connect requires that the user first obtain
7+
# a license for the use of the APIs from Databricks, Inc. ("Databricks"),
8+
# by creating an account at www.databricks.com and agreeing to either (a)
9+
# the Community Edition Terms of Service, (b) the Databricks Terms of
10+
# Service, or (c) another written agreement between Licensee and Databricks
11+
# for the use of the APIs.
12+
#
13+
# You may not use this file except in compliance with the License.
14+
# You may obtain a copy of the License at
15+
#
16+
# http://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS,
20+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
# See the License for the specific language governing permissions and
22+
# limitations under the License.
23+
24+
import pytest
25+
26+
import databricks_cli.version as version
27+
28+
29+
def test_is_release_version_defaults_to_current_version():
30+
# Not specifying a version explicitly implies testing the current version.
31+
assert version.is_release_version() == version.is_release_version(version.version)
32+
33+
34+
def test_is_release_version():
35+
# Release versions
36+
assert version.is_release_version("0.16.0")
37+
assert version.is_release_version("0.16.8")
38+
assert version.is_release_version("0.16.1111")
39+
assert version.is_release_version("0.17.0")
40+
assert version.is_release_version("1.0.0")
41+
42+
# Development versions
43+
assert not version.is_release_version("0.16.0.dev0")
44+
assert not version.is_release_version("0.16.8.dev0")
45+
assert not version.is_release_version("0.16.1111.dev1")
46+
assert not version.is_release_version("0.17.0.dev")
47+
assert not version.is_release_version("1.0.0.x")
48+
49+
# Malformed version
50+
with pytest.raises(ValueError):
51+
version.is_release_version("foobar")
52+
53+
with pytest.raises(ValueError):
54+
version.is_release_version("1.0.0dev0")
55+
56+
with pytest.raises(ValueError):
57+
version.is_release_version("1.0.0.")
58+
59+
with pytest.raises(ValueError):
60+
version.is_release_version("1.0.0.!")
61+
62+
63+
def test_next_development_version():
64+
assert version.next_development_version("0.16.0") == "0.16.1.dev0"
65+
assert version.next_development_version("0.16.8") == "0.16.9.dev0"
66+
assert version.next_development_version("0.16.1111") == "0.16.1112.dev0"
67+
assert version.next_development_version("0.17.0") == "0.17.1.dev0"
68+
assert version.next_development_version("1.0.0") == "1.0.1.dev0"

0 commit comments

Comments
 (0)