diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_cmdline.py | 9 | ||||
-rw-r--r-- | tests/test_files.py | 62 | ||||
-rw-r--r-- | tests/test_process.py | 49 |
3 files changed, 116 insertions, 4 deletions
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 695c3bec..3032a2f1 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -33,6 +33,9 @@ class BaseCmdLineTest(CoverageTest): ignore_errors=None, include=None, omit=None, morfs=[], show_missing=None, ) + defaults.start( + main_module=None, + ) defaults.xml_report( ignore_errors=None, include=None, omit=None, morfs=[], outfile=None, ) @@ -472,7 +475,7 @@ class CmdLineTest(BaseCmdLineTest): self.cmd_executes("run -m mymodule", """\ .coverage() .erase() - .start() + .start(main_module='mymodule') .run_python_module('mymodule', ['mymodule']) .stop() .save() @@ -480,7 +483,7 @@ class CmdLineTest(BaseCmdLineTest): self.cmd_executes("run -m mymodule -qq arg1 arg2", """\ .coverage() .erase() - .start() + .start(main_module='mymodule') .run_python_module('mymodule', ['mymodule', '-qq', 'arg1', 'arg2']) .stop() .save() @@ -488,7 +491,7 @@ class CmdLineTest(BaseCmdLineTest): self.cmd_executes("run --branch -m mymodule", """\ .coverage(branch=True) .erase() - .start() + .start(main_module='mymodule') .run_python_module('mymodule', ['mymodule']) .stop() .save() diff --git a/tests/test_files.py b/tests/test_files.py index 648c76a9..d72788c4 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -2,7 +2,9 @@ import os, os.path -from coverage.files import FileLocator, TreeMatcher, FnmatchMatcher +from coverage.files import ( + FileLocator, TreeMatcher, FnmatchMatcher, ModuleMatcher +) from coverage.files import PathAliases, find_python_files, abs_file from coverage.misc import CoverageException @@ -80,6 +82,64 @@ class MatcherTest(CoverageTest): for filepath, matches in matches_to_try: self.assertMatches(tm, filepath, matches) + def test_module_matcher(self): + matches_to_try = [ + ('test', True), + ('test', True), + ('trash', False), + ('testing', False), + ('test.x', True), + ('test.x.y.z', True), + ('py', False), + ('py.t', False), + ('py.test', True), + ('py.testing', False), + ('py.test.buz', True), + ('py.test.buz.baz', True), + ('__main__', False), + ('mymain', True), + ('yourmain', False), + ] + modules = ['test', 'py.test', 'mymain'] + for mm in ( + ModuleMatcher(modules), + ModuleMatcher(modules, main_module=None), + ModuleMatcher(modules, main_module='yourmain'), + ): + self.assertEqual( + mm.info(), + ['main_module=%r' % mm.main_module] + modules + ) + for modulename, matches in matches_to_try: + self.assertEqual( + mm.match(modulename), + modulename if matches else False, + modulename, + ) + + def test_module_matcher_dunder_main(self): + matches_to_try = [ + ('__main__', True), + ('mymain', True), + ('yourmain', False), + ] + modules = ['test', 'py.test', 'mymain'] + mm = ModuleMatcher(modules, main_module='mymain') + self.assertEqual(mm.info(), ["main_module='mymain'"] + modules) + for modulename, matches in matches_to_try: + if not matches: + expected = False + elif modulename == '__main__': + expected = mm.main_module + else: + expected = modulename + + self.assertEqual( + mm.match(modulename), + expected, + modulename, + ) + def test_fnmatch_matcher(self): matches_to_try = [ (self.make_file("sub/file1.py"), True), diff --git a/tests/test_process.py b/tests/test_process.py index ac5c6e1b..e53e64c9 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -331,6 +331,55 @@ class ProcessTest(CoverageTest): out_py = self.run_command("python -m tests.try_execfile") self.assertMultiLineEqual(out_cov, out_py) + def test_coverage_run_dashm_equal_to_doubledashsource(self): + """regression test for #328 + + When imported by -m, a module's __name__ is __main__, but we need the + --source machinery to know and respect the original name. + """ + # These -m commands assume the coverage tree is on the path. + out_cov = self.run_command( + "coverage run --source tests.try_execfile -m tests.try_execfile" + ) + out_py = self.run_command("python -m tests.try_execfile") + self.assertMultiLineEqual(out_cov, out_py) + + def test_coverage_run_dashm_superset_of_doubledashsource(self): + """Edge case: --source foo -m foo.bar""" + # These -m commands assume the coverage tree is on the path. + out_cov = self.run_command( + "coverage run --source tests -m tests.try_execfile" + ) + out_py = self.run_command("python -m tests.try_execfile") + self.assertMultiLineEqual(out_cov, out_py) + + st, out = self.run_command_status("coverage report") + self.assertEqual(st, 0) + self.assertEqual(self.line_count(out), 6, out) + + def test_coverage_run_script_imports_doubledashsource(self): + self.make_file("myscript", """\ + import sys + sys.dont_write_bytecode = True + + def main(): + import tests.try_execfile + + if __name__ == '__main__': + main() + """) + + # These -m commands assume the coverage tree is on the path. + out_cov = self.run_command( + "coverage run --source tests myscript" + ) + out_py = self.run_command("python myscript") + self.assertMultiLineEqual(out_cov, out_py) + + st, out = self.run_command_status("coverage report") + self.assertEqual(st, 0) + self.assertEqual(self.line_count(out), 6, out) + def test_coverage_run_dashm_is_like_python_dashm_off_path(self): # https://bitbucket.org/ned/coveragepy/issue/242 tryfile = os.path.join(here, "try_execfile.py") |