Skip to content

Commit

Permalink
Create temporary configuration files for testing
Browse files Browse the repository at this point in the history
- tmp_path/gunicorn_conf.py
- tmp_path/logging_conf.py
  • Loading branch information
br3ndonland committed Aug 29, 2020
1 parent aff93b5 commit 02a9645
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import shutil
from copy import deepcopy
from pathlib import Path
from typing import Any, Dict, List
Expand Down Expand Up @@ -44,6 +45,13 @@ def gunicorn_conf_path(mocker: MockerFixture, monkeypatch: MonkeyPatch) -> Path:
return path


@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)


@pytest.fixture
def logging_conf_dict() -> Dict[str, Any]:
"""Load logging configuration dictionary from logging configuration module."""
Expand All @@ -59,6 +67,13 @@ def logging_conf_path(mocker: MockerFixture, monkeypatch: MonkeyPatch) -> Path:
return path


@pytest.fixture
def logging_conf_path_tmp(tmp_path: Path) -> Path:
"""Copy logging configuration file to custom temporary file."""
tmp_file = shutil.copy(Path(logging_conf_module.__file__), tmp_path)
return Path(tmp_file)


@pytest.fixture
def mock_logger(mocker: MockerFixture) -> logging.Logger:
"""Mock the logger with pytest-mock and a pytest fixture.
Expand Down
24 changes: 24 additions & 0 deletions tests/test_start.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os
from pathlib import Path

# import pytest # type: ignore
from _pytest.monkeypatch import MonkeyPatch # type: ignore

from inboard import start


Expand All @@ -19,3 +23,23 @@ def test_set_default_conf_path_logging(self, logging_conf_path: Path) -> None:
assert "inboard/logging_conf.py" in str(logging_conf_path)
assert "gunicorn" not in str(logging_conf_path)
assert start.set_conf_path("logging") == logging_conf_path

def test_set_custom_conf_path_gunicorn(
self, gunicorn_conf_path_tmp: 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") == gunicorn_conf_path_tmp

def test_set_custom_conf_path_logging(
self, logging_conf_path_tmp: Path, monkeypatch: MonkeyPatch, tmp_path: Path
) -> None:
"""Set path to custom temporary logging configuration file."""
monkeypatch.setenv("LOGGING_CONF", str(logging_conf_path_tmp))
assert os.getenv("LOGGING_CONF") == str(logging_conf_path_tmp)
assert f"{tmp_path}/logging_conf.py" in str(logging_conf_path_tmp)
assert "gunicorn" not in str(logging_conf_path_tmp)
assert start.set_conf_path("logging") == logging_conf_path_tmp

0 comments on commit 02a9645

Please sign in to comment.