Skip to content

Commit 834ff47

Browse files
committed
helpers.match_with_asterisks function
1 parent 6f36bcd commit 834ff47

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## [Unreleased]
4+
### Added
5+
- `helpers.match_with_asterisks` function, by @HardNorth
46
### Removed
57
- `Python 3.7` support, by @HardNorth
68

reportportal_client/helpers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,36 @@ def to_bool(value: Optional[Any]) -> Optional[bool]:
485485
if value in {'FALSE', 'False', 'false', '0', 'N', 'n', 0, False}:
486486
return False
487487
raise ValueError(f'Invalid boolean value {value}.')
488+
489+
490+
def match_with_asterisks(pattern: Optional[str], line: Optional[str]) -> bool:
491+
"""Check if the line matches the pattern with asterisks.
492+
493+
:param pattern: pattern with asterisks
494+
:param line: line to check
495+
:return: True if the line matches the pattern with asterisks, False otherwise
496+
"""
497+
if pattern is None and line is None:
498+
return True
499+
if pattern is None:
500+
return True
501+
if line is None:
502+
return False
503+
504+
if '*' not in pattern:
505+
return pattern == line
506+
507+
pattern_parts = pattern.split('*')
508+
if pattern_parts[0] and not line.startswith(pattern_parts[0]):
509+
return False
510+
511+
pos = len(pattern_parts[0])
512+
for part in pattern_parts[1:-1]:
513+
if not part:
514+
continue
515+
pos = line.find(part, pos)
516+
if pos < 0:
517+
return False
518+
pos += len(part)
519+
520+
return line.endswith(pattern_parts[-1], pos)

tests/test_helpers.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
# limitations under the License
1313

1414
"""This script contains unit tests for the helpers script."""
15-
15+
from typing import Optional
1616
from unittest import mock
1717

1818
# noinspection PyPackageRequirements
1919
import pytest
2020

2121
from reportportal_client.helpers import (
2222
gen_attributes, get_launch_sys_attrs, to_bool,
23-
verify_value_length, ATTRIBUTE_LENGTH_LIMIT, TRUNCATE_REPLACEMENT, guess_content_type_from_bytes, is_binary
23+
verify_value_length, ATTRIBUTE_LENGTH_LIMIT, TRUNCATE_REPLACEMENT, guess_content_type_from_bytes, is_binary,
24+
match_with_asterisks
2425
)
2526

2627

@@ -166,3 +167,49 @@ def test_to_bool_invalid_value():
166167
"""Test for validate to_bool() function exception case."""
167168
with pytest.raises(ValueError):
168169
to_bool('invalid_value')
170+
171+
172+
@pytest.mark.parametrize(['pattern', 'line', 'expected'], [
173+
(None, None, True),
174+
('', None, False),
175+
(None, '', True),
176+
(None, 'line', True),
177+
('', '', True),
178+
('', 'line', False),
179+
('line', 'line', True),
180+
('line', 'line1', False),
181+
('line*', 'line1', True),
182+
('line*', 'line', True),
183+
('line*', 'line2', True),
184+
('*line', 'line1', False),
185+
('*line', 'line', True),
186+
('*line', '1line', True),
187+
('*line*', '1line', True),
188+
('*line*', 'line', True),
189+
('*line*', 'line1', True),
190+
('*line*', '1line1', True),
191+
('l*ne', 'line', True),
192+
('l*ne', 'lane', True),
193+
('l*ne', 'line1', False),
194+
('l*ne', 'lane1', False),
195+
('l*ne', 'lin1', False),
196+
('l*ne', 'lan1', False),
197+
('l*ne', 'lin', False),
198+
('l*ne', 'lan', False),
199+
('l*ne', 'lne', True),
200+
('l*e', 'line', True),
201+
('l*e', 'lane', True),
202+
('l*e', 'line1', False),
203+
('l*e', 'lane1', False),
204+
('l*e', 'lin1', False),
205+
('l*e', 'lan1', False),
206+
('l*e', 'lin', False),
207+
('l*e', 'lan', False),
208+
('l*e', 'lne', True),
209+
('l*e', 'le', True),
210+
('l*e', 'l1e', True),
211+
('l*e', 'l1ne', True),
212+
('l*e', 'l1ne1', False),
213+
])
214+
def test_match_with_asterisks(pattern: Optional[str], line: Optional[str], expected: bool):
215+
assert match_with_asterisks(pattern, line) == expected

0 commit comments

Comments
 (0)