-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Programmatically lauch reload to allow factories and arguments to be …
…passed to the app
- Loading branch information
Showing
5 changed files
with
88 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,62 @@ | ||
from pathlib import Path | ||
from unittest.mock import patch | ||
import asyncio | ||
import pytest | ||
|
||
import gradio | ||
from gradio.reload import run_in_reload_mode | ||
import gradio as gr | ||
from gradio.reload import _setup_config, Server | ||
|
||
|
||
@patch("gradio.reload.os.system") | ||
@patch("gradio.reload.sys") | ||
def test_run_in_reload_mode(mock_sys, mock_system_call): | ||
def build_demo(): | ||
with gr.Blocks() as demo: | ||
gr.Textbox("") | ||
|
||
mock_sys.argv = ["gradio", "demo/calculator/run.py"] | ||
run_in_reload_mode() | ||
reload_command = mock_system_call.call_args[0][0] | ||
gradio_dir = Path(gradio.__file__).parent | ||
demo_dir = Path("demo/calculator/run.py").resolve().parent | ||
return demo | ||
|
||
assert "uvicorn demo.calculator.run:demo.app" in reload_command | ||
assert f'--reload-dir "{gradio_dir}"' in reload_command | ||
assert f'--reload-dir "{demo_dir}"' in reload_command | ||
|
||
class TestReload: | ||
|
||
@pytest.fixture(autouse=True) | ||
def argv(self): | ||
return ["demo/calculator/run.py"] | ||
|
||
@pytest.fixture | ||
def config(self, monkeypatch, argv): | ||
monkeypatch.setattr("sys.argv", ["gradio"] + argv) | ||
return _setup_config() | ||
|
||
@pytest.fixture(params=[{}]) | ||
def reloader(self, config): | ||
reloader = Server(config) | ||
reloader.should_exit = True | ||
yield reloader | ||
reloader.handle_exit(2, None) | ||
|
||
def test_config_default_app(self, config): | ||
assert "demo.calculator.run:demo.app" == config.app | ||
|
||
@pytest.mark.parametrize("argv", [["demo/calculator/run.py", "test.app"]]) | ||
def test_config_custom_app(self, config): | ||
assert "demo.calculator.run:test.app" == config.app | ||
|
||
def test_config_watch_gradio(self, config): | ||
gradio_dir = Path(gradio.__file__).parent | ||
assert gradio_dir in config.reload_dirs | ||
|
||
def test_config_watch_app(self, config): | ||
demo_dir = Path("demo/calculator/run.py").resolve().parent | ||
assert demo_dir in config.reload_dirs | ||
|
||
def test_config_load_default(self, config): | ||
config.load() | ||
assert config.loaded == True | ||
|
||
@pytest.mark.parametrize("argv", [["test/test_reload.py", "build_demo"]]) | ||
def test_config_load_factory(self, config): | ||
config.load() | ||
assert config.loaded == True | ||
|
||
def test_reload_run_default(self, reloader): | ||
asyncio.run(reloader.serve()) | ||
assert reloader.started == True |