Skip to content

Commit 86a65c5

Browse files
committed
fixup! Add type validation.
1 parent 733d719 commit 86a65c5

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
3.0.5.dev0
22
==========
33

4+
* Add Argparse style type validation for ``--confcutdir`` and ``--junit-xml`` (`#2089`_).
5+
Thanks to `@lwm`_ for the PR.
6+
47
* Add hint to error message hinting possible missing ``__init__.py`` (`#478`_). Thanks `@DuncanBetts`_.
58

69
* Provide ``:ref:`` targets for ``recwarn.rst`` so we can use intersphinx referencing.
@@ -33,6 +36,7 @@
3336
.. _@nedbat: https://github.com/nedbat
3437
.. _@nmundar: https://github.com/nmundar
3538

39+
.. _#2089: https://github.com/pytest-dev/pytest/issues/2089
3640
.. _#478: https://github.com/pytest-dev/pytest/issues/478
3741
.. _#2034: https://github.com/pytest-dev/pytest/issues/2034
3842
.. _#2038: https://github.com/pytest-dev/pytest/issues/2038

_pytest/config.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,24 @@ class UsageError(Exception):
7070
""" error in pytest usage or invocation"""
7171

7272

73-
def filename(path):
74-
"""Argparse type validator for filename arguments"""
73+
def filename_arg(path, optname):
74+
""" Argparse type validator for filename arguments.
75+
76+
:path: path of filename
77+
:optname: name of the option
78+
"""
7579
if os.path.isdir(path):
76-
raise UsageError("Must be a filename, given: {0}".format(path))
80+
raise UsageError("{0} must be a filename, given: {1}".format(optname, path))
7781
return path
7882

79-
def directory(path):
80-
"""Argparse type validator for directory arguments"""
83+
def directory_arg(path, optname):
84+
"""Argparse type validator for directory arguments.
85+
86+
:path: path of directory
87+
:optname: name of the option
88+
"""
8189
if not os.path.isdir(path):
82-
raise UsageError("Must be a directory, given: {0}".format(path))
90+
raise UsageError("{0} must be a directory, given: {1}".format(optname, path))
8391
return path
8492

8593
_preinit = []

_pytest/junitxml.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
# Output conforms to https://github.com/jenkinsci/xunit-plugin/blob/master/
99
# src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
1010

11+
import functools
1112
import py
1213
import os
1314
import re
1415
import sys
1516
import time
1617
import pytest
17-
from _pytest.config import filename
18+
from _pytest.config import filename_arg
1819

1920
# Python 2.X and 3.X compatibility
2021
if sys.version_info[0] < 3:
@@ -215,7 +216,7 @@ def pytest_addoption(parser):
215216
action="store",
216217
dest="xmlpath",
217218
metavar="path",
218-
type=filename,
219+
type=functools.partial(filename_arg, optname="--junitxml"),
219220
default=None,
220221
help="create junit-xml style report file at given path.")
221222
group.addoption(

_pytest/main.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" core implementation of testing process: init, session, runtest loop. """
2+
import functools
23
import os
34
import sys
45

@@ -11,7 +12,7 @@
1112
except ImportError:
1213
from UserDict import DictMixin as MappingMixin
1314

14-
from _pytest.config import directory
15+
from _pytest.config import directory_arg
1516
from _pytest.runner import collect_one_node
1617

1718
tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
@@ -59,7 +60,7 @@ def pytest_addoption(parser):
5960
# when changing this to --conf-cut-dir, config.py Conftest.setinitial
6061
# needs upgrading as well
6162
group.addoption('--confcutdir', dest="confcutdir", default=None,
62-
metavar="dir", type=directory,
63+
metavar="dir", type=functools.partial(directory_arg, optname="--confcutdir"),
6364
help="only load conftest.py's relative to specified dir.")
6465
group.addoption('--noconftest', action="store_true",
6566
dest="noconftest", default=False,

testing/test_junitxml.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ def test_pass():
718718
def test_logxml_check_isdir(testdir):
719719
"""Give an error if --junit-xml is a directory (#2089)"""
720720
result = testdir.runpytest("--junit-xml=.")
721-
result.stderr.fnmatch_lines(["*Must be a filename*"])
721+
result.stderr.fnmatch_lines(["*--junitxml must be a filename*"])
722722

723723
def test_escaped_parametrized_names_xml(testdir):
724724
testdir.makepyfile("""

0 commit comments

Comments
 (0)