From 4b78b17d1320f0a18a61d787384f1d4ebfad7c52 Mon Sep 17 00:00:00 2001 From: Karan Gathani Date: Thu, 27 Jun 2024 15:23:45 -0700 Subject: [PATCH 1/3] dynamically determine the path to the shiny app --- CHANGELOG.md | 2 +- shiny/pytest/_fixture.py | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d5a85fc88..b86c7f9e61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Expose `shiny.playwright`, `shiny.run`, and `shiny.pytest` modules that allow users to testing their Shiny apps. (#1448, #1456, #1481) * `shiny.playwright` contains `controller` and `expect` submodules. `controller` will contain many classes to interact with (and verify!) your Shiny app using Playwright. `expect` contains expectation functions that enhance standard Playwright expectation methods. * `shiny.run` contains the `run_shiny_app` command and the return type `ShinyAppProc`. `ShinyAppProc` can be used to type the Shiny app pytest fixtures. - * `shiny.pytest` contains pytest test fixtures. The `local_app` pytest fixture is automatically available and runs a sibling `app.py` file. Where as `create_app_fixture(PATH_TO_APP)` allows for a Shiny app to be instantiated from a different folder. + * `shiny.pytest` contains pytest test fixtures. The `local_app` pytest fixture is automatically available and runs a sibling `app.py` file. Where as `create_app_fixture(PATH_TO_APP)` allows for a relative path to a Shiny app to be instantiated from a different folder. * Added CLI command `shiny add test` to add a test file to an existing Shiny app. (#1461) diff --git a/shiny/pytest/_fixture.py b/shiny/pytest/_fixture.py index e0c3c6df24..6737bf4971 100644 --- a/shiny/pytest/_fixture.py +++ b/shiny/pytest/_fixture.py @@ -1,12 +1,14 @@ from __future__ import annotations -from pathlib import PurePath +import inspect +from pathlib import Path, PurePath from typing import Literal, Union import pytest from .._docstring import no_example from ..run._run import shiny_app_gen +from ..types import MISSING, MISSING_TYPE __all__ = ( "create_app_fixture", @@ -21,6 +23,8 @@ def create_app_fixture( app: Union[PurePath, str], scope: ScopeName = "module", + # *, + # start_dir: Union[PurePath, str, MISSING_TYPE, None] = MISSING, ): """ Create a fixture for a local Shiny app directory. @@ -29,13 +33,22 @@ def create_app_fixture( ---------- app The path to the Shiny app file. + + If `app` is a `Path` or `PurePath` instance and `.is_file()` returns `True`, then this value will be used directly. + Note, file paths will be checked from where `pytest` was called, not from the calling function. + + Otherwise, all `app` paths will be considered to be relative paths from where the test function was collected. + + To be sure that your app path is always relative, supply a `str` value. scope The scope of the fixture. """ @pytest.fixture(scope=scope) - def fixture_func(): - sa_gen = shiny_app_gen(app) + def fixture_func(request: pytest.FixtureRequest): + app_purepath_exists = isinstance(app, PurePath) and Path(app).is_file() + app_path = app if app_purepath_exists else request.path.parent / app + sa_gen = shiny_app_gen(app_path) yield next(sa_gen) return fixture_func From 9127131b2418a877ce8aae9546dfdbe0e6a46f04 Mon Sep 17 00:00:00 2001 From: Karan Gathani Date: Thu, 27 Jun 2024 15:35:09 -0700 Subject: [PATCH 2/3] address linting issues --- shiny/pytest/_fixture.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/shiny/pytest/_fixture.py b/shiny/pytest/_fixture.py index 6737bf4971..7e7eb53c7f 100644 --- a/shiny/pytest/_fixture.py +++ b/shiny/pytest/_fixture.py @@ -1,6 +1,5 @@ from __future__ import annotations -import inspect from pathlib import Path, PurePath from typing import Literal, Union @@ -8,7 +7,6 @@ from .._docstring import no_example from ..run._run import shiny_app_gen -from ..types import MISSING, MISSING_TYPE __all__ = ( "create_app_fixture", @@ -23,8 +21,6 @@ def create_app_fixture( app: Union[PurePath, str], scope: ScopeName = "module", - # *, - # start_dir: Union[PurePath, str, MISSING_TYPE, None] = MISSING, ): """ Create a fixture for a local Shiny app directory. From 634c6625106a77b06458ca9f48e627f2e5309336 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Tue, 2 Jul 2024 09:43:49 -0400 Subject: [PATCH 3/3] Apply suggestions from code review --- shiny/pytest/_fixture.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shiny/pytest/_fixture.py b/shiny/pytest/_fixture.py index 7e7eb53c7f..bb649e7f51 100644 --- a/shiny/pytest/_fixture.py +++ b/shiny/pytest/_fixture.py @@ -30,8 +30,8 @@ def create_app_fixture( app The path to the Shiny app file. - If `app` is a `Path` or `PurePath` instance and `.is_file()` returns `True`, then this value will be used directly. - Note, file paths will be checked from where `pytest` was called, not from the calling function. + If `app` is a `Path` or `PurePath` instance and `Path(app).is_file()` returns `True`, then this value will be used directly. + Note, `app`'s file path will be checked from where corresponding `pytest` test is collected, not necessarily where `create_app_fixture()` is called. Otherwise, all `app` paths will be considered to be relative paths from where the test function was collected.