diff options
Diffstat (limited to 'igor.py')
-rw-r--r-- | igor.py | 69 |
1 files changed, 53 insertions, 16 deletions
@@ -1,4 +1,7 @@ # coding: utf-8 +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt + """Helper for building, testing, and linting coverage.py. To get portability, all these operations are written in Python here instead @@ -6,6 +9,7 @@ of in shell scripts, batch files, or Makefiles. """ +import contextlib import fnmatch import glob import inspect @@ -18,8 +22,17 @@ import warnings import zipfile +# We want to see all warnings while we are running tests. But we also need to +# disable warnings for some of the more complex setting up of tests. warnings.simplefilter("default") +@contextlib.contextmanager +def ignore_warnings(): + """Context manager to ignore warning within the with statement.""" + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + yield + # Functions named do_* are executable from the command line: do_blah is run # by "python igor.py blah". @@ -32,6 +45,7 @@ def do_remove_extension(): tracer.so tracer.*.so tracer.pyd + tracer.*.pyd """.split() for pattern in so_patterns: @@ -45,13 +59,18 @@ def do_remove_extension(): def run_tests(tracer, *nose_args): """The actual running of tests.""" - import nose.core + with ignore_warnings(): + import nose.core + if tracer == "py": label = "with Python tracer" skipper = os.environ.get("COVERAGE_NO_PYTRACER") else: label = "with C tracer" - skipper = os.environ.get("COVERAGE_NO_EXTENSION") + skipper = ( + os.environ.get("COVERAGE_NO_EXTENSION") or + os.environ.get("COVERAGE_NO_CTRACER") + ) if skipper: msg = "Skipping tests " + label @@ -60,6 +79,7 @@ def run_tests(tracer, *nose_args): print(msg) return + os.environ['COVERAGE_TESTING'] = "True" print_banner(label) nose_args = ["nosetests"] + list(nose_args) nose.core.main(argv=nose_args) @@ -67,6 +87,9 @@ def run_tests(tracer, *nose_args): def run_tests_with_coverage(tracer, *nose_args): """Run tests, but with coverage.""" + # Need to define this early enough that the first import of env.py sees it. + os.environ['COVERAGE_TESTING'] = "True" + import coverage os.environ['COVERAGE_PROCESS_START'] = os.path.abspath('metacov.ini') @@ -84,10 +107,10 @@ def run_tests_with_coverage(tracer, *nose_args): version = "%s%s" % sys.version_info[:2] suffix = "%s_%s_%s" % (version, tracer, socket.gethostname()) - cov = coverage.coverage(config_file="metacov.ini", data_suffix=suffix) - # Cheap trick: the coverage code itself is excluded from measurement, but - # if we clobber the cover_prefix in the coverage object, we can defeat the - # self-detection. + cov = coverage.Coverage(config_file="metacov.ini", data_suffix=suffix) + # Cheap trick: the coverage.py code itself is excluded from measurement, + # but if we clobber the cover_prefix in the coverage object, we can defeat + # the self-detection. cov.cover_prefix = "Please measure coverage.py!" cov._warn_unimported_source = False cov.erase() @@ -126,7 +149,7 @@ def do_combine_html(): """Combine data from a meta-coverage run, and make the HTML report.""" import coverage os.environ['COVERAGE_HOME'] = os.getcwd() - cov = coverage.coverage(config_file="metacov.ini") + cov = coverage.Coverage(config_file="metacov.ini") cov.load() cov.combine() cov.save() @@ -176,14 +199,15 @@ def do_install_egg(): """Install the egg1 egg for tests.""" # I am pretty certain there are easier ways to install eggs... # pylint: disable=import-error,no-name-in-module - import distutils.core cur_dir = os.getcwd() os.chdir("tests/eggsrc") - distutils.core.run_setup("setup.py", ["--quiet", "bdist_egg"]) - egg = glob.glob("dist/*.egg")[0] - distutils.core.run_setup( - "setup.py", ["--quiet", "easy_install", "--no-deps", "--zip-ok", egg] - ) + with ignore_warnings(): + import distutils.core + distutils.core.run_setup("setup.py", ["--quiet", "bdist_egg"]) + egg = glob.glob("dist/*.egg")[0] + distutils.core.run_setup( + "setup.py", ["--quiet", "easy_install", "--no-deps", "--zip-ok", egg] + ) os.chdir(cur_dir) @@ -281,6 +305,19 @@ def do_help(): print("%-20s%s" % (name[3:], value.__doc__)) +def analyze_args(function): + """What kind of args does `function` expect? + + Returns: + star, num_pos: + star(boolean): Does `function` accept *args? + num_args(int): How many positional arguments does `function` have? + """ + with ignore_warnings(): + argspec = inspect.getargspec(function) + return bool(argspec[1]), len(argspec[0]) + + def main(args): """Main command-line execution for igor. @@ -294,14 +331,13 @@ def main(args): if handler is None: print("*** No handler for %r" % verb) return 1 - argspec = inspect.getargspec(handler) - if argspec[1]: + star, num_args = analyze_args(handler) + if star: # Handler has *args, give it all the rest of the command line. handler_args = args args = [] else: # Handler has specific arguments, give it only what it needs. - num_args = len(argspec[0]) handler_args = args[:num_args] args = args[num_args:] ret = handler(*handler_args) @@ -309,5 +345,6 @@ def main(args): if ret: return ret + if __name__ == '__main__': sys.exit(main(sys.argv[1:])) |