Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys

import pytest
from _pytest import monkeypatch

from flask import Flask
from flask.globals import app_ctx as _app_ctx
Expand All @@ -11,34 +10,22 @@
@pytest.fixture(scope="session", autouse=True)
def _standard_os_environ():
"""Set up ``os.environ`` at the start of the test session to have
standard values. Returns a list of operations that is used by
:func:`._reset_os_environ` after each test.
standard values.
"""
mp = monkeypatch.MonkeyPatch()
out = (
(os.environ, "FLASK_ENV_FILE", monkeypatch.notset),
(os.environ, "FLASK_APP", monkeypatch.notset),
(os.environ, "FLASK_DEBUG", monkeypatch.notset),
(os.environ, "FLASK_RUN_FROM_CLI", monkeypatch.notset),
(os.environ, "WERKZEUG_RUN_MAIN", monkeypatch.notset),
)

for _, key, value in out:
if value is monkeypatch.notset:
mp.delenv(key, False)
else:
mp.setenv(key, value)

yield out
mp.undo()
pass


@pytest.fixture(autouse=True)
def _reset_os_environ(monkeypatch, _standard_os_environ):
"""Reset ``os.environ`` to the standard environ after each test,
in case a test changed something without cleaning up.
"""
monkeypatch._setitem.extend(_standard_os_environ)
def _reset_os_environ(monkeypatch):
"""Reset ``os.environ`` to the standard environ after each test."""
for key in (
"FLASK_APP",
"FLASK_ENV_FILE",
"FLASK_DEBUG",
"FLASK_RUN_FROM_CLI",
"WERKZEUG_RUN_MAIN",
):
monkeypatch.delenv(key, raising=False)


@pytest.fixture
Expand Down
6 changes: 2 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import click
import pytest
from _pytest.monkeypatch import notset
from click.testing import CliRunner

from flask import Blueprint
Expand Down Expand Up @@ -535,9 +534,8 @@ def dotenv_not_available():

@need_dotenv
def test_load_dotenv(monkeypatch):
# can't use monkeypatch.delitem since the keys don't exist yet
for item in ("FOO", "BAR", "SPAM", "HAM"):
monkeypatch._setitem.append((os.environ, item, notset))
monkeypatch.delenv(item, raising=False)

monkeypatch.setenv("EGGS", "3")
monkeypatch.chdir(test_path)
Expand All @@ -560,7 +558,7 @@ def test_load_dotenv(monkeypatch):
@need_dotenv
def test_dotenv_path(monkeypatch):
for item in ("FOO", "BAR", "EGGS"):
monkeypatch._setitem.append((os.environ, item, notset))
monkeypatch.delenv(item, raising=False)

load_dotenv(test_path / ".flaskenv")
assert Path.cwd() == cwd
Expand Down
Loading