diff options
author | Ben Finney <ben@benfinney.id.au> | 2017-10-19 18:56:39 -0400 |
---|---|---|
committer | Ben Finney <ben@benfinney.id.au> | 2017-10-19 18:56:39 -0400 |
commit | 26c4ba8f7bad145a2db986a68441265939f053f7 (patch) | |
tree | 4aeec074d77277200e06085ea8f1f51e887016cf | |
parent | 00bf2f8272a1a61619314b3038318af89db86eea (diff) | |
download | python-coveragepy-git-26c4ba8f7bad145a2db986a68441265939f053f7.tar.gz |
Get the command name from the file path of the first command-line argument.
-rw-r--r-- | coverage/cmdline.py | 8 | ||||
-rw-r--r-- | tests/test_cmdline.py | 24 |
2 files changed, 29 insertions, 3 deletions
diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 63e4eb1f..841d8f4a 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -404,9 +404,11 @@ class CoverageScript(object): self.coverage = None - self.program_name = os.path.basename(sys.argv[0]) - if self.program_name == '__main__.py': - self.program_name = 'coverage' + program_path = sys.argv[0] + if program_path.endswith(os.path.sep + '__main__.py'): + # The path is the main module of a package; get that path instead. + program_path = os.path.dirname(program_path) + self.program_name = os.path.basename(program_path) if env.WINDOWS: # entry_points={'console_scripts':...} on Windows makes files # called coverage.exe, coverage3.exe, and coverage-3.5.exe. These diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 2378887a..d7b5d307 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -642,6 +642,30 @@ class CmdLineStdoutTest(BaseCmdLineTest): self.assertIn("without C extension", out) self.assertLess(out.count("\n"), 4) + def test_help_contains_command_name(self): + """ Command name should be present in help output. """ + fake_command_path = "lorem/ipsum/dolor" + expected_command_name = "dolor" + fake_argv = [fake_command_path, "sit", "amet"] + with mock.patch.object(sys, 'argv', new=fake_argv): + self.command_line("help") + self.assertIn(expected_command_name, out) + + def test_help_contains_command_name_from_package(self): + """ + Command package name should be present in help output. + + When the main module is actually a package's `__main__` module, the resulting command line + has the `__main__.py` file's patch as the command name. Instead, the command name should be + derived from the package name. + """ + fake_command_path = "lorem/ipsum/dolor/__main__.py" + expected_command_name = "dolor" + fake_argv = [fake_command_path, "sit", "amet"] + with mock.patch.object(sys, 'argv', new=fake_argv): + self.command_line("help") + self.assertIn(expected_command_name, out) + def test_help(self): self.command_line("help") out = self.stdout() |