forked from orbit-oss/flask
Merge pull request #2592 from pallets/tests-env
reset standard os env after each test
This commit is contained in:
commit
2949306b7b
2 changed files with 43 additions and 18 deletions
|
|
@ -6,16 +6,52 @@
|
||||||
:copyright: (c) 2015 by the Flask Team, see AUTHORS for more details.
|
:copyright: (c) 2015 by the Flask Team, see AUTHORS for more details.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
import flask
|
|
||||||
import gc
|
import gc
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import pytest
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from _pytest import monkeypatch
|
||||||
|
|
||||||
|
import flask
|
||||||
from flask import Flask as _Flask
|
from flask import Flask as _Flask
|
||||||
|
|
||||||
|
|
||||||
|
@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_APP', monkeypatch.notset),
|
||||||
|
(os.environ, 'FLASK_ENV', 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()
|
||||||
|
|
||||||
|
|
||||||
|
@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)
|
||||||
|
|
||||||
|
|
||||||
class Flask(_Flask):
|
class Flask(_Flask):
|
||||||
testing = True
|
testing = True
|
||||||
secret_key = 'test key'
|
secret_key = 'test key'
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,11 @@ from _pytest.monkeypatch import notset
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
|
|
||||||
from flask import Flask, current_app
|
from flask import Flask, current_app
|
||||||
from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv, \
|
from flask.cli import (
|
||||||
find_best_app, get_version, load_dotenv, locate_app, prepare_import, \
|
AppGroup, FlaskGroup, NoAppException, ScriptInfo, dotenv,
|
||||||
|
find_best_app, get_version, load_dotenv, locate_app, prepare_import,
|
||||||
with_appcontext
|
with_appcontext
|
||||||
|
)
|
||||||
|
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
test_path = os.path.abspath(os.path.join(
|
test_path = os.path.abspath(os.path.join(
|
||||||
|
|
@ -33,19 +35,6 @@ test_path = os.path.abspath(os.path.join(
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def manage_os_environ(monkeypatch):
|
|
||||||
# can't use monkeypatch.delitem since we don't want to restore a value
|
|
||||||
os.environ.pop('FLASK_APP', None)
|
|
||||||
os.environ.pop('FLASK_DEBUG', None)
|
|
||||||
# use monkeypatch internals to force-delete environ keys
|
|
||||||
monkeypatch._setitem.extend((
|
|
||||||
(os.environ, 'FLASK_APP', notset),
|
|
||||||
(os.environ, 'FLASK_DEBUG', notset),
|
|
||||||
(os.environ, 'FLASK_RUN_FROM_CLI', notset),
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def runner():
|
def runner():
|
||||||
return CliRunner()
|
return CliRunner()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue