diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-11-14 17:04:14 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-11-14 17:04:14 -0500 |
commit | 862e52de7a7609f672077d73df5673fee6c79acc (patch) | |
tree | e3adc5cbaa715e0719a38b31e842ec1c0736cc88 | |
parent | 7dd5319ef31ed33b153275e72f517190ab2afcff (diff) | |
download | python-coveragepy-git-862e52de7a7609f672077d73df5673fee6c79acc.tar.gz |
Cleanups for style, docs, etc from #438
-rw-r--r-- | CHANGES.rst | 5 | ||||
-rw-r--r-- | coverage/backward.py | 9 | ||||
-rw-r--r-- | coverage/cmdline.py | 13 | ||||
-rw-r--r-- | tests/coveragetest.py | 59 |
4 files changed, 42 insertions, 44 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index a599352a..51d1635d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,7 +12,12 @@ Version 4.0.2, in progress - Fixed an unusual edge case of detecting source encodings, described in `issue 443`_. +- Help messages that mention the command to use now properly use the actual + command name, which might be different than "coverage". Thanks to Ben Finney, + this closes `issue 438`_. + .. _issue 443: https://bitbucket.org/ned/coveragepy/issues/443/coverage-gets-confused-when-encoding +.. _issue 438: https://bitbucket.org/ned/coveragepy/issues/438/parameterise-coverage-command-name Version 4.0.2 --- 4 November 2015 diff --git a/coverage/backward.py b/coverage/backward.py index 7f571c07..81ca342f 100644 --- a/coverage/backward.py +++ b/coverage/backward.py @@ -49,6 +49,15 @@ try: except NameError: range = range +# shlex.quote is new, but there's an undocumented implementation in "pipes", +# who knew!? +try: + from shlex import quote as shlex_quote +except ImportError: + # Useful function, available under a different (undocumented) name + # in Python versions earlier than 3.3. + from pipes import quote as shlex_quote + # A function to iterate listlessly over a dict's items. try: {}.iteritems diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 97ea596c..5742e6a9 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -242,12 +242,11 @@ class CmdOptionParser(CoverageOptionParser): return (other == "<CmdOptionParser:%s>" % self.cmd) def get_prog_name(self): + """Override of an undocumented function in optparse.OptionParser.""" program_name = super(CmdOptionParser, self).get_prog_name() # Include the sub-command for this parser as part of the command. - result = "%(command)s %(subcommand)s" % { - 'command': program_name, 'subcommand': self.cmd} - return result + return "%(command)s %(subcommand)s" % {'command': program_name, 'subcommand': self.cmd} GLOBAL_ARGS = [ @@ -533,14 +532,12 @@ class CoverageScript(object): assert error or topic or parser if error: print(error) - print("Use '%(program_name)s help' for help." % { - 'program_name': self.program_name}) + print("Use '%s help' for help." % (self.program_name,)) elif parser: print(parser.format_help().strip()) else: - help_params = self.covpkg.__dict__ - help_params.update({ - 'program_name': self.program_name}) + help_params = dict(self.covpkg.__dict__) + help_params['program_name'] = self.program_name help_msg = textwrap.dedent(HELP_TOPICS.get(topic, '')).strip() if help_msg: print(help_msg % help_params) diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 8b54c819..1c0dd681 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -10,20 +10,12 @@ import os import random import re import shlex -try: - shlex.quote -except AttributeError: - # Useful function, available under a different (undocumented) name - # in Python versions earlier than 3.3. - import pipes - shlex.quote = pipes.quote - del pipes import shutil import sys import coverage from coverage.backunittest import TestCase -from coverage.backward import StringIO, import_local_file, string_class +from coverage.backward import StringIO, import_local_file, string_class, shlex_quote from coverage.cmdline import CoverageScript from coverage.debug import _TEST_NAME_FILE, DebugControl from coverage.test_helpers import ( @@ -343,48 +335,43 @@ class CoverageTest( coverage_command = "coverage" def run_command(self, cmd): - """ Run the command-line `cmd` in a sub-process. + """Run the command-line `cmd` in a sub-process. - :param cmd: The command line to invoke in a sub-process. - :return: Combined content of `stdout` and `stderr` output - streams from the sub-process. + `cmd` is the command line to invoke in a sub-process. Returns the + combined content of `stdout` and `stderr` output streams from the + sub-process. - Use this when you need to test the process behavior of - coverage. + Use this when you need to test the process behavior of coverage. - Compare with `command_line`. + Compare with `command_line`. - Handles the following command name specially: + Handles the following command name specially: - * "python" is replaced with the command name of the current - Python interpreter. + * "python" is replaced with the command name of the current + Python interpreter. - * "coverage" is replaced with the command name for the main - Coverage.py program. + * "coverage" is replaced with the command name for the main + Coverage.py program. - """ + """ split_commandline = cmd.split(" ", 1) command_name = split_commandline[0] command_args = split_commandline[1:] if command_name == "python": - # Running a Python interpreter in a sub-processes can be - # tricky. Use the real name of our own executable. So - # "python foo.py" might get executed as "python3.3 - # foo.py". This is important because Python 3.x doesn't - # install as "python", so you might get a Python 2 - # executable instead if you don't use the executable's - # basename. + # Running a Python interpreter in a sub-processes can be tricky. + # Use the real name of our own executable. So "python foo.py" might + # get executed as "python3.3 foo.py". This is important because + # Python 3.x doesn't install as "python", so you might get a Python + # 2 executable instead if you don't use the executable's basename. command_name = os.path.basename(sys.executable) - + if command_name == "coverage": - # The invocation requests the Coverage.py program. Test - # whether that's actually the command name to use. - if command_name != self.coverage_command: - # Substitute the actual Coverage.py main command name. - command_name = self.coverage_command + # The invocation requests the Coverage.py program. Substitute the + # actual Coverage.py main command name. + command_name = self.coverage_command - full_commandline = " ".join([shlex.quote(command_name)] + command_args) + full_commandline = " ".join([shlex_quote(command_name)] + command_args) _, output = self.run_command_status(full_commandline) return output |