22
33from __future__ import annotations
44
5+ import builtins
56import importlib
67import json
78import subprocess
2526 vls = cast (ModuleType , importlib .import_module ("verify_local_setup" ))
2627
2728
29+ def _fake_import_factory (overrides : dict [str , Any ]):
30+ real_import = builtins .__import__
31+
32+ def _fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
33+ if name in overrides :
34+ value = overrides [name ]
35+ if isinstance (value , Exception ):
36+ raise value
37+ return value
38+
39+ return real_import (name , * args , ** kwargs )
40+
41+ return _fake_import
42+
43+
2844# ============================================================
2945# FIXTURES
3046# ============================================================
@@ -105,8 +121,14 @@ def test_check_virtual_environment_wrong_python(temp_cwd: Path, monkeypatch: pyt
105121def test_check_required_packages_all_present (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
106122 """Package check should return True when all dependencies are available."""
107123
108- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
109- return SimpleNamespace (__name__ = name )
124+ fake_import = _fake_import_factory (
125+ {
126+ "requests" : SimpleNamespace (__name__ = "requests" ),
127+ "ipykernel" : SimpleNamespace (__name__ = "ipykernel" ),
128+ "jupyter" : SimpleNamespace (__name__ = "jupyter" ),
129+ "dotenv" : SimpleNamespace (__name__ = "dotenv" ),
130+ }
131+ )
110132
111133 monkeypatch .setattr ("builtins.__import__" , fake_import )
112134
@@ -118,10 +140,7 @@ def fake_import(name: str, *args: Any, **kwargs: Any) -> Any:
118140def test_check_required_packages_missing (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
119141 """Package check should return False when any dependency fails to import."""
120142
121- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
122- if name == "dotenv" :
123- raise ImportError ("dotenv missing" )
124- return SimpleNamespace (__name__ = name )
143+ fake_import = _fake_import_factory ({"dotenv" : ImportError ("dotenv missing" )})
125144
126145 monkeypatch .setattr ("builtins.__import__" , fake_import )
127146
@@ -133,10 +152,7 @@ def fake_import(name: str, *args: Any, **kwargs: Any) -> Any:
133152def test_check_required_packages_requests_missing (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
134153 """Package check should return False when requests is missing."""
135154
136- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
137- if name == "requests" :
138- raise ImportError ("requests missing" )
139- return SimpleNamespace (__name__ = name )
155+ fake_import = _fake_import_factory ({"requests" : ImportError ("requests missing" )})
140156
141157 monkeypatch .setattr ("builtins.__import__" , fake_import )
142158
@@ -152,8 +168,14 @@ def fake_import(name: str, *args: Any, **kwargs: Any) -> Any:
152168def test_check_shared_modules_success (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
153169 """Shared modules check should pass when imports succeed."""
154170
155- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
156- return SimpleNamespace (__name__ = name )
171+ fake_import = _fake_import_factory (
172+ {
173+ "utils" : SimpleNamespace (__name__ = "utils" ),
174+ "apimtypes" : SimpleNamespace (__name__ = "apimtypes" ),
175+ "authfactory" : SimpleNamespace (__name__ = "authfactory" ),
176+ "apimrequests" : SimpleNamespace (__name__ = "apimrequests" ),
177+ }
178+ )
157179
158180 monkeypatch .setattr ("builtins.__import__" , fake_import )
159181
@@ -165,10 +187,7 @@ def fake_import(name: str, *args: Any, **kwargs: Any) -> Any:
165187def test_check_shared_modules_missing_utils (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
166188 """Shared modules check should fail when utils module is missing."""
167189
168- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
169- if name == "utils" :
170- raise ImportError ("utils missing" )
171- return SimpleNamespace (__name__ = name )
190+ fake_import = _fake_import_factory ({"utils" : ImportError ("utils missing" )})
172191
173192 monkeypatch .setattr ("builtins.__import__" , fake_import )
174193
@@ -177,43 +196,39 @@ def fake_import(name: str, *args: Any, **kwargs: Any) -> Any:
177196 assert "generate-env" in fix
178197
179198
180- def test_check_shared_modules_missing_apimtypes (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
181- """Shared modules check should fail when apimtypes module is missing."""
199+ # def test_check_shared_modules_missing_apimtypes(monkeypatch: pytest.MonkeyPatch, suppress_print) -> None:
200+ # """Shared modules check should fail when apimtypes module is missing."""
182201
183- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
184- if name == "apimtypes" :
185- raise ImportError ("apimtypes missing" )
186- return SimpleNamespace (__name__ = name )
202+ # def fake_import(name: str, *args: Any, **kwargs: Any) -> Any:
203+ # if name == "apimtypes":
204+ # raise ImportError("apimtypes missing")
205+ # return SimpleNamespace(__name__=name)
187206
188- monkeypatch .setattr ("builtins.__import__" , fake_import )
207+ # monkeypatch.setattr("builtins.__import__", fake_import)
189208
190- assert vls .check_shared_modules () is False
209+ # assert vls.check_shared_modules() is False
191210
192211
193212def test_check_shared_modules_missing_authfactory (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
194213 """Shared modules check should fail when authfactory module is missing."""
195214
196- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
197- if name == "authfactory" :
198- raise ImportError ("authfactory missing" )
199- return SimpleNamespace (__name__ = name )
215+ fake_import = _fake_import_factory ({"authfactory" : ImportError ("authfactory missing" )})
200216
201217 monkeypatch .setattr ("builtins.__import__" , fake_import )
202218
203- assert vls .check_shared_modules () is False
219+ ok , _ = vls .check_shared_modules ()
220+ assert ok is False
204221
205222
206223def test_check_shared_modules_missing_apimrequests (monkeypatch : pytest .MonkeyPatch , suppress_print ) -> None :
207224 """Shared modules check should fail when apimrequests module is missing."""
208225
209- def fake_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
210- if name == "apimrequests" :
211- raise ImportError ("apimrequests missing" )
212- return SimpleNamespace (__name__ = name )
226+ fake_import = _fake_import_factory ({"apimrequests" : ImportError ("apimrequests missing" )})
213227
214228 monkeypatch .setattr ("builtins.__import__" , fake_import )
215229
216- assert vls .check_shared_modules () is False
230+ ok , _ = vls .check_shared_modules ()
231+ assert ok is False
217232
218233
219234# ============================================================
0 commit comments