Skip to content

Commit 2bb0135

Browse files
authored
Report django-configurations setting (#791)
Closes #790
1 parent 1625ece commit 2bb0135

File tree

3 files changed

+73
-45
lines changed

3 files changed

+73
-45
lines changed

pytest_django/plugin.py

+17-29
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
# pytest 4.2 handles unittest setup/teardown itself via wrapping fixtures.
5656
_handle_unittest_methods = parse_version(pytest.__version__) < parse_version("4.2")
5757

58+
_report_header = []
59+
5860

5961
# ############### pytest hooks ################
6062

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

290-
# Configure DJANGO_SETTINGS_MODULE
291-
if options.ds:
292-
ds_source = "command line option"
293-
ds = options.ds
294-
elif SETTINGS_MODULE_ENV in os.environ:
295-
ds = os.environ[SETTINGS_MODULE_ENV]
296-
ds_source = "environment variable"
297-
elif early_config.getini(SETTINGS_MODULE_ENV):
298-
ds = early_config.getini(SETTINGS_MODULE_ENV)
299-
ds_source = "ini file"
300-
else:
301-
ds = None
302-
ds_source = None
292+
def _get_option_with_source(option, envname):
293+
if option:
294+
return option, "option"
295+
if envname in os.environ:
296+
return os.environ[envname], "env"
297+
cfgval = early_config.getini(envname)
298+
if cfgval:
299+
return cfgval, "ini"
300+
return None, None
303301

304-
if ds:
305-
early_config._dsm_report_header = "Django settings: %s (from %s)" % (
306-
ds,
307-
ds_source,
308-
)
309-
else:
310-
early_config._dsm_report_header = None
311-
312-
# Configure DJANGO_CONFIGURATION
313-
dc = (
314-
options.dc
315-
or os.environ.get(CONFIGURATION_ENV)
316-
or early_config.getini(CONFIGURATION_ENV)
317-
)
302+
ds, ds_source = _get_option_with_source(options.ds, SETTINGS_MODULE_ENV)
303+
dc, dc_source = _get_option_with_source(options.dc, CONFIGURATION_ENV)
318304

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

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

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

339327

340328
def pytest_report_header(config):
341-
if config._dsm_report_header:
342-
return [config._dsm_report_header]
329+
if _report_header:
330+
return ["django: " + ", ".join(_report_header)]
343331

344332

345333
@pytest.mark.trylast

tests/test_django_configurations.py

+44-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ def test_settings():
4040
"""
4141
)
4242
result = testdir.runpytest_subprocess()
43-
result.stdout.fnmatch_lines(["* 1 passed in*"])
43+
result.stdout.fnmatch_lines([
44+
'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)',
45+
"* 1 passed in*",
46+
])
4447
assert result.ret == 0
4548

4649

47-
def test_dc_ini(testdir, monkeypatch):
50+
def test_dc_env_overrides_ini(testdir, monkeypatch):
4851
monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env")
4952
monkeypatch.setenv("DJANGO_CONFIGURATION", "MySettings")
5053

@@ -68,7 +71,40 @@ def test_ds():
6871
"""
6972
)
7073
result = testdir.runpytest_subprocess()
71-
result.stdout.fnmatch_lines(["* 1 passed in*"])
74+
result.stdout.fnmatch_lines([
75+
'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)',
76+
"* 1 passed in*",
77+
])
78+
assert result.ret == 0
79+
80+
81+
def test_dc_ini(testdir, monkeypatch):
82+
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
83+
84+
testdir.makeini(
85+
"""
86+
[pytest]
87+
DJANGO_SETTINGS_MODULE = tpkg.settings_ini
88+
DJANGO_CONFIGURATION = MySettings
89+
"""
90+
)
91+
pkg = testdir.mkpydir("tpkg")
92+
settings = pkg.join("settings_ini.py")
93+
settings.write(BARE_SETTINGS)
94+
testdir.makepyfile(
95+
"""
96+
import os
97+
98+
def test_ds():
99+
assert os.environ['DJANGO_SETTINGS_MODULE'] == 'tpkg.settings_ini'
100+
assert os.environ['DJANGO_CONFIGURATION'] == 'MySettings'
101+
"""
102+
)
103+
result = testdir.runpytest_subprocess()
104+
result.stdout.fnmatch_lines([
105+
'django: settings: tpkg.settings_ini (from ini), configuration: MySettings (from ini)',
106+
"* 1 passed in*",
107+
])
72108
assert result.ret == 0
73109

74110

@@ -96,5 +132,9 @@ def test_ds():
96132
"""
97133
)
98134
result = testdir.runpytest_subprocess("--ds=tpkg.settings_opt", "--dc=MySettings")
99-
result.stdout.fnmatch_lines(["* 1 passed in*"])
135+
result.stdout.fnmatch_lines([
136+
'django: settings: tpkg.settings_opt (from option),'
137+
' configuration: MySettings (from option)',
138+
"* 1 passed in*",
139+
])
100140
assert result.ret == 0

tests/test_django_settings_module.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ def test_ds():
3838
"""
3939
)
4040
result = testdir.runpytest_subprocess()
41-
assert result.parseoutcomes()["passed"] == 1
42-
result.stdout.fnmatch_lines(
43-
["Django settings: tpkg.settings_ini " "(from ini file)*"]
44-
)
41+
result.stdout.fnmatch_lines([
42+
"django: settings: tpkg.settings_ini (from ini)",
43+
"*= 1 passed in *",
44+
])
4545
assert result.ret == 0
4646

4747

@@ -59,10 +59,10 @@ def test_settings():
5959
"""
6060
)
6161
result = testdir.runpytest_subprocess()
62-
result.stdout.fnmatch_lines(
63-
["Django settings: tpkg.settings_env (from " "environment variable)*"]
64-
)
65-
assert result.parseoutcomes()["passed"] == 1
62+
result.stdout.fnmatch_lines([
63+
"django: settings: tpkg.settings_env (from env)",
64+
"*= 1 passed in *",
65+
])
6666

6767

6868
def test_ds_option(testdir, monkeypatch):
@@ -85,10 +85,10 @@ def test_ds():
8585
"""
8686
)
8787
result = testdir.runpytest_subprocess("--ds=tpkg.settings_opt")
88-
result.stdout.fnmatch_lines(
89-
["Django settings: tpkg.settings_opt " "(from command line option)"]
90-
)
91-
assert result.parseoutcomes()["passed"] == 1
88+
result.stdout.fnmatch_lines([
89+
"django: settings: tpkg.settings_opt (from option)",
90+
"*= 1 passed in *",
91+
])
9292

9393

9494
def test_ds_env_override_ini(testdir, monkeypatch):

0 commit comments

Comments
 (0)