From a0c1d64093682fcdee04377ffeba601352d62fd7 Mon Sep 17 00:00:00 2001 From: qiujiacai Date: Tue, 26 Sep 2023 11:16:10 +0800 Subject: [PATCH 1/2] add conditional judgment to avoid string matching errors --- pyporter/utils.py | 8 +++++++- tests/test_transform_module_name.py | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pyporter/utils.py b/pyporter/utils.py index 1c8e3d4..a34d098 100644 --- a/pyporter/utils.py +++ b/pyporter/utils.py @@ -3,7 +3,13 @@ import re # TODO: this should be more compatible for https://peps.python.org/pep-0508/ def transform_module_name(input_str): - module_name = re.match(r"([a-zA-Z0-9_-]+)", input_str).group(1).strip() + module_name = re.match(r"([a-zA-Z0-9_-]+)", input_str) + match = re.match(r"([a-zA-Z0-9_-]+)", input_str) + if match: + module_name = match.group(1).strip() + else: + raise ValueError("Invalid input format") + version_names = input_str[len(module_name):].strip().strip("()") version_constraint = version_names.split(",") package_name = "python3-" + module_name diff --git a/tests/test_transform_module_name.py b/tests/test_transform_module_name.py index c292b7d..eaefcd6 100644 --- a/tests/test_transform_module_name.py +++ b/tests/test_transform_module_name.py @@ -16,9 +16,14 @@ class TestTransofrmModuleName(unittest.TestCase): def test_transform_module_name_strip_whitespace(self): input_str = "pyjwkest (>=1.3.6)" - expected_output = "(python3-pyjwkest(>=1.3.6))" + expected_output = "(python3-pyjwkest>=1.3.6)" self.assertEqual(transform_module_name(input_str), expected_output) + def test_invalid_input(self): + input_str = "!invalid_module123" + with self.assertRaises(ValueError): + transform_module_name(input_str) + if __name__ == '__main__': unittest.main() -- Gitee From dc9a77df07f7f540e6a5b437f9208926ea0a2aef Mon Sep 17 00:00:00 2001 From: qiujiacai Date: Tue, 26 Sep 2023 03:24:52 +0000 Subject: [PATCH 2/2] update pyporter/utils.py. Signed-off-by: qiujiacai --- pyporter/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyporter/utils.py b/pyporter/utils.py index a34d098..c445694 100644 --- a/pyporter/utils.py +++ b/pyporter/utils.py @@ -3,7 +3,6 @@ import re # TODO: this should be more compatible for https://peps.python.org/pep-0508/ def transform_module_name(input_str): - module_name = re.match(r"([a-zA-Z0-9_-]+)", input_str) match = re.match(r"([a-zA-Z0-9_-]+)", input_str) if match: module_name = match.group(1).strip() -- Gitee