From dc901951112e0f7644ff389018eb0fde2376ef3f Mon Sep 17 00:00:00 2001 From: Jirka Date: Tue, 22 Nov 2022 20:19:48 +0100 Subject: [PATCH] split examples and pytests --- .github/checkgroup.yml | 1 - .github/workflows/ci-app-tests.yml | 1 - tests/tests_app/conftest.py | 17 --------------- tests/tests_app/runners/test_cloud.py | 14 ++----------- tests/tests_app/utilities/test_load_app.py | 5 ----- tests/tests_examples_app/conftest.py | 17 +++++++++++++++ .../public}/test_gradio.py | 0 .../tests_examples_app/public/test_v0_app.py | 21 +++++++++++++++++++ 8 files changed, 40 insertions(+), 36 deletions(-) rename tests/{tests_app/components/serve => tests_examples_app/public}/test_gradio.py (100%) diff --git a/.github/checkgroup.yml b/.github/checkgroup.yml index 1d1ef2d6439a1..4241aa2256175 100644 --- a/.github/checkgroup.yml +++ b/.github/checkgroup.yml @@ -223,7 +223,6 @@ subprojects: - ".github/workflows/ci-app-tests.yml" - "src/lightning_app/**" - "tests/tests_app/**" - - "examples/app_*/**" # some tests_app tests call examples files - "requirements/app/**" - "setup.py" - ".actions/**" diff --git a/.github/workflows/ci-app-tests.yml b/.github/workflows/ci-app-tests.yml index 8ddbf2a5ddb8d..0f8d63e70d3b8 100644 --- a/.github/workflows/ci-app-tests.yml +++ b/.github/workflows/ci-app-tests.yml @@ -11,7 +11,6 @@ on: - ".github/workflows/ci-app-tests.yml" - "src/lightning_app/**" - "tests/tests_app/**" - - "examples/app_*/**" # some tests_app tests call examples files - "requirements/app/**" - "setup.py" - ".actions/**" diff --git a/tests/tests_app/conftest.py b/tests/tests_app/conftest.py index e557e3648b8d6..7b9d56850c84d 100644 --- a/tests/tests_app/conftest.py +++ b/tests/tests_app/conftest.py @@ -3,12 +3,10 @@ import threading from datetime import datetime from pathlib import Path -from subprocess import Popen import psutil import py import pytest -from tests_app import _PROJECT_ROOT from lightning_app.storage.path import _storage_root_dir from lightning_app.utilities.component import _set_context @@ -16,24 +14,9 @@ from lightning_app.utilities.packaging.app_config import _APP_CONFIG_FILENAME from lightning_app.utilities.state import AppState -GITHUB_APP_URLS = { - "template_react_ui": "https://github.com/Lightning-AI/lightning-template-react.git", -} - os.environ["LIGHTNING_DISPATCHED"] = "1" -def pytest_sessionstart(*_): - """Pytest hook that get called after the Session object has been created and before performing collection and - entering the run test loop.""" - for name, url in GITHUB_APP_URLS.items(): - if not os.path.exists(os.path.join(_PROJECT_ROOT, "examples", name)): - path_examples = os.path.join(_PROJECT_ROOT, "examples") - Popen(["git", "clone", url, name], cwd=path_examples).wait(timeout=90) - else: - Popen(["git", "pull", "main"], cwd=os.path.join(_PROJECT_ROOT, "examples", name)).wait(timeout=90) - - def pytest_sessionfinish(session, exitstatus): """Pytest hook that get called after whole test run finished, right before returning the exit status to the system.""" diff --git a/tests/tests_app/runners/test_cloud.py b/tests/tests_app/runners/test_cloud.py index 25bc590893280..6848fdee963c4 100644 --- a/tests/tests_app/runners/test_cloud.py +++ b/tests/tests_app/runners/test_cloud.py @@ -40,11 +40,11 @@ V1Work, ) -from lightning_app import _PROJECT_ROOT, BuildConfig, LightningApp, LightningWork +from lightning_app import BuildConfig, LightningApp, LightningWork from lightning_app.runners import backends, cloud, CloudRuntime from lightning_app.runners.cloud import _validate_build_spec_and_compute from lightning_app.storage import Drive, Mount -from lightning_app.testing.helpers import EmptyFlow, EmptyWork +from lightning_app.testing.helpers import EmptyWork from lightning_app.utilities.cloud import _get_project from lightning_app.utilities.dependency_caching import get_hash from lightning_app.utilities.packaging.cloud_compute import CloudCompute @@ -1221,16 +1221,6 @@ def test_project_has_sufficient_credits(): assert cloud_runtime._project_has_sufficient_credits(project) is result -@mock.patch( - "lightning_app.runners.cloud.load_app_from_file", - MagicMock(side_effect=ModuleNotFoundError("Module X not found")), -) -def test_load_app_from_file_module_error(): - empty_app = CloudRuntime.load_app_from_file(os.path.join(_PROJECT_ROOT, "examples", "app_v0", "app.py")) - assert isinstance(empty_app, LightningApp) - assert isinstance(empty_app.root, EmptyFlow) - - @pytest.mark.parametrize( "lines", [ diff --git a/tests/tests_app/utilities/test_load_app.py b/tests/tests_app/utilities/test_load_app.py index a26c622ac2b66..87894e7e8c611 100644 --- a/tests/tests_app/utilities/test_load_app.py +++ b/tests/tests_app/utilities/test_load_app.py @@ -3,17 +3,12 @@ import pytest import tests_app.core.scripts -from tests_app import _PROJECT_ROOT -from lightning_app import LightningApp from lightning_app.utilities.exceptions import MisconfigurationException from lightning_app.utilities.load_app import extract_metadata_from_app, load_app_from_file def test_load_app_from_file(): - app = load_app_from_file(os.path.join(_PROJECT_ROOT, "examples", "app_v0", "app.py")) - assert isinstance(app, LightningApp) - test_script_dir = os.path.join(os.path.dirname(tests_app.core.__file__), "scripts") with pytest.raises(MisconfigurationException, match="There should not be multiple apps instantiated within a file"): load_app_from_file(os.path.join(test_script_dir, "two_apps.py")) diff --git a/tests/tests_examples_app/conftest.py b/tests/tests_examples_app/conftest.py index 493d2c941eac3..fcefa6287c3c6 100644 --- a/tests/tests_examples_app/conftest.py +++ b/tests/tests_examples_app/conftest.py @@ -1,19 +1,36 @@ import os import shutil import threading +from subprocess import Popen import psutil import pytest +from lightning_app import _PROJECT_ROOT from lightning_app.storage.path import _storage_root_dir from lightning_app.utilities.component import _set_context from lightning_app.utilities.packaging import cloud_compute from lightning_app.utilities.packaging.app_config import _APP_CONFIG_FILENAME from lightning_app.utilities.state import AppState +GITHUB_APP_URLS = { + "template_react_ui": "https://github.com/Lightning-AI/lightning-template-react.git", +} + os.environ["LIGHTNING_DISPATCHED"] = "1" +def pytest_sessionstart(*_): + """Pytest hook that get called after the Session object has been created and before performing collection and + entering the run test loop.""" + for name, url in GITHUB_APP_URLS.items(): + if not os.path.exists(os.path.join(_PROJECT_ROOT, "examples", name)): + path_examples = os.path.join(_PROJECT_ROOT, "examples") + Popen(["git", "clone", url, name], cwd=path_examples).wait(timeout=90) + else: + Popen(["git", "pull", "main"], cwd=os.path.join(_PROJECT_ROOT, "examples", name)).wait(timeout=90) + + def pytest_sessionfinish(session, exitstatus): """Pytest hook that get called after whole test run finished, right before returning the exit status to the system.""" diff --git a/tests/tests_app/components/serve/test_gradio.py b/tests/tests_examples_app/public/test_gradio.py similarity index 100% rename from tests/tests_app/components/serve/test_gradio.py rename to tests/tests_examples_app/public/test_gradio.py diff --git a/tests/tests_examples_app/public/test_v0_app.py b/tests/tests_examples_app/public/test_v0_app.py index f6e46666c62a3..1201d8653712d 100644 --- a/tests/tests_examples_app/public/test_v0_app.py +++ b/tests/tests_examples_app/public/test_v0_app.py @@ -1,12 +1,18 @@ import os from time import sleep from typing import Tuple +from unittest import mock +from unittest.mock import MagicMock import pytest from tests_examples_app.public import _PATH_EXAMPLES +from lightning_app import LightningApp +from lightning_app.runners import CloudRuntime +from lightning_app.testing import EmptyFlow from lightning_app.testing.testing import application_testing, LightningTestApp, run_app_in_cloud, wait_for from lightning_app.utilities.enum import AppStage +from lightning_app.utilities.load_app import load_app_from_file class LightningAppTestInt(LightningTestApp): @@ -78,3 +84,18 @@ def test_v0_app_example_cloud() -> None: _, ): run_v0_app(fetch_logs, view_page) + + +@mock.patch( + "lightning_app.runners.cloud.load_app_from_file", + MagicMock(side_effect=ModuleNotFoundError("Module X not found")), +) +def test_load_app_from_file_module_error(): + empty_app = CloudRuntime.load_app_from_file(os.path.join(_PATH_EXAMPLES, "app_v0", "app.py")) + assert isinstance(empty_app, LightningApp) + assert isinstance(empty_app.root, EmptyFlow) + + +def test_load_app_from_file(): + app = load_app_from_file(os.path.join(_PATH_EXAMPLES, "app_v0", "app.py")) + assert isinstance(app, LightningApp)