Skip to content

Commit

Permalink
Add a callback function so that -k can take a variable number of
Browse files Browse the repository at this point in the history
arguments.  Fixes #5228
  • Loading branch information
pbrady committed Sep 23, 2014
1 parent c0a8fbe commit f87e5d4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
30 changes: 28 additions & 2 deletions bin/test
Expand Up @@ -17,6 +17,32 @@ import re
from get_sympy import path_hack
path_hack()

# callback to support variable length argument in optparse
# docs.python.org/2/library/optparse.html#callback-example-6-variable-arguments
def vararg_callback(option, opt_str, value, parser):
assert value is None
value = []

def floatable(str):
try:
float(str)
return True
except ValueError:
return False

for arg in parser.rargs:
# stop on --foo like options
if arg[:2] == "--" and len(arg) > 2:
break
# stop on -a, but not on -3 or -3.0
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
break
value.append(arg)

del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value)


parser = OptionParser()
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
default=False)
Expand All @@ -27,8 +53,8 @@ parser.add_option("--no-colors", action="store_false", dest="colors",
parser.add_option("--force-colors", action="store_true", dest="force_colors",
default=False, help="Always use colors, even if the output is not to a terminal.")
parser.add_option("-k", dest="kw",
help="only run tests matching the given keyword expression",
metavar="KEYWORD", default="")
help="only run tests matching the given keyword expressions",
metavar="KEYWORDS", action="callback", callback=vararg_callback)
parser.add_option("--tb", dest="tb",
help="traceback verboseness (short/no) [default: %default]",
metavar="TBSTYLE", default="short")
Expand Down
12 changes: 9 additions & 3 deletions sympy/utilities/runtests.py
Expand Up @@ -452,7 +452,10 @@ def _test(*paths, **kwargs):
"""
verbose = kwargs.get("verbose", False)
tb = kwargs.get("tb", "short")
kw = kwargs.get("kw", "")
kw = kwargs.get("kw", [])
# ensure that kw is a tuple (list is fine too)
if isinstance(kw, str):
kw = (kw, )
post_mortem = kwargs.get("pdb", False)
colors = kwargs.get("colors", True)
force_colors = kwargs.get("force_colors", False)
Expand Down Expand Up @@ -1136,9 +1139,12 @@ def matches(self, x):
Always returns True if self._kw is "".
"""
if self._kw == "":
if not self._kw:
return True
return x.__name__.find(self._kw) != -1
for kw in self._kw:
if x.__name__.find(kw) != -1:
return True
return False

def get_test_files(self, dir, pat='test_*.py'):
"""
Expand Down

0 comments on commit f87e5d4

Please sign in to comment.