diff options
Diffstat (limited to 'test/test_cmdline.py')
-rw-r--r-- | test/test_cmdline.py | 159 |
1 files changed, 148 insertions, 11 deletions
diff --git a/test/test_cmdline.py b/test/test_cmdline.py index 258a08ac..c530f890 100644 --- a/test/test_cmdline.py +++ b/test/test_cmdline.py @@ -1,8 +1,10 @@ """Test cmdline.py for coverage.""" -import os, re, shlex, sys, textwrap, unittest +import os, pprint, re, shlex, sys, textwrap, unittest import mock import coverage +import coverage.cmdline +from coverage.misc import ExceptionDuringRun sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from coveragetest import CoverageTest, OK, ERR @@ -14,7 +16,7 @@ class CmdLineTest(CoverageTest): run_in_temp_dir = False INIT_LOAD = """\ - .coverage(cover_pylib=None, data_suffix=False, timid=None, branch=None) + .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True) .load()\n""" def model_object(self): @@ -47,14 +49,24 @@ class CmdLineTest(CoverageTest): m2 = self.model_object() code_obj = compile(code, "<code>", "exec") eval(code_obj, globals(), { 'm2': m2 }) - self.assertEqual(m1.method_calls, m2.method_calls) + self.assert_same_method_calls(m1, m2) def cmd_executes_same(self, args1, args2): """Assert that the `args1` executes the same as `args2`.""" m1, r1 = self.mock_command_line(args1) m2, r2 = self.mock_command_line(args2) self.assertEqual(r1, r2) - self.assertEqual(m1.method_calls, m2.method_calls) + self.assert_same_method_calls(m1, m2) + + def assert_same_method_calls(self, m1, m2): + """Assert that `m1.method_calls` and `m2.method_calls` are the same.""" + # Use a real equality comparison, but if it fails, use a nicer assert + # so we can tell what's going on. We have to use the real == first due + # to CmdOptionParser.__eq__ + if m1.method_calls != m2.method_calls: + pp1 = pprint.pformat(m1.method_calls) + pp2 = pprint.pformat(m2.method_calls) + self.assertMultiLineEqual(pp1+'\n', pp2+'\n') def cmd_help(self, args, help_msg=None, topic=None, ret=ERR): """Run a command line, and check that it prints the right help. @@ -83,7 +95,7 @@ class ClassicCmdLineTest(CmdLineTest): def testErase(self): # coverage -e self.cmd_executes("-e", """\ - .coverage(cover_pylib=None, data_suffix=False, timid=None, branch=None) + .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True) .erase() """) self.cmd_executes_same("-e", "--erase") @@ -93,7 +105,7 @@ class ClassicCmdLineTest(CmdLineTest): # -x calls coverage.load first. self.cmd_executes("-x foo.py", """\ - .coverage(cover_pylib=None, data_suffix=False, timid=None, branch=None) + .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True) .load() .start() .run_python_file('foo.py', ['foo.py']) @@ -102,7 +114,7 @@ class ClassicCmdLineTest(CmdLineTest): """) # -e -x calls coverage.erase first. self.cmd_executes("-e -x foo.py", """\ - .coverage(cover_pylib=None, data_suffix=False, timid=None, branch=None) + .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True) .erase() .start() .run_python_file('foo.py', ['foo.py']) @@ -111,7 +123,7 @@ class ClassicCmdLineTest(CmdLineTest): """) # --timid sets a flag, and program arguments get passed through. self.cmd_executes("-x --timid foo.py abc 123", """\ - .coverage(cover_pylib=None, data_suffix=False, timid=True, branch=None) + .coverage(cover_pylib=None, data_suffix=None, timid=True, branch=None, config_file=True) .load() .start() .run_python_file('foo.py', ['foo.py', 'abc', '123']) @@ -120,7 +132,7 @@ class ClassicCmdLineTest(CmdLineTest): """) # -L sets a flag, and flags for the program don't confuse us. self.cmd_executes("-x -p -L foo.py -a -b", """\ - .coverage(cover_pylib=True, data_suffix=True, timid=None, branch=None) + .coverage(cover_pylib=True, data_suffix=True, timid=None, branch=None, config_file=True) .load() .start() .run_python_file('foo.py', ['foo.py', '-a', '-b']) @@ -137,7 +149,7 @@ class ClassicCmdLineTest(CmdLineTest): def testCombine(self): # coverage -c self.cmd_executes("-c", """\ - .coverage(cover_pylib=None, data_suffix=False, timid=None, branch=None) + .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True) .load() .combine() .save() @@ -325,6 +337,33 @@ class ClassicCmdLineTest(CmdLineTest): self.cmd_help("-z", "no such option: -z") +class FakeCoverageForDebugData(object): + """Just enough of a fake coverage package for the 'debug data' tests.""" + def __init__(self, summary): + self._summary = summary + self.filename = "FILENAME" + self.data = self + + # package members + def coverage(self, *unused_args, **unused_kwargs): + """The coverage class in the package.""" + return self + + # coverage methods + def load(self): + """Fake coverage().load()""" + pass + + # data methods + def has_arcs(self): + """Fake coverage().data.has_arcs()""" + return False + + def summary(self, fullpath): # pylint: disable-msg=W0613 + """Fake coverage().data.summary()""" + return self._summary + + class NewCmdLineTest(CmdLineTest): """Tests of the coverage.py command line.""" @@ -344,6 +383,31 @@ class NewCmdLineTest(CmdLineTest): self.cmd_help("debug", "What information would you like: data, sys?") self.cmd_help("debug foo", "Don't know what you mean by 'foo'") + def testDebugData(self): + fake = FakeCoverageForDebugData({ + 'file1.py': 17, 'file2.py': 23, + }) + self.command_line("debug data", _covpkg=fake) + self.assertMultiLineEqual(self.stdout(), textwrap.dedent("""\ + -- data --------------------------------------- + path: FILENAME + has_arcs: False + + 2 files: + file1.py: 17 lines + file2.py: 23 lines + """)) + + def testDebugDataWithNoData(self): + fake = FakeCoverageForDebugData({}) + self.command_line("debug data", _covpkg=fake) + self.assertMultiLineEqual(self.stdout(), textwrap.dedent("""\ + -- data --------------------------------------- + path: FILENAME + has_arcs: False + No data collected + """)) + def testDebugSys(self): self.command_line("debug sys") out = self.stdout() @@ -387,6 +451,22 @@ class NewCmdLineTest(CmdLineTest): self.cmd_executes_same("run -L f.py", "-e -x -L f.py") self.cmd_executes_same("run --timid f.py", "-e -x --timid f.py") self.cmd_executes_same("run", "-x") + self.cmd_executes("run --branch foo.py", """\ + .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=True, config_file=True) + .erase() + .start() + .run_python_file('foo.py', ['foo.py']) + .stop() + .save() + """) + self.cmd_executes("run --rcfile=myrc.rc foo.py", """\ + .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file="myrc.rc") + .erase() + .start() + .run_python_file('foo.py', ['foo.py']) + .stop() + .save() + """) def testXml(self): # coverage xml [-i] [--omit DIR,...] [FILE1 FILE2 ...] @@ -404,7 +484,7 @@ class NewCmdLineTest(CmdLineTest): """) self.cmd_executes("xml -o -", self.INIT_LOAD + """\ .xml_report(ignore_errors=None, omit_prefixes=None, morfs=[], - outfile=None) + outfile="-") """) self.cmd_executes("xml --omit fooey", self.INIT_LOAD + """\ .xml_report(ignore_errors=None, omit_prefixes=["fooey"], morfs=[], @@ -459,5 +539,62 @@ class CmdLineStdoutTest(CmdLineTest): assert "help" in out +class CmdMainTest(CoverageTest): + """Tests of coverage.cmdline.main(), using mocking for isolation.""" + + class CoverageScriptStub(object): + """A stub for coverage.cmdline.CoverageScript, used by CmdMainTest.""" + + def command_line(self, argv): + """Stub for command_line, the arg determines what it will do.""" + if argv[0] == 'hello': + print("Hello, world!") + elif argv[0] == 'raise': + try: + raise Exception("oh noes!") + except: + raise ExceptionDuringRun(*sys.exc_info()) + elif argv[0] == 'internalraise': + raise ValueError("coverage is broken") + elif argv[0] == 'exit': + sys.exit(23) + else: + raise AssertionError("Bad CoverageScriptStub: %r"% (argv,)) + return 0 + + def setUp(self): + super(CmdMainTest, self).setUp() + self.old_CoverageScript = coverage.cmdline.CoverageScript + coverage.cmdline.CoverageScript = self.CoverageScriptStub + + def tearDown(self): + coverage.cmdline.CoverageScript = self.old_CoverageScript + super(CmdMainTest, self).tearDown() + + def test_normal(self): + ret = coverage.cmdline.main(['hello']) + self.assertEqual(ret, 0) + self.assertEqual(self.stdout(), "Hello, world!\n") + + def test_raise(self): + ret = coverage.cmdline.main(['raise']) + self.assertEqual(ret, 1) + self.assertEqual(self.stdout(), "") + err = self.stderr().split('\n') + self.assertEqual(err[0], 'Traceback (most recent call last):') + self.assertEqual(err[-3], ' raise Exception("oh noes!")') + self.assertEqual(err[-2], 'Exception: oh noes!') + + def test_internalraise(self): + self.assertRaisesRegexp(ValueError, + "coverage is broken", + coverage.cmdline.main, ['internalraise'] + ) + + def test_exit(self): + ret = coverage.cmdline.main(['exit']) + self.assertEqual(ret, 23) + + if __name__ == '__main__': unittest.main() |