Test command pattern matching.
()
| 207 | |
| 208 | |
| 209 | def test_pattern_matching(): |
| 210 | """Test command pattern matching.""" |
| 211 | print("\nTesting pattern matching:\n") |
| 212 | passed = 0 |
| 213 | failed = 0 |
| 214 | |
| 215 | # Test cases: (command, pattern, should_match, description) |
| 216 | test_cases = [ |
| 217 | # Exact matches |
| 218 | ("swift", "swift", True, "exact match"), |
| 219 | ("npm", "npm", True, "exact npm"), |
| 220 | ("xcodebuild", "xcodebuild", True, "exact xcodebuild"), |
| 221 | |
| 222 | # Prefix wildcards |
| 223 | ("swiftc", "swift*", True, "swiftc matches swift*"), |
| 224 | ("swiftlint", "swift*", True, "swiftlint matches swift*"), |
| 225 | ("swiftformat", "swift*", True, "swiftformat matches swift*"), |
| 226 | ("swift", "swift*", True, "swift matches swift*"), |
| 227 | ("npm", "swift*", False, "npm doesn't match swift*"), |
| 228 | |
| 229 | # Bare wildcard (security: should NOT match anything) |
| 230 | ("npm", "*", False, "bare wildcard doesn't match npm"), |
| 231 | ("sudo", "*", False, "bare wildcard doesn't match sudo"), |
| 232 | ("anything", "*", False, "bare wildcard doesn't match anything"), |
| 233 | |
| 234 | # Local script paths (with ./ prefix) |
| 235 | ("build.sh", "./scripts/build.sh", True, "script name matches path"), |
| 236 | ("./scripts/build.sh", "./scripts/build.sh", True, "exact script path"), |
| 237 | ("scripts/build.sh", "./scripts/build.sh", True, "relative script path"), |
| 238 | ("/abs/path/scripts/build.sh", "./scripts/build.sh", True, "absolute path matches"), |
| 239 | ("test.sh", "./scripts/build.sh", False, "different script name"), |
| 240 | |
| 241 | # Path patterns (without ./ prefix - new behavior) |
| 242 | ("test.sh", "scripts/test.sh", True, "script name matches path pattern"), |
| 243 | ("scripts/test.sh", "scripts/test.sh", True, "exact path pattern match"), |
| 244 | ("/abs/path/scripts/test.sh", "scripts/test.sh", True, "absolute path matches pattern"), |
| 245 | ("build.sh", "scripts/test.sh", False, "different script name in pattern"), |
| 246 | ("integration.test.js", "tests/integration.test.js", True, "script with dots matches"), |
| 247 | |
| 248 | # Non-matches |
| 249 | ("go", "swift*", False, "go doesn't match swift*"), |
| 250 | ("rustc", "swift*", False, "rustc doesn't match swift*"), |
| 251 | ] |
| 252 | |
| 253 | for command, pattern, should_match, description in test_cases: |
| 254 | result = matches_pattern(command, pattern) |
| 255 | if result == should_match: |
| 256 | print(f" PASS: {command!r} vs {pattern!r} ({description})") |
| 257 | passed += 1 |
| 258 | else: |
| 259 | expected = "match" if should_match else "no match" |
| 260 | actual = "match" if result else "no match" |
| 261 | print(f" FAIL: {command!r} vs {pattern!r} ({description})") |
| 262 | print(f" Expected: {expected}, Got: {actual}") |
| 263 | failed += 1 |
| 264 | |
| 265 | return passed, failed |
| 266 |
no test coverage detected