Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --help/--version crash in a partially configured app #1108

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,15 @@ def _get_option_with_source(

@pytest.hookimpl(trylast=True)
def pytest_configure(config: pytest.Config) -> None:
# Allow Django settings to be configured in a user pytest_configure call,
# but make sure we call django.setup()
if config.getoption("version", 0) > 0 or config.getoption("help", False):
return

# Normally Django is set up in `pytest_load_initial_conftests`, but we also
# allow users to not set DJANGO_SETTINGS_MODULE/`--ds` and instead
# configure the Django settings in a `pytest_configure` hookimpl using e.g.
# `settings.configure(...)`. In this case, the `_setup_django` call in
# `pytest_load_initial_conftests` only partially initializes Django, and
# it's fully initialized here.
_setup_django(config)


Expand Down Expand Up @@ -470,8 +477,7 @@ def get_order_number(test: pytest.Item) -> int:

@pytest.fixture(autouse=True, scope="session")
def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, None, None]:
"""
Ensure that Django is loaded and has its testing environment setup.
"""Setup Django's test environment for the testing session.

XXX It is a little dodgy that this is an autouse fixture. Perhaps
an email fixture should be requested in order to be able to
Expand All @@ -481,7 +487,6 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N
we need to follow this model.
"""
if django_settings_is_configured():
_setup_django(request.config)
from django.test.utils import setup_test_environment, teardown_test_environment

debug_ini = request.config.getini("django_debug_mode")
Expand Down
31 changes: 31 additions & 0 deletions tests/test_manage_py_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,37 @@ def test_django_project_found_invalid_settings_version(
result.stdout.fnmatch_lines(["*usage:*"])


@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_django_project_late_settings_version(
django_pytester: DjangoPytester,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Late configuration should not cause an error with --help or --version."""
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
django_pytester.makepyfile(
t="WAT = 1",
)
django_pytester.makeconftest(
"""
import os

def pytest_configure():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 't')
from django.conf import settings
settings.WAT
"""
)

result = django_pytester.runpytest_subprocess("django_project_root", "--version", "--version")
assert result.ret == 0

result.stdout.fnmatch_lines(["*This is pytest version*"])

result = django_pytester.runpytest_subprocess("django_project_root", "--help")
assert result.ret == 0
result.stdout.fnmatch_lines(["*usage:*"])


@pytest.mark.django_project(project_root="django_project_root", create_manage_py=True)
def test_runs_without_error_on_long_args(django_pytester: DjangoPytester) -> None:
django_pytester.create_test_module(
Expand Down