@@ -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