Skip to content

Commit

Permalink
fixup! Add type validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
decentral1se committed Nov 28, 2016
1 parent 733d719 commit 6cc6894
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
3.0.5.dev0
==========

* Add Argparse style type validation for ``--confcutdir`` and ``--junit-xml`` (`#2089`_).
Thanks to `@lwm`_ for the PR.

* Add hint to error message hinting possible missing ``__init__.py`` (`#478`_). Thanks `@DuncanBetts`_.

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

.. _#2089: https://github.com/pytest-dev/pytest/issues/2089
.. _#478: https://github.com/pytest-dev/pytest/issues/478
.. _#2034: https://github.com/pytest-dev/pytest/issues/2034
.. _#2038: https://github.com/pytest-dev/pytest/issues/2038
Expand Down
21 changes: 15 additions & 6 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,25 @@ class UsageError(Exception):
""" error in pytest usage or invocation"""


def filename(path):
"""Argparse type validator for filename arguments"""
def filename_arg(path, optname):
""" Argparse type validator for filename arguments.
:path: path of filename
:optname: name of the option
"""
if os.path.isdir(path):
raise UsageError("Must be a filename, given: {0}".format(path))
raise UsageError("{0} must be a filename, given: {1}".format(optname, path))
return path

def directory(path):
"""Argparse type validator for directory arguments"""

def directory_arg(path, optname):
"""Argparse type validator for directory arguments.
:path: path of directory
:optname: name of the option
"""
if not os.path.isdir(path):
raise UsageError("Must be a directory, given: {0}".format(path))
raise UsageError("{0} must be a directory, given: {1}".format(optname, path))
return path

_preinit = []
Expand Down
5 changes: 3 additions & 2 deletions _pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
# Output conforms to https://github.com/jenkinsci/xunit-plugin/blob/master/
# src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd

import functools
import py
import os
import re
import sys
import time
import pytest
from _pytest.config import filename
from _pytest.config import filename_arg

# Python 2.X and 3.X compatibility
if sys.version_info[0] < 3:
Expand Down Expand Up @@ -215,7 +216,7 @@ def pytest_addoption(parser):
action="store",
dest="xmlpath",
metavar="path",
type=filename,
type=functools.partial(filename_arg, optname="--junitxml"),
default=None,
help="create junit-xml style report file at given path.")
group.addoption(
Expand Down
5 changes: 3 additions & 2 deletions _pytest/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" core implementation of testing process: init, session, runtest loop. """
import functools
import os
import sys

Expand All @@ -11,7 +12,7 @@
except ImportError:
from UserDict import DictMixin as MappingMixin

from _pytest.config import directory
from _pytest.config import directory_arg
from _pytest.runner import collect_one_node

tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
Expand Down Expand Up @@ -59,7 +60,7 @@ def pytest_addoption(parser):
# when changing this to --conf-cut-dir, config.py Conftest.setinitial
# needs upgrading as well
group.addoption('--confcutdir', dest="confcutdir", default=None,
metavar="dir", type=directory,
metavar="dir", type=functools.partial(directory_arg, optname="--confcutdir"),
help="only load conftest.py's relative to specified dir.")
group.addoption('--noconftest', action="store_true",
dest="noconftest", default=False,
Expand Down
2 changes: 1 addition & 1 deletion testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def test_pass():
def test_logxml_check_isdir(testdir):
"""Give an error if --junit-xml is a directory (#2089)"""
result = testdir.runpytest("--junit-xml=.")
result.stderr.fnmatch_lines(["*Must be a filename*"])
result.stderr.fnmatch_lines(["*--junitxml must be a filename*"])

def test_escaped_parametrized_names_xml(testdir):
testdir.makepyfile("""
Expand Down

0 comments on commit 6cc6894

Please sign in to comment.