Skip to content

Commit 733d719

Browse files
committed
Add type validation.
Argparse driven argument type validation is added for the `--junit-xml` and `--confcutdir` arguments. The commit partially reverts #2080. Closes #2089.
1 parent 0735d45 commit 733d719

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

_pytest/config.py

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

7272

73+
def filename(path):
74+
"""Argparse type validator for filename arguments"""
75+
if os.path.isdir(path):
76+
raise UsageError("Must be a filename, given: {0}".format(path))
77+
return path
78+
79+
def directory(path):
80+
"""Argparse type validator for directory arguments"""
81+
if not os.path.isdir(path):
82+
raise UsageError("Must be a directory, given: {0}".format(path))
83+
return path
84+
7385
_preinit = []
7486

7587
default_plugins = (
@@ -996,7 +1008,6 @@ def _warn_about_missing_assertion(self, mode):
9961008
"(are you using python -O?)\n")
9971009

9981010
def _preparse(self, args, addopts=True):
999-
import pytest
10001011
self._initini(args)
10011012
if addopts:
10021013
args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args
@@ -1009,9 +1020,7 @@ def _preparse(self, args, addopts=True):
10091020
self.pluginmanager.consider_env()
10101021
self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy())
10111022
confcutdir = self.known_args_namespace.confcutdir
1012-
if confcutdir and not os.path.isdir(confcutdir):
1013-
raise pytest.UsageError('--confcutdir must be a directory, given: {0}'.format(confcutdir))
1014-
if confcutdir is None and self.inifile:
1023+
if self.known_args_namespace.confcutdir is None and self.inifile:
10151024
confcutdir = py.path.local(self.inifile).dirname
10161025
self.known_args_namespace.confcutdir = confcutdir
10171026
try:

_pytest/junitxml.py

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import sys
1515
import time
1616
import pytest
17+
from _pytest.config import filename
1718

1819
# Python 2.X and 3.X compatibility
1920
if sys.version_info[0] < 3:
@@ -214,6 +215,7 @@ def pytest_addoption(parser):
214215
action="store",
215216
dest="xmlpath",
216217
metavar="path",
218+
type=filename,
217219
default=None,
218220
help="create junit-xml style report file at given path.")
219221
group.addoption(

_pytest/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
except ImportError:
1212
from UserDict import DictMixin as MappingMixin
1313

14+
from _pytest.config import directory
1415
from _pytest.runner import collect_one_node
1516

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

testing/test_junitxml.py

+4
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,10 @@ def test_pass():
715715
assert result.ret == 0
716716
assert testdir.tmpdir.join("path/to/results.xml").check()
717717

718+
def test_logxml_check_isdir(testdir):
719+
"""Give an error if --junit-xml is a directory (#2089)"""
720+
result = testdir.runpytest("--junit-xml=.")
721+
result.stderr.fnmatch_lines(["*Must be a filename*"])
718722

719723
def test_escaped_parametrized_names_xml(testdir):
720724
testdir.makepyfile("""

0 commit comments

Comments
 (0)