summaryrefslogtreecommitdiff
path: root/tests/test_process.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2013-02-02 11:15:11 -0500
committerNed Batchelder <ned@nedbatchelder.com>2013-02-02 11:15:11 -0500
commitc19232a9d2be2bde30e446311f32e9dcdd5d4774 (patch)
treec2a9afb49cb74d9e041058c0275e1896dc0ecd0c /tests/test_process.py
parentc359cea1408c410a4c770debeddf3aed0abd43fe (diff)
downloadpython-coveragepy-git-c19232a9d2be2bde30e446311f32e9dcdd5d4774.tar.gz
Move the test directory to tests to avoid conflicts with the stdlib test package.
--HG-- rename : test/__init__.py => tests/__init__.py rename : test/backtest.py => tests/backtest.py rename : test/backunittest.py => tests/backunittest.py rename : test/coveragetest.py => tests/coveragetest.py rename : test/covmodzip1.py => tests/covmodzip1.py rename : test/eggsrc/egg1/__init__.py => tests/eggsrc/egg1/__init__.py rename : test/eggsrc/egg1/egg1.py => tests/eggsrc/egg1/egg1.py rename : test/eggsrc/setup.py => tests/eggsrc/setup.py rename : test/farm/annotate/annotate_dir.py => tests/farm/annotate/annotate_dir.py rename : test/farm/annotate/gold/white.py,cover => tests/farm/annotate/gold/white.py,cover rename : test/farm/annotate/gold_anno_dir/a___init__.py,cover => tests/farm/annotate/gold_anno_dir/a___init__.py,cover rename : test/farm/annotate/gold_anno_dir/a_a.py,cover => tests/farm/annotate/gold_anno_dir/a_a.py,cover rename : test/farm/annotate/gold_anno_dir/b___init__.py,cover => tests/farm/annotate/gold_anno_dir/b___init__.py,cover rename : test/farm/annotate/gold_anno_dir/b_b.py,cover => tests/farm/annotate/gold_anno_dir/b_b.py,cover rename : test/farm/annotate/gold_anno_dir/multi.py,cover => tests/farm/annotate/gold_anno_dir/multi.py,cover rename : test/farm/annotate/gold_multi/a/__init__.py,cover => tests/farm/annotate/gold_multi/a/__init__.py,cover rename : test/farm/annotate/gold_multi/a/a.py,cover => tests/farm/annotate/gold_multi/a/a.py,cover rename : test/farm/annotate/gold_multi/b/__init__.py,cover => tests/farm/annotate/gold_multi/b/__init__.py,cover rename : test/farm/annotate/gold_multi/b/b.py,cover => tests/farm/annotate/gold_multi/b/b.py,cover rename : test/farm/annotate/gold_multi/multi.py,cover => tests/farm/annotate/gold_multi/multi.py,cover rename : test/farm/annotate/gold_v24/white.py,cover => tests/farm/annotate/gold_v24/white.py,cover rename : test/farm/annotate/run.py => tests/farm/annotate/run.py rename : test/farm/annotate/run_multi.py => tests/farm/annotate/run_multi.py rename : test/farm/annotate/src/a/__init__.py => tests/farm/annotate/src/a/__init__.py rename : test/farm/annotate/src/a/a.py => tests/farm/annotate/src/a/a.py rename : test/farm/annotate/src/b/__init__.py => tests/farm/annotate/src/b/__init__.py rename : test/farm/annotate/src/b/b.py => tests/farm/annotate/src/b/b.py rename : test/farm/annotate/src/multi.py => tests/farm/annotate/src/multi.py rename : test/farm/annotate/src/white.py => tests/farm/annotate/src/white.py rename : test/farm/html/gold_a/a.html => tests/farm/html/gold_a/a.html rename : test/farm/html/gold_a/index.html => tests/farm/html/gold_a/index.html rename : test/farm/html/gold_b_branch/b.html => tests/farm/html/gold_b_branch/b.html rename : test/farm/html/gold_b_branch/index.html => tests/farm/html/gold_b_branch/index.html rename : test/farm/html/gold_bom/bom.html => tests/farm/html/gold_bom/bom.html rename : test/farm/html/gold_bom/index.html => tests/farm/html/gold_bom/index.html rename : test/farm/html/gold_isolatin1/index.html => tests/farm/html/gold_isolatin1/index.html rename : test/farm/html/gold_isolatin1/isolatin1.html => tests/farm/html/gold_isolatin1/isolatin1.html rename : test/farm/html/gold_omit_1/index.html => tests/farm/html/gold_omit_1/index.html rename : test/farm/html/gold_omit_1/m1.html => tests/farm/html/gold_omit_1/m1.html rename : test/farm/html/gold_omit_1/m2.html => tests/farm/html/gold_omit_1/m2.html rename : test/farm/html/gold_omit_1/m3.html => tests/farm/html/gold_omit_1/m3.html rename : test/farm/html/gold_omit_1/main.html => tests/farm/html/gold_omit_1/main.html rename : test/farm/html/gold_omit_2/index.html => tests/farm/html/gold_omit_2/index.html rename : test/farm/html/gold_omit_2/m2.html => tests/farm/html/gold_omit_2/m2.html rename : test/farm/html/gold_omit_2/m3.html => tests/farm/html/gold_omit_2/m3.html rename : test/farm/html/gold_omit_2/main.html => tests/farm/html/gold_omit_2/main.html rename : test/farm/html/gold_omit_3/index.html => tests/farm/html/gold_omit_3/index.html rename : test/farm/html/gold_omit_3/m3.html => tests/farm/html/gold_omit_3/m3.html rename : test/farm/html/gold_omit_3/main.html => tests/farm/html/gold_omit_3/main.html rename : test/farm/html/gold_omit_4/index.html => tests/farm/html/gold_omit_4/index.html rename : test/farm/html/gold_omit_4/m1.html => tests/farm/html/gold_omit_4/m1.html rename : test/farm/html/gold_omit_4/m3.html => tests/farm/html/gold_omit_4/m3.html rename : test/farm/html/gold_omit_4/main.html => tests/farm/html/gold_omit_4/main.html rename : test/farm/html/gold_omit_5/index.html => tests/farm/html/gold_omit_5/index.html rename : test/farm/html/gold_omit_5/m1.html => tests/farm/html/gold_omit_5/m1.html rename : test/farm/html/gold_omit_5/main.html => tests/farm/html/gold_omit_5/main.html rename : test/farm/html/gold_other/blah_blah_other.html => tests/farm/html/gold_other/blah_blah_other.html rename : test/farm/html/gold_other/here.html => tests/farm/html/gold_other/here.html rename : test/farm/html/gold_other/index.html => tests/farm/html/gold_other/index.html rename : test/farm/html/gold_partial/index.html => tests/farm/html/gold_partial/index.html rename : test/farm/html/gold_partial/partial.html => tests/farm/html/gold_partial/partial.html rename : test/farm/html/gold_styled/a.html => tests/farm/html/gold_styled/a.html rename : test/farm/html/gold_styled/extra.css => tests/farm/html/gold_styled/extra.css rename : test/farm/html/gold_styled/index.html => tests/farm/html/gold_styled/index.html rename : test/farm/html/gold_styled/style.css => tests/farm/html/gold_styled/style.css rename : test/farm/html/gold_unicode/index.html => tests/farm/html/gold_unicode/index.html rename : test/farm/html/gold_unicode/unicode.html => tests/farm/html/gold_unicode/unicode.html rename : test/farm/html/gold_x_xml/coverage.xml => tests/farm/html/gold_x_xml/coverage.xml rename : test/farm/html/gold_y_xml_branch/coverage.xml => tests/farm/html/gold_y_xml_branch/coverage.xml rename : test/farm/html/othersrc/other.py => tests/farm/html/othersrc/other.py rename : test/farm/html/run_a.py => tests/farm/html/run_a.py rename : test/farm/html/run_a_xml_1.py => tests/farm/html/run_a_xml_1.py rename : test/farm/html/run_a_xml_2.py => tests/farm/html/run_a_xml_2.py rename : test/farm/html/run_b_branch.py => tests/farm/html/run_b_branch.py rename : test/farm/html/run_bom.py => tests/farm/html/run_bom.py rename : test/farm/html/run_isolatin1.py => tests/farm/html/run_isolatin1.py rename : test/farm/html/run_omit_1.py => tests/farm/html/run_omit_1.py rename : test/farm/html/run_omit_2.py => tests/farm/html/run_omit_2.py rename : test/farm/html/run_omit_3.py => tests/farm/html/run_omit_3.py rename : test/farm/html/run_omit_4.py => tests/farm/html/run_omit_4.py rename : test/farm/html/run_omit_5.py => tests/farm/html/run_omit_5.py rename : test/farm/html/run_other.py => tests/farm/html/run_other.py rename : test/farm/html/run_partial.py => tests/farm/html/run_partial.py rename : test/farm/html/run_styled.py => tests/farm/html/run_styled.py rename : test/farm/html/run_tabbed.py => tests/farm/html/run_tabbed.py rename : test/farm/html/run_unicode.py => tests/farm/html/run_unicode.py rename : test/farm/html/run_y_xml_branch.py => tests/farm/html/run_y_xml_branch.py rename : test/farm/html/src/a.py => tests/farm/html/src/a.py rename : test/farm/html/src/b.py => tests/farm/html/src/b.py rename : test/farm/html/src/bom.py => tests/farm/html/src/bom.py rename : test/farm/html/src/coverage.xml => tests/farm/html/src/coverage.xml rename : test/farm/html/src/extra.css => tests/farm/html/src/extra.css rename : test/farm/html/src/here.py => tests/farm/html/src/here.py rename : test/farm/html/src/isolatin1.py => tests/farm/html/src/isolatin1.py rename : test/farm/html/src/m1.py => tests/farm/html/src/m1.py rename : test/farm/html/src/m2.py => tests/farm/html/src/m2.py rename : test/farm/html/src/m3.py => tests/farm/html/src/m3.py rename : test/farm/html/src/main.py => tests/farm/html/src/main.py rename : test/farm/html/src/omit4.ini => tests/farm/html/src/omit4.ini rename : test/farm/html/src/omit5.ini => tests/farm/html/src/omit5.ini rename : test/farm/html/src/partial.py => tests/farm/html/src/partial.py rename : test/farm/html/src/run_a_xml_2.ini => tests/farm/html/src/run_a_xml_2.ini rename : test/farm/html/src/tabbed.py => tests/farm/html/src/tabbed.py rename : test/farm/html/src/unicode.py => tests/farm/html/src/unicode.py rename : test/farm/html/src/y.py => tests/farm/html/src/y.py rename : test/farm/run/run_chdir.py => tests/farm/run/run_chdir.py rename : test/farm/run/run_timid.py => tests/farm/run/run_timid.py rename : test/farm/run/run_xxx.py => tests/farm/run/run_xxx.py rename : test/farm/run/src/chdir.py => tests/farm/run/src/chdir.py rename : test/farm/run/src/showtrace.py => tests/farm/run/src/showtrace.py rename : test/farm/run/src/subdir/placeholder => tests/farm/run/src/subdir/placeholder rename : test/farm/run/src/xxx => tests/farm/run/src/xxx rename : test/js/index.html => tests/js/index.html rename : test/js/tests.js => tests/js/tests.js rename : test/modules/aa/__init__.py => tests/modules/aa/__init__.py rename : test/modules/aa/afile.odd.py => tests/modules/aa/afile.odd.py rename : test/modules/aa/afile.py => tests/modules/aa/afile.py rename : test/modules/aa/bb.odd/bfile.py => tests/modules/aa/bb.odd/bfile.py rename : test/modules/aa/bb/__init__.py => tests/modules/aa/bb/__init__.py rename : test/modules/aa/bb/bfile.odd.py => tests/modules/aa/bb/bfile.odd.py rename : test/modules/aa/bb/bfile.py => tests/modules/aa/bb/bfile.py rename : test/modules/aa/bb/cc/__init__.py => tests/modules/aa/bb/cc/__init__.py rename : test/modules/aa/bb/cc/cfile.py => tests/modules/aa/bb/cc/cfile.py rename : test/modules/aa/zfile.py => tests/modules/aa/zfile.py rename : test/modules/covmod1.py => tests/modules/covmod1.py rename : test/modules/pkg1/__init__.py => tests/modules/pkg1/__init__.py rename : test/modules/pkg1/__main__.py => tests/modules/pkg1/__main__.py rename : test/modules/pkg1/p1a.py => tests/modules/pkg1/p1a.py rename : test/modules/pkg1/p1b.py => tests/modules/pkg1/p1b.py rename : test/modules/pkg1/p1c.py => tests/modules/pkg1/p1c.py rename : test/modules/pkg1/runmod2.py => tests/modules/pkg1/runmod2.py rename : test/modules/pkg1/sub/__init__.py => tests/modules/pkg1/sub/__init__.py rename : test/modules/pkg1/sub/__main__.py => tests/modules/pkg1/sub/__main__.py rename : test/modules/pkg1/sub/ps1a.py => tests/modules/pkg1/sub/ps1a.py rename : test/modules/pkg1/sub/runmod3.py => tests/modules/pkg1/sub/runmod3.py rename : test/modules/pkg2/__init__.py => tests/modules/pkg2/__init__.py rename : test/modules/pkg2/p2a.py => tests/modules/pkg2/p2a.py rename : test/modules/pkg2/p2b.py => tests/modules/pkg2/p2b.py rename : test/modules/runmod1.py => tests/modules/runmod1.py rename : test/modules/usepkgs.py => tests/modules/usepkgs.py rename : test/moremodules/othermods/__init__.py => tests/moremodules/othermods/__init__.py rename : test/moremodules/othermods/othera.py => tests/moremodules/othermods/othera.py rename : test/moremodules/othermods/otherb.py => tests/moremodules/othermods/otherb.py rename : test/moremodules/othermods/sub/__init__.py => tests/moremodules/othermods/sub/__init__.py rename : test/moremodules/othermods/sub/osa.py => tests/moremodules/othermods/sub/osa.py rename : test/moremodules/othermods/sub/osb.py => tests/moremodules/othermods/sub/osb.py rename : test/osinfo.py => tests/osinfo.py rename : test/qunit/jquery.tmpl.min.js => tests/qunit/jquery.tmpl.min.js rename : test/qunit/qunit.css => tests/qunit/qunit.css rename : test/qunit/qunit.js => tests/qunit/qunit.js rename : test/stress_phystoken.tok => tests/stress_phystoken.tok rename : test/stress_phystoken_dos.tok => tests/stress_phystoken_dos.tok rename : test/test_api.py => tests/test_api.py rename : test/test_arcs.py => tests/test_arcs.py rename : test/test_cmdline.py => tests/test_cmdline.py rename : test/test_codeunit.py => tests/test_codeunit.py rename : test/test_config.py => tests/test_config.py rename : test/test_coverage.py => tests/test_coverage.py rename : test/test_data.py => tests/test_data.py rename : test/test_execfile.py => tests/test_execfile.py rename : test/test_farm.py => tests/test_farm.py rename : test/test_files.py => tests/test_files.py rename : test/test_html.py => tests/test_html.py rename : test/test_misc.py => tests/test_misc.py rename : test/test_oddball.py => tests/test_oddball.py rename : test/test_parser.py => tests/test_parser.py rename : test/test_phystokens.py => tests/test_phystokens.py rename : test/test_process.py => tests/test_process.py rename : test/test_results.py => tests/test_results.py rename : test/test_summary.py => tests/test_summary.py rename : test/test_templite.py => tests/test_templite.py rename : test/test_testing.py => tests/test_testing.py rename : test/test_xml.py => tests/test_xml.py rename : test/try_execfile.py => tests/try_execfile.py
Diffstat (limited to 'tests/test_process.py')
-rw-r--r--tests/test_process.py575
1 files changed, 575 insertions, 0 deletions
diff --git a/tests/test_process.py b/tests/test_process.py
new file mode 100644
index 00000000..bf22cc9d
--- /dev/null
+++ b/tests/test_process.py
@@ -0,0 +1,575 @@
+"""Tests for process behavior of coverage.py."""
+
+import glob, os, sys, textwrap
+from nose.plugins.skip import SkipTest
+import coverage
+
+from test.coveragetest import CoverageTest
+
+here = os.path.dirname(__file__)
+
+class ProcessTest(CoverageTest):
+ """Tests of the per-process behavior of coverage.py."""
+
+ def number_of_data_files(self):
+ """Return the number of coverage data files in this directory."""
+ num = 0
+ for f in os.listdir('.'):
+ if f.startswith('.coverage.') or f == '.coverage':
+ num += 1
+ return num
+
+ def test_save_on_exit(self):
+ self.make_file("mycode.py", """\
+ h = "Hello"
+ w = "world"
+ """)
+
+ self.assert_doesnt_exist(".coverage")
+ self.run_command("coverage -x mycode.py")
+ self.assert_exists(".coverage")
+
+ def test_environment(self):
+ # Checks that we can import modules from the test directory at all!
+ self.make_file("mycode.py", """\
+ import covmod1
+ import covmodzip1
+ a = 1
+ print ('done')
+ """)
+
+ self.assert_doesnt_exist(".coverage")
+ out = self.run_command("coverage -x mycode.py")
+ self.assert_exists(".coverage")
+ self.assertEqual(out, 'done\n')
+
+ def test_combine_parallel_data(self):
+ self.make_file("b_or_c.py", """\
+ import sys
+ a = 1
+ if sys.argv[1] == 'b':
+ b = 1
+ else:
+ c = 1
+ d = 1
+ print ('done')
+ """)
+
+ out = self.run_command("coverage -x -p b_or_c.py b")
+ self.assertEqual(out, 'done\n')
+ self.assert_doesnt_exist(".coverage")
+
+ out = self.run_command("coverage -x -p b_or_c.py c")
+ self.assertEqual(out, 'done\n')
+ self.assert_doesnt_exist(".coverage")
+
+ # After two -p runs, there should be two .coverage.machine.123 files.
+ self.assertEqual(self.number_of_data_files(), 2)
+
+ # Combine the parallel coverage data files into .coverage .
+ self.run_command("coverage -c")
+ self.assert_exists(".coverage")
+
+ # After combining, there should be only the .coverage file.
+ self.assertEqual(self.number_of_data_files(), 1)
+
+ # Read the coverage file and see that b_or_c.py has all 7 lines
+ # executed.
+ data = coverage.CoverageData()
+ data.read_file(".coverage")
+ self.assertEqual(data.summary()['b_or_c.py'], 7)
+
+ def test_combine_parallel_data_in_two_steps(self):
+ self.make_file("b_or_c.py", """\
+ import sys
+ a = 1
+ if sys.argv[1] == 'b':
+ b = 1
+ else:
+ c = 1
+ d = 1
+ print ('done')
+ """)
+
+ out = self.run_command("coverage -x -p b_or_c.py b")
+ self.assertEqual(out, 'done\n')
+ self.assert_doesnt_exist(".coverage")
+ self.assertEqual(self.number_of_data_files(), 1)
+
+ # Combine the (one) parallel coverage data file into .coverage .
+ self.run_command("coverage -c")
+ self.assert_exists(".coverage")
+ self.assertEqual(self.number_of_data_files(), 1)
+
+ out = self.run_command("coverage -x -p b_or_c.py c")
+ self.assertEqual(out, 'done\n')
+ self.assert_exists(".coverage")
+ self.assertEqual(self.number_of_data_files(), 2)
+
+ # Combine the parallel coverage data files into .coverage .
+ self.run_command("coverage -c")
+ self.assert_exists(".coverage")
+
+ # After combining, there should be only the .coverage file.
+ self.assertEqual(self.number_of_data_files(), 1)
+
+ # Read the coverage file and see that b_or_c.py has all 7 lines
+ # executed.
+ data = coverage.CoverageData()
+ data.read_file(".coverage")
+ self.assertEqual(data.summary()['b_or_c.py'], 7)
+
+ def test_combine_with_rc(self):
+ self.make_file("b_or_c.py", """\
+ import sys
+ a = 1
+ if sys.argv[1] == 'b':
+ b = 1
+ else:
+ c = 1
+ d = 1
+ print ('done')
+ """)
+
+ self.make_file(".coveragerc", """\
+ [run]
+ parallel = true
+ """)
+
+ out = self.run_command("coverage run b_or_c.py b")
+ self.assertEqual(out, 'done\n')
+ self.assert_doesnt_exist(".coverage")
+
+ out = self.run_command("coverage run b_or_c.py c")
+ self.assertEqual(out, 'done\n')
+ self.assert_doesnt_exist(".coverage")
+
+ # After two runs, there should be two .coverage.machine.123 files.
+ self.assertEqual(self.number_of_data_files(), 2)
+
+ # Combine the parallel coverage data files into .coverage .
+ self.run_command("coverage combine")
+ self.assert_exists(".coverage")
+ self.assert_exists(".coveragerc")
+
+ # After combining, there should be only the .coverage file.
+ self.assertEqual(self.number_of_data_files(), 1)
+
+ # Read the coverage file and see that b_or_c.py has all 7 lines
+ # executed.
+ data = coverage.CoverageData()
+ data.read_file(".coverage")
+ self.assertEqual(data.summary()['b_or_c.py'], 7)
+
+ # Reporting should still work even with the .rc file
+ out = self.run_command("coverage report")
+ self.assertMultiLineEqual(out, textwrap.dedent("""\
+ Name Stmts Miss Cover
+ ----------------------------
+ b_or_c 7 0 100%
+ """))
+
+ def test_combine_with_aliases(self):
+ self.make_file("d1/x.py", """\
+ a = 1
+ b = 2
+ print("%s %s" % (a, b))
+ """)
+
+ self.make_file("d2/x.py", """\
+ # 1
+ # 2
+ # 3
+ c = 4
+ d = 5
+ print("%s %s" % (c, d))
+ """)
+
+ self.make_file(".coveragerc", """\
+ [run]
+ parallel = True
+
+ [paths]
+ source =
+ src
+ */d1
+ */d2
+ """)
+
+ out = self.run_command("coverage run " + os.path.normpath("d1/x.py"))
+ self.assertEqual(out, '1 2\n')
+ out = self.run_command("coverage run " + os.path.normpath("d2/x.py"))
+ self.assertEqual(out, '4 5\n')
+
+ self.assertEqual(self.number_of_data_files(), 2)
+
+ self.run_command("coverage combine")
+ self.assert_exists(".coverage")
+
+ # After combining, there should be only the .coverage file.
+ self.assertEqual(self.number_of_data_files(), 1)
+
+ # Read the coverage data file and see that the two different x.py
+ # files have been combined together.
+ data = coverage.CoverageData()
+ data.read_file(".coverage")
+ summary = data.summary(fullpath=True)
+ self.assertEqual(len(summary), 1)
+ actual = os.path.normcase(os.path.abspath(list(summary.keys())[0]))
+ expected = os.path.normcase(os.path.abspath('src/x.py'))
+ self.assertEqual(actual, expected)
+ self.assertEqual(list(summary.values())[0], 6)
+
+ def test_missing_source_file(self):
+ # Check what happens if the source is missing when reporting happens.
+ self.make_file("fleeting.py", """\
+ s = 'goodbye, cruel world!'
+ """)
+
+ self.run_command("coverage run fleeting.py")
+ os.remove("fleeting.py")
+ out = self.run_command("coverage html -d htmlcov")
+ self.assertRegexpMatches(out, "No source for code: '.*fleeting.py'")
+ self.assertNotIn("Traceback", out)
+
+ # It happens that the code paths are different for *.py and other
+ # files, so try again with no extension.
+ self.make_file("fleeting", """\
+ s = 'goodbye, cruel world!'
+ """)
+
+ self.run_command("coverage run fleeting")
+ os.remove("fleeting")
+ status, out = self.run_command_status("coverage html -d htmlcov", 1)
+ self.assertRegexpMatches(out, "No source for code: '.*fleeting'")
+ self.assertNotIn("Traceback", out)
+ self.assertEqual(status, 1)
+
+ def test_running_missing_file(self):
+ status, out = self.run_command_status("coverage run xyzzy.py", 1)
+ self.assertRegexpMatches(out, "No file to run: .*xyzzy.py")
+ self.assertNotIn("raceback", out)
+ self.assertNotIn("rror", out)
+ self.assertEqual(status, 1)
+
+ def test_code_throws(self):
+ self.make_file("throw.py", """\
+ def f1():
+ raise Exception("hey!")
+
+ def f2():
+ f1()
+
+ f2()
+ """)
+
+ # The important thing is for "coverage run" and "python" to report the
+ # same traceback.
+ status, out = self.run_command_status("coverage run throw.py", 1)
+ out2 = self.run_command("python throw.py")
+ if '__pypy__' in sys.builtin_module_names:
+ # Pypy has an extra frame in the traceback for some reason
+ lines2 = out2.splitlines()
+ out2 = "".join([l+"\n" for l in lines2 if "toplevel" not in l])
+ self.assertMultiLineEqual(out, out2)
+
+ # But also make sure that the output is what we expect.
+ self.assertIn('File "throw.py", line 5, in f2', out)
+ self.assertIn('raise Exception("hey!")', out)
+ self.assertNotIn('coverage', out)
+ self.assertEqual(status, 1)
+
+ def test_code_exits(self):
+ self.make_file("exit.py", """\
+ import sys
+ def f1():
+ print("about to exit..")
+ sys.exit(17)
+
+ def f2():
+ f1()
+
+ f2()
+ """)
+
+ # The important thing is for "coverage run" and "python" to have the
+ # same output. No traceback.
+ status, out = self.run_command_status("coverage run exit.py", 17)
+ status2, out2 = self.run_command_status("python exit.py", 17)
+ self.assertMultiLineEqual(out, out2)
+ self.assertMultiLineEqual(out, "about to exit..\n")
+ self.assertEqual(status, status2)
+ self.assertEqual(status, 17)
+
+ def test_code_exits_no_arg(self):
+ self.make_file("exit_none.py", """\
+ import sys
+ def f1():
+ print("about to exit quietly..")
+ sys.exit()
+
+ f1()
+ """)
+ status, out = self.run_command_status("coverage run exit_none.py", 0)
+ status2, out2 = self.run_command_status("python exit_none.py", 0)
+ self.assertMultiLineEqual(out, out2)
+ self.assertMultiLineEqual(out, "about to exit quietly..\n")
+ self.assertEqual(status, status2)
+ self.assertEqual(status, 0)
+
+ def test_coverage_run_is_like_python(self):
+ tryfile = os.path.join(here, "try_execfile.py")
+ self.make_file("run_me.py", open(tryfile).read())
+ out = self.run_command("coverage run run_me.py")
+ out2 = self.run_command("python run_me.py")
+ self.assertMultiLineEqual(out, out2)
+
+ if sys.version_info >= (2, 6): # Doesn't work in 2.5, and I don't care!
+ def test_coverage_run_dashm_is_like_python_dashm(self):
+ # These -m commands assume the coverage tree is on the path.
+ out = self.run_command("coverage run -m test.try_execfile")
+ out2 = self.run_command("python -m test.try_execfile")
+ self.assertMultiLineEqual(out, out2)
+
+ if 0: # Expected failure
+ # For https://bitbucket.org/ned/coveragepy/issue/207
+ def test_coverage_run_dashm_is_like_python_dashm_with__main__207(self):
+ self.make_file("package/__init__.py") # empty
+ self.make_file("package/__main__.py", "#\n") # empty
+ out = self.run_command("coverage run -m package")
+ out2 = self.run_command("python -m package")
+ self.assertMultiLineEqual(out, out2)
+
+ if hasattr(os, 'fork'):
+ def test_fork(self):
+ self.make_file("fork.py", """\
+ import os
+
+ def child():
+ print('Child!')
+
+ def main():
+ ret = os.fork()
+
+ if ret == 0:
+ child()
+ else:
+ os.waitpid(ret, 0)
+
+ main()
+ """)
+
+ out = self.run_command("coverage run -p fork.py")
+ self.assertEqual(out, 'Child!\n')
+ self.assert_doesnt_exist(".coverage")
+
+ # After running the forking program, there should be two
+ # .coverage.machine.123 files.
+ self.assertEqual(self.number_of_data_files(), 2)
+
+ # Combine the parallel coverage data files into .coverage .
+ self.run_command("coverage -c")
+ self.assert_exists(".coverage")
+
+ # After combining, there should be only the .coverage file.
+ self.assertEqual(self.number_of_data_files(), 1)
+
+ # Read the coverage file and see that b_or_c.py has all 7 lines
+ # executed.
+ data = coverage.CoverageData()
+ data.read_file(".coverage")
+ self.assertEqual(data.summary()['fork.py'], 9)
+
+ def test_warnings(self):
+ self.make_file("hello.py", """\
+ import sys, os
+ print("Hello")
+ """)
+ out = self.run_command("coverage run --source=sys,xyzzy,quux hello.py")
+
+ self.assertIn("Hello\n", out)
+ self.assertIn(textwrap.dedent("""\
+ Coverage.py warning: Module sys has no Python source.
+ Coverage.py warning: Module xyzzy was never imported.
+ Coverage.py warning: Module quux was never imported.
+ Coverage.py warning: No data was collected.
+ """), out)
+
+ def test_warnings_during_reporting(self):
+ # While fixing issue #224, the warnings were being printed far too
+ # often. Make sure they're not any more.
+ self.make_file("hello.py", """\
+ import sys, os, the_other
+ print("Hello")
+ """)
+ self.make_file("the_other.py", """\
+ print("What?")
+ """)
+ self.make_file(".coveragerc", """\
+ [run]
+ source =
+ .
+ xyzzy
+ """)
+
+ self.run_command("coverage run hello.py")
+ out = self.run_command("coverage html")
+ self.assertEqual(out.count("Module xyzzy was never imported."), 0)
+
+ def test_warnings_if_never_run(self):
+ out = self.run_command("coverage run i_dont_exist.py")
+ self.assertIn("No file to run: 'i_dont_exist.py'", out)
+ self.assertNotIn("warning", out)
+ self.assertNotIn("Exception", out)
+
+ out = self.run_command("coverage run -m no_such_module")
+ self.assertTrue(
+ ("No module named no_such_module" in out) or
+ ("No module named 'no_such_module'" in out)
+ )
+ self.assertNotIn("warning", out)
+ self.assertNotIn("Exception", out)
+
+ if sys.version_info >= (3, 0): # This only works on 3.x for now.
+ # It only works with the C tracer,
+ c_tracer = os.getenv('COVERAGE_TEST_TRACER', 'c') == 'c'
+ # and if we aren't measuring ourselves.
+ metacov = os.getenv('COVERAGE_COVERAGE', '') != ''
+ if c_tracer and not metacov: # pragma: not covered
+ def test_fullcoverage(self):
+ # fullcoverage is a trick to get stdlib modules measured from
+ # the very beginning of the process. Here we import os and
+ # then check how many lines are measured.
+ self.make_file("getenv.py", """\
+ import os
+ print("FOOEY == %s" % os.getenv("FOOEY"))
+ """)
+
+ fullcov = os.path.join(
+ os.path.dirname(coverage.__file__), "fullcoverage"
+ )
+ self.set_environ("FOOEY", "BOO")
+ self.set_environ("PYTHONPATH", fullcov)
+ out = self.run_command("python -m coverage run -L getenv.py")
+ self.assertEqual(out, "FOOEY == BOO\n")
+ data = coverage.CoverageData()
+ data.read_file(".coverage")
+ # The actual number of executed lines in os.py when it's
+ # imported is 120 or so. Just running os.getenv executes
+ # about 5.
+ self.assertGreater(data.summary()['os.py'], 50)
+
+
+class AliasedCommandTests(CoverageTest):
+ """Tests of the version-specific command aliases."""
+
+ def test_major_version_works(self):
+ # "coverage2" works on py2
+ cmd = "coverage%d" % sys.version_info[0]
+ out = self.run_command(cmd)
+ self.assertIn("Code coverage for Python", out)
+
+ def test_wrong_alias_doesnt_work(self):
+ # "coverage3" doesn't work on py2
+ badcmd = "coverage%d" % (5 - sys.version_info[0])
+ out = self.run_command(badcmd)
+ self.assertNotIn("Code coverage for Python", out)
+
+ def test_specific_alias_works(self):
+ # "coverage-2.7" works on py2.7
+ cmd = "coverage-%d.%d" % sys.version_info[:2]
+ out = self.run_command(cmd)
+ self.assertIn("Code coverage for Python", out)
+
+
+class FailUnderTest(CoverageTest):
+ """Tests of the --fail-under switch."""
+
+ def setUp(self):
+ super(FailUnderTest, self).setUp()
+ self.make_file("fifty.py", """\
+ # I have 50% coverage!
+ a = 1
+ if a > 2:
+ b = 3
+ c = 4
+ """)
+ st, _ = self.run_command_status("coverage run fifty.py", 0)
+ self.assertEqual(st, 0)
+
+ def test_report(self):
+ st, _ = self.run_command_status("coverage report --fail-under=50", 0)
+ self.assertEqual(st, 0)
+ st, _ = self.run_command_status("coverage report --fail-under=51", 2)
+ self.assertEqual(st, 2)
+
+ def test_html_report(self):
+ st, _ = self.run_command_status("coverage html --fail-under=50", 0)
+ self.assertEqual(st, 0)
+ st, _ = self.run_command_status("coverage html --fail-under=51", 2)
+ self.assertEqual(st, 2)
+
+ def test_xml_report(self):
+ st, _ = self.run_command_status("coverage xml --fail-under=50", 0)
+ self.assertEqual(st, 0)
+ st, _ = self.run_command_status("coverage xml --fail-under=51", 2)
+ self.assertEqual(st, 2)
+
+
+class ProcessStartupTest(CoverageTest):
+ """Test that we can measure coverage in subprocesses."""
+
+ def setUp(self):
+ super(ProcessStartupTest, 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
+ g = glob.glob(os.path.join(d, "*.pth"))
+ if g:
+ pth_path = os.path.join(d, "subcover.pth")
+ pth = open(pth_path, "w")
+ try:
+ try:
+ pth.write(pth_contents)
+ self.pth_path = pth_path
+ break
+ except (IOError, OSError): # pragma: not covered
+ pass
+ finally:
+ pth.close()
+ else: # pragma: not covered
+ raise Exception("Couldn't find a place for the .pth file")
+
+ def tearDown(self):
+ super(ProcessStartupTest, self).tearDown()
+ # Clean up the .pth file we made.
+ os.remove(self.pth_path)
+
+ def test_subprocess_with_pth_files(self): # pragma: not covered
+ 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("main.py", """\
+ import os
+ os.system("python sub.py")
+ """)
+ # sub.py will write a few lines.
+ self.make_file("sub.py", """\
+ f = open("out.txt", "w")
+ f.write("Hello, world!\\n")
+ f.close()
+ """)
+ self.make_file("coverage.ini", """\
+ [run]
+ data_file = .mycovdata
+ """)
+ self.set_environ("COVERAGE_PROCESS_START", "coverage.ini")
+ import main # pylint: disable=F0401,W0612
+
+ self.assertEqual(open("out.txt").read(), "Hello, world!\n")
+ # Read the data from .coverage
+ data = coverage.CoverageData()
+ data.read_file(".mycovdata")
+ self.assertEqual(data.summary()['sub.py'], 3)