Skip to content

Commit 86af770

Browse files
fix: Simplify pattern (#3)
* fix: simplify include pattern * chore: add new ci * update repo settings * fix: improve accept string or list * chore: update repos settings * fix: update workflow prompt
1 parent b401c86 commit 86af770

File tree

4 files changed

+261
-60
lines changed

4 files changed

+261
-60
lines changed

.github/settings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# any change of this settings.yml file is detected by the GitHub App and
66
# the settings of this repository are updated immediately.
77
#
8-
_extends: repo-settings:.github/common-settings-v2.yml
8+
_extends: repo-settings:.github/common-pipeline-settings.yml
99

1010
# repo-specific settings
1111
#

.github/workflows/ci.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@ on:
88

99
jobs:
1010
call-terraform-ci-pipeline:
11-
uses: terraform-ibm-modules/common-pipeline-assets/.github/workflows/common-terraform-module-ci-v2.yml@v1.22.5
11+
uses: terraform-ibm-modules/common-pipeline-assets/.github/workflows/common-python-ci.yml@v1.23.0
1212
secrets: inherit
13-
with:
14-
craSCCv2: true
15-
craConfigYamlFile: "cra-config.yaml"

test/test_parameter_sanitization.py

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ class TestParameterSanitization:
1414
"""Test cases for parameter sanitization."""
1515

1616
def test_sanitize_valid_list(self):
17-
"""Test sanitization preserves valid lists."""
17+
"""Test sanitization of lists with mixed patterns."""
1818
input_list = [".*\\.tf$", ".*\\.md$", "main.tf"]
1919
result = _sanitize_list_parameter(input_list, "test_param")
20-
assert result == input_list
21-
assert result is input_list # Should return the same object
20+
# main.tf is detected as a glob pattern and converted to regex
21+
expected = [".*\\.tf$", ".*\\.md$", "main\\.tf$"]
22+
assert result == expected
2223

2324
def test_sanitize_none(self):
2425
"""Test sanitization handles None correctly."""
@@ -30,7 +31,7 @@ def test_sanitize_empty_list(self):
3031
input_list = []
3132
result = _sanitize_list_parameter(input_list, "test_param")
3233
assert result == []
33-
assert result is input_list # Should return the same object
34+
# Note: result may not be the same object due to pattern processing
3435

3536
def test_sanitize_json_string(self):
3637
"""Test sanitization converts JSON strings to lists."""
@@ -40,10 +41,10 @@ def test_sanitize_json_string(self):
4041
expected = [".*\\.tf$", ".*\\.md$"]
4142
assert result == expected
4243

43-
# Test with single item
44+
# Test with single item (glob pattern gets converted)
4445
json_str = '["main.tf"]'
4546
result = _sanitize_list_parameter(json_str, "test_param")
46-
assert result == ["main.tf"]
47+
assert result == ["main\\.tf$"]
4748

4849
# Test with empty array
4950
json_str = "[]"
@@ -52,12 +53,14 @@ def test_sanitize_json_string(self):
5253

5354
def test_sanitize_single_string(self):
5455
"""Test sanitization converts single strings to lists."""
56+
# Glob patterns get converted to regex
5557
result = _sanitize_list_parameter("*.tf", "test_param")
56-
assert result == ["*.tf"]
58+
assert result == [".*\\.tf$"]
5759

5860
result = _sanitize_list_parameter("main.tf", "test_param")
59-
assert result == ["main.tf"]
61+
assert result == ["main\\.tf$"]
6062

63+
# Regex patterns are preserved
6164
result = _sanitize_list_parameter(".*\\.tf$", "test_param")
6265
assert result == [".*\\.tf$"]
6366

@@ -109,9 +112,12 @@ def test_sanitize_whitespace_handling(self):
109112
result = _sanitize_list_parameter(json_str, "test_param")
110113
assert result == [".*\\.tf$", ".*\\.md$"]
111114

112-
# Regular string with whitespace
115+
# Regular string with whitespace (treated as glob pattern with spaces)
113116
result = _sanitize_list_parameter(" *.tf ", "test_param")
114-
assert result == [" *.tf "] # Should preserve whitespace in content
117+
# Glob conversion will preserve the whitespace but convert to regex
118+
assert len(result) == 1
119+
assert result[0].endswith("$")
120+
assert "tf" in result[0]
115121

116122
def test_sanitize_complex_patterns(self):
117123
"""Test sanitization with complex regex patterns."""
@@ -132,3 +138,77 @@ def test_sanitize_complex_patterns(self):
132138
json_str = json.dumps(patterns)
133139
result = _sanitize_list_parameter(json_str, "test_param")
134140
assert result == patterns
141+
142+
def test_glob_pattern_conversion(self):
143+
"""Test automatic conversion of glob patterns to regex."""
144+
# Test basic glob patterns
145+
glob_patterns = ["*.tf", "*.md", "main.tf"]
146+
result = _sanitize_list_parameter(glob_patterns, "test_param")
147+
148+
# Should convert to regex patterns
149+
expected = [".*\\.tf$", ".*\\.md$", "main\\.tf$"]
150+
assert result == expected
151+
152+
def test_mixed_glob_and_regex_patterns(self):
153+
"""Test handling of mixed glob and regex patterns."""
154+
mixed_patterns = ["*.tf", ".*\\.py$", "README.md", "^test.*\\.js$"]
155+
result = _sanitize_list_parameter(mixed_patterns, "test_param")
156+
157+
# Only glob patterns should be converted
158+
expected = [".*\\.tf$", ".*\\.py$", "README\\.md$", "^test.*\\.js$"]
159+
assert result == expected
160+
161+
def test_complex_glob_patterns(self):
162+
"""Test complex glob patterns with wildcards."""
163+
glob_patterns = ["examples/*.tf", "modules/*/main.tf", "*.{tf,py}"]
164+
result = _sanitize_list_parameter(glob_patterns, "test_param")
165+
166+
# Should convert appropriately
167+
assert all(pattern.endswith("$") for pattern in result)
168+
assert "examples/" in result[0]
169+
assert "modules/" in result[1]
170+
171+
def test_glob_pattern_detection(self):
172+
"""Test the glob pattern detection logic."""
173+
from tim_mcp.server import _is_glob_pattern
174+
175+
# These should be detected as glob patterns
176+
assert _is_glob_pattern("*.tf") is True
177+
assert _is_glob_pattern("main.tf") is True
178+
assert _is_glob_pattern("examples/*.py") is True
179+
assert _is_glob_pattern("test?.md") is True
180+
181+
# These should be detected as regex patterns
182+
assert _is_glob_pattern(".*\\.tf$") is False
183+
assert _is_glob_pattern("^main\\.tf$") is False
184+
assert _is_glob_pattern("[abc].tf") is False
185+
assert _is_glob_pattern("test\\.py") is False
186+
187+
def test_glob_to_regex_conversion(self):
188+
"""Test the glob to regex conversion function."""
189+
from tim_mcp.server import _convert_glob_to_regex
190+
191+
# Test basic conversions
192+
assert _convert_glob_to_regex("*.tf") == ".*\\.tf$"
193+
assert _convert_glob_to_regex("main.tf") == "main\\.tf$"
194+
assert _convert_glob_to_regex("test?.py") == "test.\\.py$"
195+
196+
# Test path patterns
197+
result = _convert_glob_to_regex("examples/*.tf")
198+
assert "examples" in result
199+
assert result.endswith("$")
200+
201+
def test_single_glob_string_conversion(self):
202+
"""Test conversion of single glob pattern string."""
203+
result = _sanitize_list_parameter("*.tf", "test_param")
204+
assert result == [".*\\.tf$"]
205+
206+
result = _sanitize_list_parameter("main.py", "test_param")
207+
assert result == ["main\\.py$"]
208+
209+
def test_json_string_with_glob_patterns(self):
210+
"""Test JSON string containing glob patterns."""
211+
json_str = '["*.tf", "*.md"]'
212+
result = _sanitize_list_parameter(json_str, "test_param")
213+
expected = [".*\\.tf$", ".*\\.md$"]
214+
assert result == expected

0 commit comments

Comments
 (0)