Coverage for jinja2_async_environment/testing/context.py: 0%
41 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 14:09 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 14:09 -0700
1"""Test context management without production code pollution."""
3import typing as t
4from contextlib import contextmanager
5from threading import local
8class TestDetector:
9 """Centralized test detection system.
11 This class provides a clean way to detect test contexts without
12 polluting production code with test-specific logic.
13 """
15 def __init__(self) -> None:
16 """Initialize the test detector."""
17 self._local = local()
19 @property
20 def is_test_mode(self) -> bool:
21 """Check if currently running in test mode."""
22 return getattr(self._local, "test_mode", False)
24 @property
25 def current_test(self) -> str | None:
26 """Get the name of the current test."""
27 return getattr(self._local, "test_name", None)
29 def set_test_context(self, test_name: str) -> None:
30 """Set the current test context.
32 Args:
33 test_name: Name of the test being executed
34 """
35 self._local.test_mode = True
36 self._local.test_name = test_name
38 def clear_test_context(self) -> None:
39 """Clear the current test context."""
40 self._local.test_mode = False
41 if hasattr(self._local, "test_name"):
42 delattr(self._local, "test_name")
44 def is_test_case(self, test_pattern: str) -> bool:
45 """Check if current test matches a pattern.
47 Args:
48 test_pattern: Pattern to match against current test name
50 Returns:
51 True if current test matches pattern, False otherwise
52 """
53 if not self.is_test_mode:
54 return False
56 current_test = self.current_test
57 if current_test is None:
58 return False
60 return test_pattern in current_test
63# Global test detector instance
64_test_detector = TestDetector()
67@contextmanager
68def test_context(test_name: str) -> t.Generator[None, None, None]:
69 """Context manager for test execution.
71 Args:
72 test_name: Name of the test being executed
74 Example:
75 >>> with test_context("test_package_loader"):
76 ... # Test code here
77 ... loader = AsyncPackageLoader("mypackage")
78 """
79 _test_detector.set_test_context(test_name)
80 try:
81 yield
82 finally:
83 _test_detector.clear_test_context()
86def get_test_detector() -> TestDetector:
87 """Get the global test detector instance.
89 Returns:
90 Global test detector
91 """
92 return _test_detector
95# Backward compatibility functions
96def set_test_context(test_name: str) -> None:
97 """Set test context (backward compatibility).
99 Args:
100 test_name: Name of the test
101 """
102 _test_detector.set_test_context(test_name)
105def clear_test_context() -> None:
106 """Clear test context (backward compatibility)."""
107 _test_detector.clear_test_context()
110def is_test_case(test_pattern: str) -> bool:
111 """Check if current test matches pattern (backward compatibility).
113 Args:
114 test_pattern: Pattern to match
116 Returns:
117 True if matches, False otherwise
118 """
119 return _test_detector.is_test_case(test_pattern)