summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2014-11-24 21:30:04 -0500
committerNed Batchelder <ned@nedbatchelder.com>2014-11-24 21:30:04 -0500
commit2b369aa719d2a4b4e755c9030f1d0cc1dfeeeacb (patch)
tree361d0a995b5360a0170aff07cc87aa5afbc7562a /tests
parent8fa9db9f86de0b7cbce45e0a5fe87e38e47212b7 (diff)
parentb3ccb75241566c1e1a814ae99a84637fd0ac2b44 (diff)
downloadpython-coveragepy-git-2b369aa719d2a4b4e755c9030f1d0cc1dfeeeacb.tar.gz
Merged pull request 42, fixing issue #328.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_files.py35
-rw-r--r--tests/test_process.py151
-rw-r--r--tests/try_execfile.py5
3 files changed, 185 insertions, 6 deletions
diff --git a/tests/test_files.py b/tests/test_files.py
index 648c76a9..f6976a81 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,37 @@ 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']
+ mm = ModuleMatcher(modules)
+ self.assertEqual(
+ mm.info(),
+ modules
+ )
+ for modulename, matches in matches_to_try:
+ self.assertEqual(
+ mm.match(modulename),
+ matches,
+ 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 4584e7d7..09d4c207 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")
@@ -613,11 +662,10 @@ class FailUnderTest(CoverageTest):
self.assertEqual(st, 2)
-class ProcessStartupTest(CoverageTest):
- """Test that we can measure coverage in subprocesses."""
-
+class ProcessCoverageMixin(object):
+ """Set up a .pth file that causes all subprocesses to be coverage'd"""
def setUp(self):
- super(ProcessStartupTest, self).setUp()
+ super(ProcessCoverageMixin, self).setUp()
# Find a place to put a .pth file.
pth_contents = "import coverage; coverage.process_startup()\n"
for d in sys.path: # pragma: part covered
@@ -635,10 +683,14 @@ class ProcessStartupTest(CoverageTest):
raise Exception("Couldn't find a place for the .pth file")
def tearDown(self):
- super(ProcessStartupTest, self).tearDown()
+ super(ProcessCoverageMixin, self).tearDown()
# Clean up the .pth file we made.
os.remove(self.pth_path)
+
+class ProcessStartupTest(ProcessCoverageMixin, CoverageTest):
+ """Test that we can measure coverage in subprocesses."""
+
def test_subprocess_with_pth_files(self): # pragma: not covered
if os.environ.get('COVERAGE_COVERAGE', ''):
raise SkipTest(
@@ -671,3 +723,92 @@ class ProcessStartupTest(CoverageTest):
data = coverage.CoverageData()
data.read_file(".mycovdata")
self.assertEqual(data.summary()['sub.py'], 2)
+
+
+class ProcessStartupWithSourceTest(ProcessCoverageMixin, CoverageTest):
+ """Show that we can configure {[run]source} during process-level coverage.
+
+ There are two interesting variables:
+ 1) -m versus a simple script argument (eg `python myscript`)
+ 2) filtering for the top-level (main.py) or second-level (sub.py) module
+ 3) whether the files are in a package or not
+
+ ... for a total of eight tests.
+ """
+ def assert_pth_and_source_work_together(self, dashm, package, source):
+ def fullname(modname):
+ if package and dashm:
+ return '.'.join((package, modname))
+ else:
+ return modname
+
+ def path(basename):
+ return os.path.join(package, basename)
+
+ if os.environ.get('COVERAGE_COVERAGE', ''):
+ raise SkipTest(
+ "Can't test subprocess pth file suppport during metacoverage"
+ )
+ # Main will run sub.py
+ self.make_file(path("main.py"), """\
+ import %s
+ if True: pass
+ """ % fullname('sub'))
+ if package:
+ self.make_file(path("__init__.py"), '')
+ # sub.py will write a few lines.
+ self.make_file(path("sub.py"), """\
+ with open("out.txt", "w") as f:
+ f.write("Hello, world!\\n")
+ """)
+ self.make_file("coverage.ini", """\
+ [run]
+ source = %s
+ """ % fullname(source)
+ )
+
+ self.set_environ("COVERAGE_PROCESS_START", "coverage.ini")
+
+ if dashm:
+ cmd = (sys.executable, dashm, fullname('main'))
+ else:
+ cmd = (sys.executable, path('main.py'))
+
+ from subprocess import Popen
+ Popen(cmd).wait()
+
+ with open("out.txt") as f:
+ self.assertEqual(f.read(), "Hello, world!\n")
+
+ # Read the data from .coverage
+ self.assert_exists(".coverage")
+ data = coverage.CoverageData()
+ data.read_file(".coverage")
+ summary = data.summary()
+ print(summary)
+ self.assertEqual(summary[source + '.py'], 2)
+ self.assertEqual(len(summary), 1)
+
+ def test_dashm_main(self):
+ self.assert_pth_and_source_work_together('-m', '', 'main')
+
+ def test_script_main(self):
+ self.assert_pth_and_source_work_together('', '', 'main')
+
+ def test_dashm_sub(self):
+ self.assert_pth_and_source_work_together('-m', '', 'sub')
+
+ def test_script_sub(self):
+ self.assert_pth_and_source_work_together('', '', 'sub')
+
+ def test_dashm_pkg_main(self):
+ self.assert_pth_and_source_work_together('-m', 'pkg', 'main')
+
+ def test_script_pkg_main(self):
+ self.assert_pth_and_source_work_together('', 'pkg', 'main')
+
+ def test_dashm_pkg_sub(self):
+ self.assert_pth_and_source_work_together('-m', 'pkg', 'sub')
+
+ def test_script_pkg_sub(self):
+ self.assert_pth_and_source_work_together('', 'pkg', 'sub')
diff --git a/tests/try_execfile.py b/tests/try_execfile.py
index 314ffae0..ee5bae5c 100644
--- a/tests/try_execfile.py
+++ b/tests/try_execfile.py
@@ -54,12 +54,17 @@ def my_function(a):
FN_VAL = my_function("fooey")
+loader = globals().get('__loader__')
+fullname = getattr(loader, 'fullname', None) or getattr(loader, 'name', None)
+
globals_to_check = {
'__name__': __name__,
'__file__': __file__,
'__doc__': __doc__,
'__builtins__.has_open': hasattr(__builtins__, 'open'),
'__builtins__.dir': dir(__builtins__),
+ '__loader__ exists': loader is not None,
+ '__loader__.fullname': fullname,
'__package__': __package__,
'DATA': DATA,
'FN_VAL': FN_VAL,