diff options
Diffstat (limited to 'tests/test_process.py')
| -rw-r--r-- | tests/test_process.py | 265 |
1 files changed, 145 insertions, 120 deletions
diff --git a/tests/test_process.py b/tests/test_process.py index be9bdb76..0182fa26 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -19,8 +19,6 @@ from coverage.misc import output_encoding from tests.coveragetest import CoverageTest -TRY_EXECFILE = os.path.join(os.path.dirname(__file__), "modules/process_test/try_execfile.py") - class ProcessTest(CoverageTest): """Tests of the per-process behavior of coverage.py.""" @@ -438,118 +436,6 @@ class ProcessTest(CoverageTest): self.assertEqual(status, status2) self.assertEqual(status, 0) - def assert_execfile_output(self, out): - """Assert that the output we got is a successful run of try_execfile.py""" - self.assertIn('"DATA": "xyzzy"', out) - - def test_coverage_run_is_like_python(self): - with open(TRY_EXECFILE) as f: - self.make_file("run_me.py", f.read()) - out_cov = self.run_command("coverage run run_me.py") - out_py = self.run_command("python run_me.py") - self.assertMultiLineEqual(out_cov, out_py) - self.assert_execfile_output(out_cov) - - def test_coverage_run_dashm_is_like_python_dashm(self): - # These -m commands assume the coverage tree is on the path. - out_cov = self.run_command("coverage run -m process_test.try_execfile") - out_py = self.run_command("python -m process_test.try_execfile") - self.assertMultiLineEqual(out_cov, out_py) - self.assert_execfile_output(out_cov) - - def test_coverage_run_dir_is_like_python_dir(self): - with open(TRY_EXECFILE) as f: - self.make_file("with_main/__main__.py", f.read()) - out_cov = self.run_command("coverage run with_main") - out_py = self.run_command("python with_main") - - # The coverage.py results are not identical to the Python results, and - # I don't know why. For now, ignore those failures. If someone finds - # a real problem with the discrepancies, we can work on it some more. - ignored = r"__file__|__loader__|__package__" - # PyPy includes the current directory in the path when running a - # directory, while CPython and coverage.py do not. Exclude that from - # the comparison also... - if env.PYPY: - ignored += "|"+re.escape(os.getcwd()) - out_cov = remove_matching_lines(out_cov, ignored) - out_py = remove_matching_lines(out_py, ignored) - self.assertMultiLineEqual(out_cov, out_py) - self.assert_execfile_output(out_cov) - - 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 process_test.try_execfile -m process_test.try_execfile" - ) - out_py = self.run_command("python -m process_test.try_execfile") - self.assertMultiLineEqual(out_cov, out_py) - self.assert_execfile_output(out_cov) - - 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 process_test -m process_test.try_execfile" - ) - out_py = self.run_command("python -m process_test.try_execfile") - self.assertMultiLineEqual(out_cov, out_py) - self.assert_execfile_output(out_cov) - - 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): - # This file imports try_execfile, which compiles it to .pyc, so the - # first run will have __file__ == "try_execfile.py" and the second will - # have __file__ == "try_execfile.pyc", which throws off the comparison. - # Setting dont_write_bytecode True stops the compilation to .pyc and - # keeps the test working. - self.make_file("myscript", """\ - import sys; sys.dont_write_bytecode = True - import process_test.try_execfile - """) - - # These -m commands assume the coverage tree is on the path. - out_cov = self.run_command( - "coverage run --source process_test myscript" - ) - out_py = self.run_command("python myscript") - self.assertMultiLineEqual(out_cov, out_py) - self.assert_execfile_output(out_cov) - - 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 - self.make_file("sub/__init__.py", "") - with open(TRY_EXECFILE) as f: - self.make_file("sub/run_me.py", f.read()) - out_cov = self.run_command("coverage run -m sub.run_me") - out_py = self.run_command("python -m sub.run_me") - self.assertMultiLineEqual(out_cov, out_py) - self.assert_execfile_output(out_cov) - - def test_coverage_run_dashm_is_like_python_dashm_with__main__207(self): - if sys.version_info < (2, 7): - # Coverage.py isn't bug-for-bug compatible in the behavior of -m for - # Pythons < 2.7 - self.skipTest("-m doesn't work the same < Python 2.7") - # https://bitbucket.org/ned/coveragepy/issue/207 - self.make_file("package/__init__.py", "print('init')") - self.make_file("package/__main__.py", "print('main')") - out_cov = self.run_command("coverage run -m package") - out_py = self.run_command("python -m package") - self.assertMultiLineEqual(out_cov, out_py) - def test_fork(self): if not hasattr(os, 'fork'): self.skipTest("Can't test os.fork since it doesn't exist.") @@ -789,6 +675,133 @@ class ProcessTest(CoverageTest): self.assertIn("Use 'coverage help' for help", out) +TRY_EXECFILE = os.path.join(os.path.dirname(__file__), "modules/process_test/try_execfile.py") + +class EnvironmentTest(CoverageTest): + """Tests using try_execfile.py to test the execution environment.""" + + def setUp(self): + super(EnvironmentTest, self).setUp() + + if env.JYTHON: + # Jython has a different sys.argv[0], always skip it. + self.set_environ("COVERAGE_TRY_EXECFILE_SKIPS", "argv0") + + def assert_execfile_output(self, out): + """Assert that the output we got is a successful run of try_execfile.py""" + self.assertIn('"DATA": "xyzzy"', out) + + def test_coverage_run_is_like_python(self): + with open(TRY_EXECFILE) as f: + self.make_file("run_me.py", f.read()) + out_cov = self.run_command("coverage run run_me.py") + out_py = self.run_command("python run_me.py") + self.assertMultiLineEqual(out_cov, out_py) + self.assert_execfile_output(out_cov) + + def test_coverage_run_dashm_is_like_python_dashm(self): + # These -m commands assume the coverage tree is on the path. + out_cov = self.run_command("coverage run -m process_test.try_execfile") + out_py = self.run_command("python -m process_test.try_execfile") + self.assertMultiLineEqual(out_cov, out_py) + self.assert_execfile_output(out_cov) + + def test_coverage_run_dir_is_like_python_dir(self): + with open(TRY_EXECFILE) as f: + self.make_file("with_main/__main__.py", f.read()) + + out_cov = self.run_command("coverage run with_main") + out_py = self.run_command("python with_main") + + # The coverage.py results are not identical to the Python results, and + # I don't know why. For now, ignore those failures. If someone finds + # a real problem with the discrepancies, we can work on it some more. + ignored = r"__file__|__loader__|__package__" + # PyPy includes the current directory in the path when running a + # directory, while CPython and coverage.py do not. Exclude that from + # the comparison also... + if env.PYPY: + ignored += "|"+re.escape(os.getcwd()) + out_cov = remove_matching_lines(out_cov, ignored) + out_py = remove_matching_lines(out_py, ignored) + self.assertMultiLineEqual(out_cov, out_py) + self.assert_execfile_output(out_cov) + + 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 process_test.try_execfile -m process_test.try_execfile" + ) + out_py = self.run_command("python -m process_test.try_execfile") + self.assertMultiLineEqual(out_cov, out_py) + self.assert_execfile_output(out_cov) + + 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 process_test -m process_test.try_execfile" + ) + out_py = self.run_command("python -m process_test.try_execfile") + self.assertMultiLineEqual(out_cov, out_py) + self.assert_execfile_output(out_cov) + + 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): + # This file imports try_execfile, which compiles it to .pyc, so the + # first run will have __file__ == "try_execfile.py" and the second will + # have __file__ == "try_execfile.pyc", which throws off the comparison. + # Setting dont_write_bytecode True stops the compilation to .pyc and + # keeps the test working. + self.make_file("myscript", """\ + import sys; sys.dont_write_bytecode = True + import process_test.try_execfile + """) + + # These -m commands assume the coverage tree is on the path. + out_cov = self.run_command( + "coverage run --source process_test myscript" + ) + out_py = self.run_command("python myscript") + self.assertMultiLineEqual(out_cov, out_py) + self.assert_execfile_output(out_cov) + + 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 + self.make_file("sub/__init__.py", "") + with open(TRY_EXECFILE) as f: + self.make_file("sub/run_me.py", f.read()) + + out_cov = self.run_command("coverage run -m sub.run_me") + out_py = self.run_command("python -m sub.run_me") + self.assertMultiLineEqual(out_cov, out_py) + self.assert_execfile_output(out_cov) + + def test_coverage_run_dashm_is_like_python_dashm_with__main__207(self): + if sys.version_info < (2, 7): + # Coverage.py isn't bug-for-bug compatible in the behavior of -m for + # Pythons < 2.7 + self.skipTest("-m doesn't work the same < Python 2.7") + # https://bitbucket.org/ned/coveragepy/issue/207 + self.make_file("package/__init__.py", "print('init')") + self.make_file("package/__main__.py", "print('main')") + out_cov = self.run_command("coverage run -m package") + out_py = self.run_command("python -m package") + self.assertMultiLineEqual(out_cov, out_py) + + class ExcepthookTest(CoverageTest): """Tests of sys.excepthook support.""" @@ -808,8 +821,9 @@ class ExcepthookTest(CoverageTest): """) cov_st, cov_out = self.run_command_status("coverage run excepthook.py") py_st, py_out = self.run_command_status("python excepthook.py") - self.assertEqual(cov_st, py_st) - self.assertEqual(cov_st, 1) + if not env.JYTHON: + self.assertEqual(cov_st, py_st) + self.assertEqual(cov_st, 1) self.assertIn("in excepthook", py_out) self.assertEqual(cov_out, py_out) @@ -821,8 +835,8 @@ class ExcepthookTest(CoverageTest): self.assertEqual(data.line_counts()['excepthook.py'], 7) def test_excepthook_exit(self): - if env.PYPY: - self.skipTest("PyPy handles excepthook exits differently, punt for now.") + if env.PYPY or env.JYTHON: + self.skipTest("non-CPython handles excepthook exits differently, punt for now.") self.make_file("excepthook_exit.py", """\ import sys @@ -861,8 +875,9 @@ class ExcepthookTest(CoverageTest): """) cov_st, cov_out = self.run_command_status("coverage run excepthook_throw.py") py_st, py_out = self.run_command_status("python excepthook_throw.py") - self.assertEqual(cov_st, py_st) - self.assertEqual(cov_st, 1) + if not env.JYTHON: + self.assertEqual(cov_st, py_st) + self.assertEqual(cov_st, 1) self.assertIn("in excepthook", py_out) self.assertEqual(cov_out, py_out) @@ -873,6 +888,11 @@ class AliasedCommandTest(CoverageTest): run_in_temp_dir = False + def setUp(self): + super(AliasedCommandTest, self).setUp() + if env.JYTHON: + self.skipTest("Coverage command names don't work on Jython") + def test_major_version_works(self): # "coverage2" works on py2 cmd = "coverage%d" % sys.version_info[0] @@ -1072,6 +1092,11 @@ class FailUnder100Test(CoverageTest): class UnicodeFilePathsTest(CoverageTest): """Tests of using non-ascii characters in the names of files.""" + def setUp(self): + super(UnicodeFilePathsTest, self).setUp() + if env.JYTHON: + self.skipTest("Jython 2 doesn't like accented file names") + def test_accented_dot_py(self): # Make a file with a non-ascii character in the filename. self.make_file(u"h\xe2t.py", "print('accented')") |
