Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Fix version checking
Browse files Browse the repository at this point in the history
Invalid versions that passed previosuly:
* 1.01.1
* 1.0x1.1
* 1.+1.1

Signed-off-by: Martin Bašti <mbasti@redhat.com>
  • Loading branch information
MartinBasti committed Feb 27, 2019
1 parent 75fa94d commit eaba558
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
19 changes: 7 additions & 12 deletions omps/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#

"""Module related to quay operations"""

import re
from functools import total_ordering
import logging

Expand Down Expand Up @@ -44,18 +46,11 @@ def _raise(msg):

assert isinstance(version, str)

parts = version.split('.')
if len(parts) != 3:
_raise("version must consist of 3 parts separated by '.'")

for part in parts:
try:
value = int(part)
except ValueError:
_raise("'{}' cannot be converted to integer".format(part))
else:
if value < 0:
_raise("integer '{}' must be >=0".format(value))
r_int_part = r"(0|[1-9][0-9]*)"
regexp = r"^{p}\.{p}\.{p}$".format(p=r_int_part)
match = re.match(regexp, version)
if not match:
_raise("must match regexp '{}'".format(regexp))

@classmethod
def from_str(cls, version):
Expand Down
17 changes: 13 additions & 4 deletions tests/test_quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
class TestReleaseVersion:
"""Tests for ReleaseVersion class"""

def test_from_str(self):
@pytest.mark.parametrize('version,expected', [
("1.2.3", (1, 2, 3)),
("1.0.0", (1, 0, 0)),
("0.0.0", (0, 0, 0)),
])
def test_from_str(self, version, expected):
"""Test of creating ReleaseVersion object from string"""
version = ReleaseVersion.from_str("1.2.3")
assert isinstance(version, ReleaseVersion)
assert version.version_tuple == (1, 2, 3)
v = ReleaseVersion.from_str(version)
assert isinstance(v, ReleaseVersion)
assert v.version_tuple == expected

@pytest.mark.parametrize('value', [
"1",
Expand All @@ -32,6 +37,10 @@ def test_from_str(self):
"1-5.1.0",
"-1.2.3",
"a.b.c",
"1.01.0",
"+1.0.0",
"0x1.0.0",
"1.1.1a0",
])
def test_from_str_invalid(self, value):
"""Test if error is properly raised for invalid input"""
Expand Down

0 comments on commit eaba558

Please sign in to comment.