|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import json
|
| 6 | +import sys |
6 | 7 | import tempfile
|
7 | 8 | import yaml
|
8 | 9 | from pathlib import Path
|
9 | 10 | from unittest.mock import MagicMock, patch
|
10 | 11 | import pytest
|
11 | 12 |
|
12 |
| -# Test the module's imports and functionality |
| 13 | +# Import only non-widget functionality at module level |
13 | 14 | from clustrix.notebook_magic import (
|
14 | 15 | DEFAULT_CONFIGS,
|
15 |
| - EnhancedClusterConfigWidget, |
16 | 16 | detect_config_files,
|
17 | 17 | load_config_from_file,
|
18 | 18 | validate_ip_address,
|
|
22 | 22 | load_ipython_extension,
|
23 | 23 | )
|
24 | 24 |
|
| 25 | +# Import widget conditionally to avoid GitHub Actions import issues |
| 26 | +try: |
| 27 | + from clustrix.notebook_magic import EnhancedClusterConfigWidget |
| 28 | +except ImportError: |
| 29 | + # In GitHub Actions, widget import fails - will be handled by fixture |
| 30 | + EnhancedClusterConfigWidget = None |
| 31 | + |
25 | 32 |
|
26 | 33 | class TestEnhancedDefaultConfigs:
|
27 | 34 | """Test enhanced default configurations."""
|
@@ -156,13 +163,46 @@ def test_validate_hostname(self):
|
156 | 163 | @pytest.fixture
|
157 | 164 | def mock_ipython_environment():
|
158 | 165 | """Mock IPython environment for testing."""
|
159 |
| - with patch("clustrix.notebook_magic.IPYTHON_AVAILABLE", True): |
160 |
| - with patch("clustrix.notebook_magic.get_ipython") as mock_get_ipython: |
161 |
| - mock_ipython = MagicMock() |
162 |
| - mock_ipython.kernel = True |
163 |
| - mock_ipython.register_magic_function = MagicMock() |
164 |
| - mock_get_ipython.return_value = mock_ipython |
165 |
| - yield mock_ipython |
| 166 | + # Save current module state |
| 167 | + original_module = sys.modules.get("clustrix.notebook_magic") |
| 168 | + |
| 169 | + try: |
| 170 | + # Clear the module to force re-import with mocks |
| 171 | + if "clustrix.notebook_magic" in sys.modules: |
| 172 | + del sys.modules["clustrix.notebook_magic"] |
| 173 | + |
| 174 | + # Mock the IPython/ipywidgets modules |
| 175 | + with patch.dict( |
| 176 | + "sys.modules", |
| 177 | + { |
| 178 | + "IPython": MagicMock(), |
| 179 | + "IPython.core": MagicMock(), |
| 180 | + "IPython.core.magic": MagicMock(), |
| 181 | + "IPython.display": MagicMock(), |
| 182 | + "ipywidgets": MagicMock(), |
| 183 | + }, |
| 184 | + ): |
| 185 | + # Re-import the module with mocked dependencies |
| 186 | + import clustrix.notebook_magic |
| 187 | + |
| 188 | + # Mock get_ipython |
| 189 | + with patch("clustrix.notebook_magic.get_ipython") as mock_get_ipython: |
| 190 | + mock_ipython = MagicMock() |
| 191 | + mock_ipython.kernel = True |
| 192 | + mock_ipython.register_magic_function = MagicMock() |
| 193 | + mock_get_ipython.return_value = mock_ipython |
| 194 | + |
| 195 | + # Make the widget class available globally |
| 196 | + global EnhancedClusterConfigWidget |
| 197 | + EnhancedClusterConfigWidget = ( |
| 198 | + clustrix.notebook_magic.EnhancedClusterConfigWidget |
| 199 | + ) |
| 200 | + |
| 201 | + yield mock_ipython |
| 202 | + finally: |
| 203 | + # Restore original module |
| 204 | + if original_module: |
| 205 | + sys.modules["clustrix.notebook_magic"] = original_module |
166 | 206 |
|
167 | 207 |
|
168 | 208 | class TestEnhancedClusterConfigWidget:
|
|
0 commit comments