Skip to content

Commit

Permalink
Report django-configurations setting (#791)
Browse files Browse the repository at this point in the history
Closes #790
  • Loading branch information
blueyed committed Jan 7, 2020
1 parent 1625ece commit 2bb0135
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 45 deletions.
46 changes: 17 additions & 29 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
# pytest 4.2 handles unittest setup/teardown itself via wrapping fixtures.
_handle_unittest_methods = parse_version(pytest.__version__) < parse_version("4.2")

_report_header = []


# ############### pytest hooks ################

Expand Down Expand Up @@ -287,39 +289,25 @@ def pytest_load_initial_conftests(early_config, parser, args):
):
os.environ[INVALID_TEMPLATE_VARS_ENV] = "true"

# Configure DJANGO_SETTINGS_MODULE
if options.ds:
ds_source = "command line option"
ds = options.ds
elif SETTINGS_MODULE_ENV in os.environ:
ds = os.environ[SETTINGS_MODULE_ENV]
ds_source = "environment variable"
elif early_config.getini(SETTINGS_MODULE_ENV):
ds = early_config.getini(SETTINGS_MODULE_ENV)
ds_source = "ini file"
else:
ds = None
ds_source = None
def _get_option_with_source(option, envname):
if option:
return option, "option"
if envname in os.environ:
return os.environ[envname], "env"
cfgval = early_config.getini(envname)
if cfgval:
return cfgval, "ini"
return None, None

if ds:
early_config._dsm_report_header = "Django settings: %s (from %s)" % (
ds,
ds_source,
)
else:
early_config._dsm_report_header = None

# Configure DJANGO_CONFIGURATION
dc = (
options.dc
or os.environ.get(CONFIGURATION_ENV)
or early_config.getini(CONFIGURATION_ENV)
)
ds, ds_source = _get_option_with_source(options.ds, SETTINGS_MODULE_ENV)
dc, dc_source = _get_option_with_source(options.dc, CONFIGURATION_ENV)

if ds:
_report_header.append("settings: %s (from %s)" % (ds, ds_source))
os.environ[SETTINGS_MODULE_ENV] = ds

if dc:
_report_header.append("configuration: %s (from %s)" % (dc, dc_source))
os.environ[CONFIGURATION_ENV] = dc

# Install the django-configurations importer
Expand All @@ -338,8 +326,8 @@ def pytest_load_initial_conftests(early_config, parser, args):


def pytest_report_header(config):
if config._dsm_report_header:
return [config._dsm_report_header]
if _report_header:
return ["django: " + ", ".join(_report_header)]


@pytest.mark.trylast
Expand Down
48 changes: 44 additions & 4 deletions tests/test_django_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ def test_settings():
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["* 1 passed in*"])
result.stdout.fnmatch_lines([
'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)',
"* 1 passed in*",
])
assert result.ret == 0


def test_dc_ini(testdir, monkeypatch):
def test_dc_env_overrides_ini(testdir, monkeypatch):
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env")
monkeypatch.setenv("DJANGO_CONFIGURATION", "MySettings")

Expand All @@ -68,7 +71,40 @@ def test_ds():
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["* 1 passed in*"])
result.stdout.fnmatch_lines([
'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)',
"* 1 passed in*",
])
assert result.ret == 0


def test_dc_ini(testdir, monkeypatch):
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")

testdir.makeini(
"""
[pytest]
DJANGO_SETTINGS_MODULE = tpkg.settings_ini
DJANGO_CONFIGURATION = MySettings
"""
)
pkg = testdir.mkpydir("tpkg")
settings = pkg.join("settings_ini.py")
settings.write(BARE_SETTINGS)
testdir.makepyfile(
"""
import os
def test_ds():
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_ini'
assert os.environ['DJANGO_CONFIGURATION'] == 'MySettings'
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines([
'django: settings: tpkg.settings_ini (from ini), configuration: MySettings (from ini)',
"* 1 passed in*",
])
assert result.ret == 0


Expand Down Expand Up @@ -96,5 +132,9 @@ def test_ds():
"""
)
result = testdir.runpytest_subprocess("--ds=tpkg.settings_opt", "--dc=MySettings")
result.stdout.fnmatch_lines(["* 1 passed in*"])
result.stdout.fnmatch_lines([
'django: settings: tpkg.settings_opt (from option),'
' configuration: MySettings (from option)',
"* 1 passed in*",
])
assert result.ret == 0
24 changes: 12 additions & 12 deletions tests/test_django_settings_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def test_ds():
"""
)
result = testdir.runpytest_subprocess()
assert result.parseoutcomes()["passed"] == 1
result.stdout.fnmatch_lines(
["Django settings: tpkg.settings_ini " "(from ini file)*"]
)
result.stdout.fnmatch_lines([
"django: settings: tpkg.settings_ini (from ini)",
"*= 1 passed in *",
])
assert result.ret == 0


Expand All @@ -59,10 +59,10 @@ def test_settings():
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(
["Django settings: tpkg.settings_env (from " "environment variable)*"]
)
assert result.parseoutcomes()["passed"] == 1
result.stdout.fnmatch_lines([
"django: settings: tpkg.settings_env (from env)",
"*= 1 passed in *",
])


def test_ds_option(testdir, monkeypatch):
Expand All @@ -85,10 +85,10 @@ def test_ds():
"""
)
result = testdir.runpytest_subprocess("--ds=tpkg.settings_opt")
result.stdout.fnmatch_lines(
["Django settings: tpkg.settings_opt " "(from command line option)"]
)
assert result.parseoutcomes()["passed"] == 1
result.stdout.fnmatch_lines([
"django: settings: tpkg.settings_opt (from option)",
"*= 1 passed in *",
])


def test_ds_env_override_ini(testdir, monkeypatch):
Expand Down

0 comments on commit 2bb0135

Please sign in to comment.