|
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
| 11 | +from ddtrace.settings._inferred_base_service import PythonDetector |
11 | 12 | from ddtrace.settings._inferred_base_service import _module_exists |
12 | 13 | from ddtrace.settings._inferred_base_service import detect_service |
13 | 14 |
|
@@ -240,3 +241,38 @@ def test_get_service(cmd, default, expected, testdir): |
240 | 241 | ) |
241 | 242 |
|
242 | 243 | assert "AssertionError" not in result.stderr, "AssertionError found in stderr" |
| 244 | + |
| 245 | + |
| 246 | +@pytest.mark.parametrize( |
| 247 | + "command,should_match,expected_capture", |
| 248 | + [ |
| 249 | + ("python", True, "python"), |
| 250 | + ("python3", True, "python3"), |
| 251 | + ("python3.11", True, "python3.11"), |
| 252 | + ("/usr/local/bin/python", True, "/python"), |
| 253 | + ("/usr/local/bin/python3", True, "/python3"), |
| 254 | + ("/usr/local/bin/python3.11", True, "/python3.11"), |
| 255 | + ("python2", True, "python2"), |
| 256 | + ("python3.9", True, "python3.9"), |
| 257 | + ("/python", True, "/python"), |
| 258 | + ("/python3", True, "/python3"), |
| 259 | + ("python.py", False, None), # Should not match .py files |
| 260 | + ("/path/to/python.py", False, None), # Should not match .py files |
| 261 | + ("not-python", False, None), # Should not match |
| 262 | + ("pythonic", False, None), # Should not match |
| 263 | + ], |
| 264 | +) |
| 265 | +def test_python_detector_pattern_matching(command, should_match, expected_capture): |
| 266 | + """Test that the PythonDetector regex pattern correctly matches various Python executable formats.""" |
| 267 | + detector = PythonDetector(dict(os.environ)) |
| 268 | + |
| 269 | + match = detector.pattern.search(command) |
| 270 | + |
| 271 | + if should_match: |
| 272 | + assert match is not None, f"Expected '{command}' to match but it didn't" |
| 273 | + # The full match should contain the expected capture |
| 274 | + assert expected_capture in match.group( |
| 275 | + 0 |
| 276 | + ), f"Expected capture '{expected_capture}' not found in match '{match.group(0)}'" |
| 277 | + else: |
| 278 | + assert match is None, f"Expected '{command}' not to match but it did: {match.group(0) if match else None}" |
0 commit comments