Skip to content

Commit 34a7759

Browse files
vuanhphungIsaac
andcommitted
test: skip the HTTP import guard only when an optional extra is missing
Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
1 parent 131ebf4 commit 34a7759

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

‎tests/unit/test_lazy_http_import.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import pytest
1414

1515
# Sentinel exit codes, distinct from the 1 of an uncaught exception.
16-
_CLEAN, _LOADED, _IMPORT_FAILED = 10, 11, 12
16+
_CLEAN, _LOADED, _MISSING_OPTIONAL_DEP = 10, 11, 12
17+
# Only a missing optional extra may skip; any other import error fails.
18+
_OPTIONAL_DEPS = ("databricks_sql_kernel", "pyarrow")
1719

1820
MODULES = [
1921
"databricks.sql",
@@ -35,10 +37,11 @@ def test_module_does_not_import_http_client_libraries(module_name):
3537
import sys
3638
try:
3739
import {module_name} # noqa: F401
38-
except BaseException:
39-
import traceback
40-
traceback.print_exc()
41-
sys.exit({_IMPORT_FAILED})
40+
except ModuleNotFoundError as e:
41+
if e.name not in {_OPTIONAL_DEPS!r}:
42+
raise
43+
print(e.name)
44+
sys.exit({_MISSING_OPTIONAL_DEP})
4245
loaded = [m for m in {UNWANTED!r} if m in sys.modules]
4346
print(loaded)
4447
sys.exit({_LOADED} if loaded else {_CLEAN})
@@ -48,8 +51,8 @@ def test_module_does_not_import_http_client_libraries(module_name):
4851
capture_output=True,
4952
text=True,
5053
)
51-
if proc.returncode == _IMPORT_FAILED:
52-
pytest.skip(f"{module_name} not importable here:\n{proc.stderr}")
54+
if proc.returncode == _MISSING_OPTIONAL_DEP:
55+
pytest.skip(f"{module_name} needs {proc.stdout.strip()}, not installed")
5356
assert proc.returncode != _LOADED, f"importing {module_name} loaded {proc.stdout}"
5457
assert (
5558
proc.returncode == _CLEAN

0 commit comments

Comments
 (0)