Skip to content
Closed

AI junk #6099

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
41 changes: 18 additions & 23 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,38 @@
import sys

import pytest
from _pytest import monkeypatch

from flask import Flask
from flask.globals import app_ctx as _app_ctx

_FLASK_ENV_KEYS = (
"FLASK_ENV_FILE",
"FLASK_APP",
"FLASK_DEBUG",
"FLASK_RUN_FROM_CLI",
"WERKZEUG_RUN_MAIN",
)


@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.
"""
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),
)
"""Remove Flask-related environment variables at the start of the test
session so they don't leak in from the developer's shell."""
mp = pytest.MonkeyPatch()

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

yield out
yield
mp.undo()


@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)
"""Remove Flask-related environment variables before each test in case a
previous test set them without cleaning up."""
for key in _FLASK_ENV_KEYS:
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