From a14f993a31238aa872ffcac6ddd703bf6cfd7bd6 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 8 Jan 2026 15:14:48 +0900 Subject: [PATCH] Add pre-compiled REGEX entries to the regex pattern file compiled ones hare prefixed with COMPILED_ --- .../check_handling/regex_constants.py | 8 ++++ .../check_handling/test_regex_constants.py | 39 +++++++++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/corelibs/check_handling/regex_constants.py b/src/corelibs/check_handling/regex_constants.py index 1fe0ecd..d005259 100644 --- a/src/corelibs/check_handling/regex_constants.py +++ b/src/corelibs/check_handling/regex_constants.py @@ -51,4 +51,12 @@ DOMAIN_WITH_LOCALHOST_PORT_REGEX: str = r""" # Domain, no localhost DOMAIN_REGEX: str = r"^(?!-)[A-Za-z0-9-]{1,63}(? re.Pattern[str]: """Fixture that returns compiled email regex pattern.""" - return compile_re(EMAIL_BASIC_REGEX) + return COMPILED_EMAIL_BASIC_REGEX @pytest.mark.parametrize("valid_email", [ "user@example.com", @@ -192,7 +198,7 @@ class TestNameEmailSimpleRegex: @pytest.fixture def name_email_simple_pattern(self) -> re.Pattern[str]: """Fixture that returns compiled name+email simple regex pattern.""" - return compile_re(NAME_EMAIL_SIMPLE_REGEX) + return COMPILED_NAME_EMAIL_SIMPLE_REGEX @pytest.mark.parametrize("test_input,expected_groups", [ ('"John Doe" ', {'name1': 'John Doe', 'email1': 'john@example.com'}), @@ -284,7 +290,7 @@ class TestNameEmailBasicRegex: @pytest.fixture def name_email_basic_pattern(self) -> re.Pattern[str]: """Fixture that returns compiled name+email basic regex pattern.""" - return compile_re(NAME_EMAIL_BASIC_REGEX) + return COMPILED_NAME_EMAIL_BASIC_REGEX @pytest.mark.parametrize("test_input,expected_name,expected_email", [ ('"John Doe" ', 'John Doe', 'john@example.com'), @@ -391,7 +397,7 @@ class TestDomainWithLocalhostRegex: @pytest.fixture def domain_localhost_pattern(self) -> re.Pattern[str]: """Fixture that returns compiled domain with localhost regex pattern.""" - return compile_re(DOMAIN_WITH_LOCALHOST_REGEX) + return COMPILED_DOMAIN_WITH_LOCALHOST_REGEX @pytest.mark.parametrize("valid_domain", [ "localhost", @@ -443,7 +449,7 @@ class TestDomainWithLocalhostPortRegex: @pytest.fixture def domain_localhost_port_pattern(self) -> re.Pattern[str]: """Fixture that returns compiled domain and localhost with port pattern.""" - return compile_re(DOMAIN_WITH_LOCALHOST_PORT_REGEX) + return COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX @pytest.mark.parametrize("valid_domain", [ "localhost", @@ -509,7 +515,7 @@ class TestDomainRegex: @pytest.fixture def domain_pattern(self) -> re.Pattern[str]: """Fixture that returns compiled domain regex pattern.""" - return compile_re(DOMAIN_REGEX) + return COMPILED_DOMAIN_REGEX @pytest.mark.parametrize("valid_domain", [ "example.com", @@ -568,6 +574,8 @@ class TestRegexPatternConsistency: """Test that all regex patterns can be compiled without errors.""" patterns = [ EMAIL_BASIC_REGEX, + NAME_EMAIL_SIMPLE_REGEX, + NAME_EMAIL_BASIC_REGEX, DOMAIN_WITH_LOCALHOST_REGEX, DOMAIN_WITH_LOCALHOST_PORT_REGEX, DOMAIN_REGEX, @@ -576,9 +584,24 @@ class TestRegexPatternConsistency: compiled = compile_re(pattern) assert isinstance(compiled, re.Pattern) + def test_compiled_patterns_are_patterns(self) -> None: + """Test that all COMPILED_ constants are Pattern objects.""" + compiled_patterns = [ + COMPILED_EMAIL_BASIC_REGEX, + COMPILED_NAME_EMAIL_SIMPLE_REGEX, + COMPILED_NAME_EMAIL_BASIC_REGEX, + COMPILED_DOMAIN_WITH_LOCALHOST_REGEX, + COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX, + COMPILED_DOMAIN_REGEX, + ] + for pattern in compiled_patterns: + assert isinstance(pattern, re.Pattern) + def test_domain_patterns_are_strings(self) -> None: """Test that all regex constants are strings.""" assert isinstance(EMAIL_BASIC_REGEX, str) + assert isinstance(NAME_EMAIL_SIMPLE_REGEX, str) + assert isinstance(NAME_EMAIL_BASIC_REGEX, str) assert isinstance(DOMAIN_WITH_LOCALHOST_REGEX, str) assert isinstance(DOMAIN_WITH_LOCALHOST_PORT_REGEX, str) assert isinstance(DOMAIN_REGEX, str) @@ -587,8 +610,8 @@ class TestRegexPatternConsistency: """Test that domain patterns follow expected hierarchy.""" # DOMAIN_WITH_LOCALHOST_PORT_REGEX should accept everything # DOMAIN_WITH_LOCALHOST_REGEX accepts - domain_localhost = compile_re(DOMAIN_WITH_LOCALHOST_REGEX) - domain_localhost_port = compile_re(DOMAIN_WITH_LOCALHOST_PORT_REGEX) + domain_localhost = COMPILED_DOMAIN_WITH_LOCALHOST_REGEX + domain_localhost_port = COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX test_cases = ["example.com", "subdomain.example.com", "localhost"] for test_case in test_cases: