Skip to content

Commit

Permalink
Improve pytest fixtures for temporary paths
Browse files Browse the repository at this point in the history
aff93b5
02a9645
397638e

- Ensure `Path()` is used for `shutil` arguments instead of string
- Create temporary directory for Gunicorn with `tmp_path_factory`
- Copy gunicorn_conf.py to temporary directory
- Update test_start.py with new Gunicorn paths
  • Loading branch information
br3ndonland committed Sep 20, 2020
1 parent 55baa72 commit c1a6f3a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
25 changes: 19 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def app_module_tmp_path(tmp_path_factory: TempPathFactory) -> Path:
"""Copy app modules to temporary directory to test custom app module paths."""
tmp_dir = tmp_path_factory.mktemp("app")
shutil.copytree(f"{Path(pre_start_module.__file__).parent}", f"{tmp_dir}/tmp_app")
shutil.copytree(Path(pre_start_module.__file__).parent, Path(f"{tmp_dir}/tmp_app"))
return tmp_dir


Expand Down Expand Up @@ -61,11 +61,24 @@ def gunicorn_conf_path(monkeypatch: MonkeyPatch) -> Path:
return path


@pytest.fixture(scope="session")
def gunicorn_conf_tmp_path(tmp_path_factory: TempPathFactory) -> Path:
"""Create temporary directory for Gunicorn configuration file."""
return tmp_path_factory.mktemp("gunicorn")


@pytest.fixture
def gunicorn_conf_path_tmp(tmp_path: Path) -> Path:
"""Copy gunicorn configuration file to custom temporary file."""
tmp_file = shutil.copy(Path(gunicorn_conf_module.__file__), tmp_path)
return Path(tmp_file)
def gunicorn_conf_tmp_file_path(
gunicorn_conf_tmp_path: Path,
monkeypatch: MonkeyPatch,
tmp_path_factory: TempPathFactory,
) -> Path:
"""Copy gunicorn configuration file to temporary directory."""
tmp_file = Path(f"{gunicorn_conf_tmp_path}/gunicorn_conf.py")
shutil.copy(Path(gunicorn_conf_module.__file__), tmp_file)
monkeypatch.setenv("GUNICORN_CONF", str(tmp_file))
assert os.getenv("GUNICORN_CONF", str(tmp_file))
return tmp_file


@pytest.fixture
Expand Down Expand Up @@ -96,7 +109,7 @@ def logging_conf_module_path(monkeypatch: MonkeyPatch) -> str:
def logging_conf_tmp_file_path(tmp_path_factory: TempPathFactory) -> Path:
"""Copy logging configuration module to custom temporary location."""
tmp_dir = tmp_path_factory.mktemp("tmp_log")
shutil.copy(Path(logging_conf_module.__file__), f"{tmp_dir}/tmp_log.py")
shutil.copy(Path(logging_conf_module.__file__), Path(f"{tmp_dir}/tmp_log.py"))
return tmp_dir


Expand Down
19 changes: 11 additions & 8 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ def test_set_default_conf_path_gunicorn(self, gunicorn_conf_path: Path) -> None:
assert start.set_conf_path("gunicorn") == str(gunicorn_conf_path)

def test_set_custom_conf_path_gunicorn(
self, gunicorn_conf_path_tmp: Path, monkeypatch: MonkeyPatch, tmp_path: Path
self,
gunicorn_conf_tmp_file_path: Path,
monkeypatch: MonkeyPatch,
tmp_path: Path,
) -> None:
"""Set path to custom temporary Gunicorn configuration file."""
monkeypatch.setenv("GUNICORN_CONF", str(gunicorn_conf_path_tmp))
assert os.getenv("GUNICORN_CONF") == str(gunicorn_conf_path_tmp)
assert f"{tmp_path}/gunicorn_conf.py" in str(gunicorn_conf_path_tmp)
assert "logging" not in str(gunicorn_conf_path_tmp)
assert start.set_conf_path("gunicorn") == str(gunicorn_conf_path_tmp)
monkeypatch.setenv("GUNICORN_CONF", str(gunicorn_conf_tmp_file_path))
assert os.getenv("GUNICORN_CONF") == str(gunicorn_conf_tmp_file_path)
assert "/gunicorn_conf.py" in str(gunicorn_conf_tmp_file_path)
assert "logging" not in str(gunicorn_conf_tmp_file_path)
assert start.set_conf_path("gunicorn") == str(gunicorn_conf_tmp_file_path)

def test_set_incorrect_conf_path(self, monkeypatch: MonkeyPatch) -> None:
"""Set path to non-existent file and raise an error."""
Expand Down Expand Up @@ -404,14 +407,14 @@ def test_start_server_uvicorn_gunicorn(
def test_start_server_uvicorn_gunicorn_custom_config(
self,
app_module: str,
gunicorn_conf_path: Path,
gunicorn_conf_tmp_file_path: Path,
logging_conf_dict: Dict[str, Any],
mock_logger: logging.Logger,
mocker: MockerFixture,
monkeypatch: MonkeyPatch,
) -> None:
"""Test `start.start_server` with Uvicorn managed by Gunicorn."""
assert os.getenv("GUNICORN_CONF", str(gunicorn_conf_path))
assert os.getenv("GUNICORN_CONF") == str(gunicorn_conf_tmp_file_path)
monkeypatch.setenv("LOG_FORMAT", "gunicorn")
monkeypatch.setenv("LOG_LEVEL", "debug")
monkeypatch.setenv("MAX_WORKERS", "1")
Expand Down

0 comments on commit c1a6f3a

Please sign in to comment.