Skip to content

Commit 3ec54d6

Browse files
blueyedpelme
authored andcommitted
Add type=bool for django_find_project/INVALID_TEMPLATE_VARS_ENV (#364)
This refactors the _parse_django_find_project_ini method into _get_boolean_value, and uses it for INVALID_TEMPLATE_VARS_ENV, too.
1 parent 313f26d commit 3ec54d6

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

pytest_django/plugin.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ def pytest_addoption(parser):
7575
parser.addini('django_find_project',
7676
'Automatically find and add a Django project to the '
7777
'Python path.',
78-
default=True)
78+
type='bool', default=True)
7979
group._addoption('--fail-on-template-vars',
8080
action='store_true', dest='itv', default=False,
8181
help='Fail for invalid variables in templates.')
8282
parser.addini(INVALID_TEMPLATE_VARS_ENV,
8383
'Fail for invalid variables in templates.',
84-
default=False)
84+
type='bool', default=False)
8585

8686

8787
def _exists(path, ignore=EnvironmentError):
@@ -149,22 +149,21 @@ def _setup_django():
149149
_blocking_manager.disable_database_access()
150150

151151

152-
def _parse_django_find_project_ini(x):
152+
def _get_boolean_value(x, name, default=None):
153+
if x is None:
154+
return default
153155
if x in (True, False):
154156
return x
155-
156-
x = x.lower()
157157
possible_values = {'true': True,
158158
'false': False,
159159
'1': True,
160160
'0': False}
161-
162161
try:
163-
return possible_values[x]
162+
return possible_values[x.lower()]
164163
except KeyError:
165-
raise ValueError('%s is not a valid value for django_find_project. '
166-
'It must be one of %s.'
167-
% (x, ', '.join(possible_values.keys())))
164+
raise ValueError('{} is not a valid value for {}. '
165+
'It must be one of {}.'
166+
% (x, name, ', '.join(possible_values.keys())))
168167

169168

170169
def pytest_load_initial_conftests(early_config, parser, args):
@@ -187,20 +186,18 @@ def pytest_load_initial_conftests(early_config, parser, args):
187186
if options.version or options.help:
188187
return
189188

190-
django_find_project = _parse_django_find_project_ini(
191-
early_config.getini('django_find_project'))
189+
django_find_project = _get_boolean_value(
190+
early_config.getini('django_find_project'), 'django_find_project')
192191

193192
if django_find_project:
194193
_django_project_scan_outcome = _add_django_project_to_path(args)
195194
else:
196195
_django_project_scan_outcome = PROJECT_SCAN_DISABLED
197196

198-
# Configure FAIL_INVALID_TEMPLATE_VARS
199-
itv = (options.itv or
200-
os.environ.get(INVALID_TEMPLATE_VARS_ENV) in ['true', 'True', '1'] or
201-
early_config.getini(INVALID_TEMPLATE_VARS_ENV))
202-
203-
if itv:
197+
if (options.itv or
198+
_get_boolean_value(os.environ.get(INVALID_TEMPLATE_VARS_ENV),
199+
INVALID_TEMPLATE_VARS_ENV) or
200+
early_config.getini(INVALID_TEMPLATE_VARS_ENV)):
204201
os.environ[INVALID_TEMPLATE_VARS_ENV] = 'true'
205202

206203
# Configure DJANGO_SETTINGS_MODULE

0 commit comments

Comments
 (0)