From 1bfaed03f115aae11e70a6be3123f6e1351cef88 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Tue, 10 Dec 2024 16:56:34 +0300 Subject: [PATCH] Rewrite `helpers.match_with_asterisks` function to `match_with_glob_pattern` function --- CHANGELOG.md | 2 +- reportportal_client/helpers.py | 27 +++++++-------------------- tests/test_helpers.py | 11 ++++++++--- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dbd8fa..b46b7a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## [Unreleased] ### Added -- `helpers.match_with_asterisks` function, by @HardNorth +- `helpers.match_with_glob_pattern` function, by @HardNorth ### Removed - `Python 3.7` support, by @HardNorth diff --git a/reportportal_client/helpers.py b/reportportal_client/helpers.py index da57e2e..76b2805 100644 --- a/reportportal_client/helpers.py +++ b/reportportal_client/helpers.py @@ -13,9 +13,11 @@ """This module contains common functions-helpers of the client and agents.""" +import fnmatch import asyncio import inspect import logging +import re import threading import time import uuid @@ -487,10 +489,10 @@ def to_bool(value: Optional[Any]) -> Optional[bool]: raise ValueError(f'Invalid boolean value {value}.') -def match_with_asterisks(pattern: Optional[str], line: Optional[str]) -> bool: - """Check if the line matches the pattern with asterisks. +def match_with_glob_pattern(pattern: Optional[str], line: Optional[str]) -> bool: + """Check if the line matches given glob pattern. - :param pattern: pattern with asterisks + :param pattern: glob pattern :param line: line to check :return: True if the line matches the pattern with asterisks, False otherwise """ @@ -501,20 +503,5 @@ def match_with_asterisks(pattern: Optional[str], line: Optional[str]) -> bool: if line is None: return False - if '*' not in pattern: - return pattern == line - - pattern_parts = pattern.split('*') - if pattern_parts[0] and not line.startswith(pattern_parts[0]): - return False - - pos = len(pattern_parts[0]) - for part in pattern_parts[1:-1]: - if not part: - continue - pos = line.find(part, pos) - if pos < 0: - return False - pos += len(part) - - return line.endswith(pattern_parts[-1], pos) + regex_pattern = fnmatch.translate(pattern) + return re.fullmatch(regex_pattern, line) is not None diff --git a/tests/test_helpers.py b/tests/test_helpers.py index b310a60..db6aca9 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -21,7 +21,7 @@ from reportportal_client.helpers import ( gen_attributes, get_launch_sys_attrs, to_bool, verify_value_length, ATTRIBUTE_LENGTH_LIMIT, TRUNCATE_REPLACEMENT, guess_content_type_from_bytes, is_binary, - match_with_asterisks + match_with_glob_pattern ) @@ -213,6 +213,11 @@ def test_to_bool_invalid_value(): ('l*e*1', 'l1ne11', True), ('l*e*1', 'l1ne1', True), ('l*e*2', 'l1ne1', False), + ('line?', 'line', False), + ('line?', 'line1', True), + ('line?', 'line11', False), + ('?line', 'line', False), + ('?line', '1line', True), ]) -def test_match_with_asterisks(pattern: Optional[str], line: Optional[str], expected: bool): - assert match_with_asterisks(pattern, line) == expected +def test_match_with_glob_pattern(pattern: Optional[str], line: Optional[str], expected: bool): + assert match_with_glob_pattern(pattern, line) == expected