|
| 1 | +"""Test that all modules can be imported without circular dependency errors. |
| 2 | +
|
| 3 | +This test automatically discovers all modules in uipath_langchain and tests each |
| 4 | +one with isolated imports to catch runtime circular imports. |
| 5 | +""" |
| 6 | + |
| 7 | +import importlib |
| 8 | +import pkgutil |
| 9 | +import sys |
| 10 | +from typing import Iterator |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | + |
| 15 | +def discover_all_modules(package_name: str) -> Iterator[str]: |
| 16 | + """Discover all importable modules in a package recursively. |
| 17 | +
|
| 18 | + Args: |
| 19 | + package_name: The top-level package name (e.g., 'uipath_langchain') |
| 20 | +
|
| 21 | + Yields: |
| 22 | + Fully qualified module names (e.g., 'uipath_langchain.agent.tools') |
| 23 | + """ |
| 24 | + try: |
| 25 | + package = importlib.import_module(package_name) |
| 26 | + package_path = package.__path__ |
| 27 | + except ImportError: |
| 28 | + return |
| 29 | + |
| 30 | + # Recursively walk through all modules |
| 31 | + for _importer, modname, _ispkg in pkgutil.walk_packages( |
| 32 | + path=package_path, prefix=f"{package_name}.", onerror=lambda x: None |
| 33 | + ): |
| 34 | + yield modname |
| 35 | + |
| 36 | + |
| 37 | +def get_all_module_imports() -> list[str]: |
| 38 | + """Get all modules to test. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + List of module names to test |
| 42 | + """ |
| 43 | + modules = list(discover_all_modules("uipath_langchain")) |
| 44 | + |
| 45 | + # Filter out optional dependency modules that won't be installed |
| 46 | + exclude = {"uipath_langchain.chat.bedrock", "uipath_langchain.chat.vertex"} |
| 47 | + return [m for m in modules if m not in exclude] |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("module_name", get_all_module_imports()) |
| 51 | +def test_module_imports_with_isolation(module_name: str) -> None: |
| 52 | + """Test that a module can be imported in isolation. |
| 53 | +
|
| 54 | + Clears all uipath_langchain modules from sys.modules before importing to |
| 55 | + catch circular imports that would be masked by module caching. |
| 56 | +
|
| 57 | + Args: |
| 58 | + module_name: The fully qualified module name to test |
| 59 | +
|
| 60 | + Raises: |
| 61 | + pytest.fail: If the module cannot be imported due to circular dependency |
| 62 | + """ |
| 63 | + # Clear all uipath_langchain modules from sys.modules to force fresh import |
| 64 | + to_remove = [key for key in sys.modules.keys() if "uipath_langchain" in key] |
| 65 | + for key in to_remove: |
| 66 | + del sys.modules[key] |
| 67 | + |
| 68 | + # Now try importing the module in isolation |
| 69 | + try: |
| 70 | + importlib.import_module(module_name) |
| 71 | + except ImportError as e: |
| 72 | + if "circular import" in str(e).lower(): |
| 73 | + pytest.fail( |
| 74 | + f"Circular import in {module_name}:\n{e}", |
| 75 | + pytrace=False, |
| 76 | + ) |
| 77 | + # Other import errors (missing dependencies, syntax errors, etc) |
| 78 | + pytest.fail( |
| 79 | + f"Failed to import {module_name}:\n{e}", |
| 80 | + pytrace=False, |
| 81 | + ) |
0 commit comments