Skip to content

Commit

Permalink
helpers.match_with_asterisks function
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 10, 2024
1 parent 6f36bcd commit 834ff47
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Added
- `helpers.match_with_asterisks` function, by @HardNorth
### Removed
- `Python 3.7` support, by @HardNorth

Expand Down
33 changes: 33 additions & 0 deletions reportportal_client/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,36 @@ def to_bool(value: Optional[Any]) -> Optional[bool]:
if value in {'FALSE', 'False', 'false', '0', 'N', 'n', 0, False}:
return False
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.
:param pattern: pattern with asterisks
:param line: line to check
:return: True if the line matches the pattern with asterisks, False otherwise
"""
if pattern is None and line is None:
return True
if pattern is None:
return True
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)
51 changes: 49 additions & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
# limitations under the License

"""This script contains unit tests for the helpers script."""

from typing import Optional
from unittest import mock

# noinspection PyPackageRequirements
import pytest

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
verify_value_length, ATTRIBUTE_LENGTH_LIMIT, TRUNCATE_REPLACEMENT, guess_content_type_from_bytes, is_binary,
match_with_asterisks
)


Expand Down Expand Up @@ -166,3 +167,49 @@ def test_to_bool_invalid_value():
"""Test for validate to_bool() function exception case."""
with pytest.raises(ValueError):
to_bool('invalid_value')


@pytest.mark.parametrize(['pattern', 'line', 'expected'], [
(None, None, True),
('', None, False),
(None, '', True),
(None, 'line', True),
('', '', True),
('', 'line', False),
('line', 'line', True),
('line', 'line1', False),
('line*', 'line1', True),
('line*', 'line', True),
('line*', 'line2', True),
('*line', 'line1', False),
('*line', 'line', True),
('*line', '1line', True),
('*line*', '1line', True),
('*line*', 'line', True),
('*line*', 'line1', True),
('*line*', '1line1', True),
('l*ne', 'line', True),
('l*ne', 'lane', True),
('l*ne', 'line1', False),
('l*ne', 'lane1', False),
('l*ne', 'lin1', False),
('l*ne', 'lan1', False),
('l*ne', 'lin', False),
('l*ne', 'lan', False),
('l*ne', 'lne', True),
('l*e', 'line', True),
('l*e', 'lane', True),
('l*e', 'line1', False),
('l*e', 'lane1', False),
('l*e', 'lin1', False),
('l*e', 'lan1', False),
('l*e', 'lin', False),
('l*e', 'lan', False),
('l*e', 'lne', True),
('l*e', 'le', True),
('l*e', 'l1e', True),
('l*e', 'l1ne', True),
('l*e', 'l1ne1', False),
])
def test_match_with_asterisks(pattern: Optional[str], line: Optional[str], expected: bool):
assert match_with_asterisks(pattern, line) == expected

0 comments on commit 834ff47

Please sign in to comment.