|
1 | 1 | import logging |
2 | 2 | import os |
| 3 | +import sys |
3 | 4 | from copy import copy |
4 | 5 | from pathlib import Path |
5 | 6 | from unittest import mock |
|
43 | 44 | from lightning_app.runners import backends, cloud, CloudRuntime |
44 | 45 | from lightning_app.runners.cloud import _validate_build_spec_and_compute |
45 | 46 | from lightning_app.storage import Drive, Mount |
46 | | -from lightning_app.testing.helpers import EmptyFlow |
| 47 | +from lightning_app.testing.helpers import EmptyFlow, EmptyWork |
47 | 48 | from lightning_app.utilities.cloud import _get_project |
48 | 49 | from lightning_app.utilities.dependency_caching import get_hash |
49 | 50 | from lightning_app.utilities.packaging.cloud_compute import CloudCompute |
@@ -1230,6 +1231,57 @@ def test_load_app_from_file_module_error(): |
1230 | 1231 | assert isinstance(empty_app.root, EmptyFlow) |
1231 | 1232 |
|
1232 | 1233 |
|
| 1234 | +@pytest.mark.parametrize( |
| 1235 | + "lines", |
| 1236 | + [ |
| 1237 | + [ |
| 1238 | + "import this_package_is_not_real", |
| 1239 | + "from lightning_app import LightningApp", |
| 1240 | + "from lightning_app.testing.helpers import EmptyWork", |
| 1241 | + "app = LightningApp(EmptyWork())", |
| 1242 | + ], |
| 1243 | + [ |
| 1244 | + "from this_package_is_not_real import this_module_is_not_real", |
| 1245 | + "from lightning_app import LightningApp", |
| 1246 | + "from lightning_app.testing.helpers import EmptyWork", |
| 1247 | + "app = LightningApp(EmptyWork())", |
| 1248 | + ], |
| 1249 | + [ |
| 1250 | + "import this_package_is_not_real", |
| 1251 | + "from this_package_is_not_real import this_module_is_not_real", |
| 1252 | + "from lightning_app import LightningApp", |
| 1253 | + "from lightning_app.testing.helpers import EmptyWork", |
| 1254 | + "app = LightningApp(EmptyWork())", |
| 1255 | + ], |
| 1256 | + [ |
| 1257 | + "import this_package_is_not_real", |
| 1258 | + "from lightning_app import LightningApp", |
| 1259 | + "from lightning_app.core.flow import _RootFlow", |
| 1260 | + "from lightning_app.testing.helpers import EmptyWork", |
| 1261 | + "class MyFlow(_RootFlow):", |
| 1262 | + " def configure_layout(self):", |
| 1263 | + " return [{'name': 'test', 'content': this_package_is_not_real()}]", |
| 1264 | + "app = LightningApp(MyFlow(EmptyWork()))", |
| 1265 | + ], |
| 1266 | + ], |
| 1267 | +) |
| 1268 | +@pytest.mark.skipif(sys.platform != "linux", reason="Causing conflicts on non-linux") |
| 1269 | +def test_load_app_from_file_mock_imports(tmpdir, lines): |
| 1270 | + path = copy(sys.path) |
| 1271 | + app_file = os.path.join(tmpdir, "app.py") |
| 1272 | + |
| 1273 | + with open(app_file, "w") as f: |
| 1274 | + f.write("\n".join(lines)) |
| 1275 | + |
| 1276 | + app = CloudRuntime.load_app_from_file(app_file) |
| 1277 | + assert isinstance(app, LightningApp) |
| 1278 | + assert isinstance(app.root.work, EmptyWork) |
| 1279 | + |
| 1280 | + # Cleanup PATH to prevent conflict with other tests |
| 1281 | + sys.path = path |
| 1282 | + os.remove(app_file) |
| 1283 | + |
| 1284 | + |
1233 | 1285 | def test_incompatible_cloud_compute_and_build_config(): |
1234 | 1286 | """Test that an exception is raised when a build config has a custom image defined, but the cloud compute is |
1235 | 1287 | the default. |
|
0 commit comments