77class TestLazyImports (unittest .TestCase ):
88 """Test that PrometheusConnect can be imported without loading heavy dependencies."""
99
10+ def _run_in_subprocess (self , code , fail_map ):
11+ """Run code in a subprocess and check exit codes against fail_map.
12+
13+ Args:
14+ code: Python code to execute in subprocess
15+ fail_map: Dictionary mapping exit codes to error messages
16+
17+ Raises:
18+ AssertionError: If subprocess exits with a code in fail_map or any non-zero code
19+ """
20+ result = subprocess .run (
21+ [sys .executable , '-c' , code ],
22+ capture_output = True ,
23+ text = True
24+ )
25+
26+ if result .returncode in fail_map :
27+ self .fail (fail_map [result .returncode ])
28+ elif result .returncode != 0 :
29+ # Include both stdout and stderr for better debugging
30+ output = []
31+ if result .stdout :
32+ output .append (f"stdout: { result .stdout } " )
33+ if result .stderr :
34+ output .append (f"stderr: { result .stderr } " )
35+ output_str = "\n " .join (output ) if output else "no output"
36+ self .fail (f"Subprocess failed with code { result .returncode } : { output_str } " )
37+
1038 def test_prometheus_connect_import_without_pandas_matplotlib_numpy (self ):
1139 """Test that importing PrometheusConnect doesn't load pandas, matplotlib, or numpy."""
1240 # Run in a subprocess to avoid affecting other tests
@@ -27,20 +55,12 @@ def test_prometheus_connect_import_without_pandas_matplotlib_numpy(self):
2755 sys.exit(3)
2856sys.exit(0)
2957"""
30- result = subprocess .run (
31- [sys .executable , '-c' , code ],
32- capture_output = True ,
33- text = True
34- )
35-
36- if result .returncode == 1 :
37- self .fail ("pandas should not be loaded when importing PrometheusConnect" )
38- elif result .returncode == 2 :
39- self .fail ("matplotlib should not be loaded when importing PrometheusConnect" )
40- elif result .returncode == 3 :
41- self .fail ("numpy should not be loaded when importing PrometheusConnect" )
42- elif result .returncode != 0 :
43- self .fail (f"Subprocess failed with code { result .returncode } : { result .stderr } " )
58+ fail_map = {
59+ 1 : "pandas should not be loaded when importing PrometheusConnect" ,
60+ 2 : "matplotlib should not be loaded when importing PrometheusConnect" ,
61+ 3 : "numpy should not be loaded when importing PrometheusConnect" ,
62+ }
63+ self ._run_in_subprocess (code , fail_map )
4464
4565 def test_prometheus_connect_instantiation_without_numpy (self ):
4666 """Test that PrometheusConnect can be instantiated without loading numpy."""
@@ -60,18 +80,11 @@ def test_prometheus_connect_instantiation_without_numpy(self):
6080 sys.exit(2)
6181sys.exit(0)
6282"""
63- result = subprocess .run (
64- [sys .executable , '-c' , code ],
65- capture_output = True ,
66- text = True
67- )
68-
69- if result .returncode == 1 :
70- self .fail ("numpy should not be loaded when instantiating PrometheusConnect" )
71- elif result .returncode == 2 :
72- self .fail ("PrometheusConnect should be instantiated successfully" )
73- elif result .returncode != 0 :
74- self .fail (f"Subprocess failed with code { result .returncode } : { result .stderr } " )
83+ fail_map = {
84+ 1 : "numpy should not be loaded when instantiating PrometheusConnect" ,
85+ 2 : "PrometheusConnect should be instantiated successfully" ,
86+ }
87+ self ._run_in_subprocess (code , fail_map )
7588
7689 def test_metric_import_loads_pandas (self ):
7790 """Test that importing Metric does load pandas (expected behavior)."""
0 commit comments