Coverage for jinja2_async_environment/testing/context.py: 0%

1"""Test context management without production code pollution."""

2

3 import typing as t

4 from contextlib import contextmanager

5 from threading import local

6

7

8 class TestDetector:

9 """Centralized test detection system.

10

11 This class provides a clean way to detect test contexts without

12 polluting production code with test-specific logic.

13 """

14

15 def __init__(self) -> None:

16 """Initialize the test detector."""

17 self._local = local()

18

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)

23

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)

28

29 def set_test_context(self, test_name: str) -> None:

30 """Set the current test context.

31

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

37

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")

43

44 def is_test_case(self, test_pattern: str) -> bool:

45 """Check if current test matches a pattern.

46

47 Args:

48 test_pattern: Pattern to match against current test name

49

50 Returns:

51 True if current test matches pattern, False otherwise

52 """

53 if not self.is_test_mode:

54 return False

55

56 current_test = self.current_test

57 if current_test is None:

58 return False

59

60 return test_pattern in current_test

61

62

63# Global test detector instance

64 _test_detector = TestDetector()

65

66

67@contextmanager

68 def test_context(test_name: str) -> t.Generator[None, None, None]:

69 """Context manager for test execution.

70

71 Args:

72 test_name: Name of the test being executed

73

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()

84

85

86 def get_test_detector() -> TestDetector:

87 """Get the global test detector instance.

88

89 Returns:

90 Global test detector

91 """

92 return _test_detector

93

94

95# Backward compatibility functions

96 def set_test_context(test_name: str) -> None:

97 """Set test context (backward compatibility).

98

99 Args:

100 test_name: Name of the test

101 """

102 _test_detector.set_test_context(test_name)

103

104

105 def clear_test_context() -> None:

106 """Clear test context (backward compatibility)."""

107 _test_detector.clear_test_context()

108

109

110 def is_test_case(test_pattern: str) -> bool:

111 """Check if current test matches pattern (backward compatibility).

112

113 Args:

114 test_pattern: Pattern to match

115

116 Returns:

117 True if matches, False otherwise

118 """

119 return _test_detector.is_test_case(test_pattern)