2022-08-23 04:04:59 +08:00
|
|
|
import inspect
|
2022-08-17 01:21:13 +08:00
|
|
|
import pathlib
|
2023-05-20 05:22:12 +08:00
|
|
|
from contextlib import contextmanager
|
2022-08-17 01:21:13 +08:00
|
|
|
|
|
|
|
import pytest
|
2023-05-20 05:22:12 +08:00
|
|
|
from gradio_client import Client
|
2022-08-17 01:21:13 +08:00
|
|
|
|
2022-08-23 04:04:59 +08:00
|
|
|
import gradio as gr
|
2024-05-03 07:07:45 +08:00
|
|
|
import gradio.utils
|
2022-08-23 04:04:59 +08:00
|
|
|
|
2022-08-17 01:21:13 +08:00
|
|
|
|
2022-07-12 01:20:11 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
config.addinivalue_line(
|
|
|
|
"markers", "flaky: mark test as flaky. Failure will not cause te"
|
|
|
|
)
|
2022-08-17 01:21:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def test_file_dir():
|
|
|
|
return pathlib.Path(pathlib.Path(__file__).parent, "test_files")
|
2022-08-23 04:04:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def io_components():
|
2024-05-03 07:07:45 +08:00
|
|
|
all_subclasses = gradio.utils.core_gradio_components()
|
2022-08-23 04:04:59 +08:00
|
|
|
subclasses = []
|
2024-05-03 07:07:45 +08:00
|
|
|
for subclass in all_subclasses:
|
2024-03-06 04:41:39 +08:00
|
|
|
if subclass in [
|
|
|
|
gr.components.FormComponent,
|
|
|
|
gr.State,
|
|
|
|
gr.LoginButton,
|
2024-06-29 07:53:44 +08:00
|
|
|
gr.Timer,
|
2024-03-06 04:41:39 +08:00
|
|
|
]:
|
2023-10-31 12:46:02 +08:00
|
|
|
continue
|
2024-05-03 07:07:45 +08:00
|
|
|
if subclass in gr.components.FormComponent.__subclasses__():
|
|
|
|
continue
|
2022-08-23 04:04:59 +08:00
|
|
|
|
2023-09-19 12:17:06 +08:00
|
|
|
if "value" in inspect.signature(subclass.__init__).parameters:
|
2022-08-23 04:04:59 +08:00
|
|
|
subclasses.append(subclass)
|
|
|
|
|
|
|
|
return subclasses
|
2023-05-20 05:22:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def connect():
|
|
|
|
@contextmanager
|
2024-06-05 08:26:28 +08:00
|
|
|
def _connect(demo: gr.Blocks, **kwargs):
|
2024-03-05 09:51:02 +08:00
|
|
|
_, local_url, _ = demo.launch(prevent_thread_lock=True, **kwargs)
|
2023-05-20 05:22:12 +08:00
|
|
|
try:
|
2024-06-05 08:26:28 +08:00
|
|
|
client = Client(local_url)
|
2024-04-02 06:31:56 +08:00
|
|
|
yield client
|
2023-05-20 05:22:12 +08:00
|
|
|
finally:
|
2024-07-22 10:55:18 +08:00
|
|
|
client.close() # type: ignore
|
2024-04-02 06:31:56 +08:00
|
|
|
demo.close()
|
2023-05-20 05:22:12 +08:00
|
|
|
|
|
|
|
return _connect
|
2023-07-12 23:19:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def gradio_temp_dir(monkeypatch, tmp_path):
|
|
|
|
"""tmp_path is unique to each test function.
|
|
|
|
It will be cleared automatically according to pytest docs: https://docs.pytest.org/en/6.2.x/reference.html#tmp-path
|
|
|
|
"""
|
|
|
|
monkeypatch.setenv("GRADIO_TEMP_DIR", str(tmp_path))
|
|
|
|
return tmp_path
|
2024-03-08 04:39:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def clear_static_files():
|
|
|
|
"""Clears all static files from the _StaticFiles class.
|
|
|
|
|
|
|
|
This is necessary because the tests should be independent of one another.
|
|
|
|
"""
|
|
|
|
yield
|
|
|
|
from gradio import data_classes
|
|
|
|
|
|
|
|
data_classes._StaticFiles.clear()
|