summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-08-01 17:15:27 -0400
committerNed Batchelder <ned@nedbatchelder.com>2015-08-01 17:15:27 -0400
commit5e45baa100135e2ef327feb767e8e027a58d1637 (patch)
tree352e759ace46d27e92a66b35045313abe7121e15 /tests
parent3e6e85a71c1ad66cd8ed658a61bedbff38f47dd1 (diff)
downloadpython-coveragepy-git-5e45baa100135e2ef327feb767e8e027a58d1637.tar.gz
Support directories on the 'coverage run' command line. #252
Diffstat (limited to 'tests')
-rw-r--r--tests/test_execfile.py15
-rw-r--r--tests/test_process.py27
-rw-r--r--tests/with_main/__main__.py2
-rw-r--r--tests/with_main/without/__init__.py1
4 files changed, 36 insertions, 9 deletions
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index 2533f81d..a3ea1153 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -86,14 +86,17 @@ class RunFileTest(CoverageTest):
run_python_file("xyzzy.py", [])
def test_directory_with_main(self):
- directory_with_main = os.path.join(HERE, "with_main")
- run_python_file(directory_with_main, [directory_with_main])
- self.assertEqual(self.stdout(), "1\n")
+ self.make_file("with_main/__main__.py", """\
+ print("I am __main__")
+ """)
+ run_python_file("with_main", ["with_main"])
+ self.assertEqual(self.stdout(), "I am __main__\n")
def test_directory_without_main(self):
- with self.assertRaises(NoSource):
- directory_with_main = os.path.join(HERE, "with_main", "without")
- run_python_file(directory_with_main, [directory_with_main])
+ self.make_file("without_main/__init__.py", "")
+ with self.assertRaisesRegex(NoSource, "Can't find '__main__' module in 'without_main'"):
+ run_python_file("without_main", ["without_main"])
+
class RunPycFileTest(CoverageTest):
"""Test cases for `run_python_file`."""
diff --git a/tests/test_process.py b/tests/test_process.py
index 7c8b0c2d..fcd88827 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -7,6 +7,7 @@
import glob
import os
import os.path
+import re
import sys
import textwrap
@@ -387,6 +388,26 @@ class ProcessTest(CoverageTest):
out_py = self.run_command("python -m tests.try_execfile")
self.assertMultiLineEqual(out_cov, out_py)
+ def test_coverage_run_dir_is_like_python_dir(self):
+ tryfile = os.path.join(HERE, "try_execfile.py")
+ with open(tryfile) 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)
+
def test_coverage_run_dashm_equal_to_doubledashsource(self):
"""regression test for #328
@@ -995,3 +1016,9 @@ class ProcessStartupWithSourceTest(ProcessCoverageMixin, CoverageTest):
def test_script_pkg_sub(self):
self.assert_pth_and_source_work_together('', 'pkg', 'sub')
+
+
+def remove_matching_lines(text, pat):
+ """Return `text` with all lines matching `pat` removed."""
+ lines = [l for l in text.splitlines(True) if not re.search(pat, l)]
+ return "".join(lines)
diff --git a/tests/with_main/__main__.py b/tests/with_main/__main__.py
deleted file mode 100644
index e7a4e4f9..00000000
--- a/tests/with_main/__main__.py
+++ /dev/null
@@ -1,2 +0,0 @@
-x = 1
-print(x) \ No newline at end of file
diff --git a/tests/with_main/without/__init__.py b/tests/with_main/without/__init__.py
deleted file mode 100644
index 595e3818..00000000
--- a/tests/with_main/without/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-__author__ = 'traff'