Skip to content

Commit f6113de

Browse files
Use glob patterns in muxing rules (#1193)
We were using a handcrafted logic to determine if it was an exact match or an extension match (sorry about that). This PR replaces it for glob patterns
1 parent b0acab0 commit f6113de

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/codegate/muxing/rulematcher.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import fnmatch
23
from abc import ABC, abstractmethod
34
from asyncio import Lock
45
from typing import Dict, List, Optional
@@ -116,16 +117,16 @@ def _extract_request_filenames(self, detected_client: ClientType, data: dict) ->
116117
def _is_matcher_in_filenames(self, detected_client: ClientType, data: dict) -> bool:
117118
"""
118119
Check if the matcher is in the request filenames.
120+
The matcher is treated as a glob pattern and matched against the filenames.
119121
"""
120122
# Empty matcher_blob means we match everything
121123
if not self._mux_rule.matcher:
122124
return True
123125
filenames_to_match = self._extract_request_filenames(detected_client, data)
124-
# _mux_rule.matcher can be a filename or a file extension. We match if any of the filenames
125-
# match the rule.
126+
# _mux_rule.matcher is a glob pattern. We match if any of the filenames
127+
# match the pattern.
126128
is_filename_match = any(
127-
self._mux_rule.matcher == filename or filename.endswith(self._mux_rule.matcher)
128-
for filename in filenames_to_match
129+
fnmatch.fnmatch(filename, self._mux_rule.matcher) for filename in filenames_to_match
129130
)
130131
return is_filename_match
131132

tests/muxing/test_rulematcher.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ def test_catch_all(matcher_blob, thing_to_match):
5151
[
5252
(None, [], True), # Empty filenames and no blob
5353
(None, ["main.py"], True), # Empty blob should match
54-
(".py", ["main.py"], True), # Extension match
54+
("*.py", ["main.py"], True), # Extension match
5555
("main.py", ["main.py"], True), # Full name match
56-
(".py", ["main.py", "test.py"], True), # Extension match
56+
("*.py", ["main.py", "test.py"], True), # Extension match
5757
("main.py", ["main.py", "test.py"], True), # Full name match
5858
("main.py", ["test.py"], False), # Full name no match
59-
(".js", ["main.py", "test.py"], False), # Extension no match
60-
(".ts", ["main.tsx", "test.tsx"], False), # Extension no match
59+
("*.js", ["main.py", "test.py"], False), # Extension no match
60+
("*.ts", ["main.tsx", "test.tsx"], False), # Extension no match
6161
],
6262
)
6363
def test_file_matcher(
@@ -89,13 +89,13 @@ def test_file_matcher(
8989
[
9090
(None, [], True), # Empty filenames and no blob
9191
(None, ["main.py"], True), # Empty blob should match
92-
(".py", ["main.py"], True), # Extension match
92+
("*.py", ["main.py"], True), # Extension match
9393
("main.py", ["main.py"], True), # Full name match
94-
(".py", ["main.py", "test.py"], True), # Extension match
94+
("*.py", ["main.py", "test.py"], True), # Extension match
9595
("main.py", ["main.py", "test.py"], True), # Full name match
9696
("main.py", ["test.py"], False), # Full name no match
97-
(".js", ["main.py", "test.py"], False), # Extension no match
98-
(".ts", ["main.tsx", "test.tsx"], False), # Extension no match
97+
("*.js", ["main.py", "test.py"], False), # Extension no match
98+
("*.ts", ["main.tsx", "test.tsx"], False), # Extension no match
9999
],
100100
)
101101
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)