Move version_arg to version_helper, add tests

This commit is contained in:
Mikhail f. Shiryaev 2022-04-07 13:48:52 +02:00
parent 11fe15be9e
commit a7145cf087
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4
3 changed files with 49 additions and 20 deletions

View File

@ -16,7 +16,6 @@ from commit_status_helper import post_commit_status
from docker_images_check import DockerImage
from env_helper import CI, GITHUB_RUN_URL, RUNNER_TEMP, S3_BUILDS_BUCKET
from get_robot_token import get_best_robot_token, get_parameter_from_ssm
from git_helper import removeprefix
from pr_info import PRInfo
from s3_helper import S3Helper
from stopwatch import Stopwatch
@ -25,8 +24,7 @@ from version_helper import (
ClickHouseVersion,
get_tagged_versions,
get_version_from_repo,
get_version_from_string,
get_version_from_tag,
version_arg,
)
TEMP_PATH = p.join(RUNNER_TEMP, "docker_images_check")
@ -114,22 +112,6 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()
def version_arg(version: str) -> ClickHouseVersion:
version = removeprefix(version, "refs/tags/")
try:
return get_version_from_string(version)
except ValueError:
pass
try:
return get_version_from_tag(version)
except ValueError:
pass
raise argparse.ArgumentTypeError(
f"version {version} does not match tag of plain version"
)
def auto_release_type(version: ClickHouseVersion, release_type: str) -> str:
if release_type != "auto":
return release_type

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
import logging
import os.path as p
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, ArgumentTypeError
from typing import Dict, List, Tuple, Union
from git_helper import Git, removeprefix
@ -220,6 +220,20 @@ def get_version_from_tag(tag: str) -> ClickHouseVersion:
return get_version_from_string(tag)
def version_arg(version: str) -> ClickHouseVersion:
version = removeprefix(version, "refs/tags/")
try:
return get_version_from_string(version)
except ValueError:
pass
try:
return get_version_from_tag(version)
except ValueError:
pass
raise ArgumentTypeError(f"version {version} does not match tag of plain version")
def get_tagged_versions() -> List[ClickHouseVersion]:
versions = []
for tag in git.get_tags():

33
tests/ci/version_test.py Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python
import unittest
from argparse import ArgumentTypeError
import version_helper as vh
class TestFunctions(unittest.TestCase):
def test_version_arg(self):
cases = (
("0.0.0.0", vh.get_version_from_string("0.0.0.0")),
("1.1.1.2", vh.get_version_from_string("1.1.1.2")),
("v1.1.1.2-lts", vh.get_version_from_string("1.1.1.2")),
("v1.1.1.2-prestable", vh.get_version_from_string("1.1.1.2")),
("v1.1.1.2-stable", vh.get_version_from_string("1.1.1.2")),
("v1.1.1.2-testing", vh.get_version_from_string("1.1.1.2")),
("refs/tags/v1.1.1.2-testing", vh.get_version_from_string("1.1.1.2")),
)
for case in cases:
version = vh.version_arg(case[0])
self.assertEqual(case[1], version)
error_cases = (
"0.0.0",
"1.1.1.a",
"1.1.1.1.1",
"1.1.1.2-testing",
"v1.1.1.2-testin",
"refs/tags/v1.1.1.2-testin",
)
for case in error_cases:
with self.assertRaises(ArgumentTypeError):
version = vh.version_arg(case[0])