|
30 | 30 |
|
31 | 31 | from tests import testenv
|
32 | 32 | from tests.coveragetest import CoverageTest, TESTS_DIR
|
33 |
| -from tests.helpers import re_line, re_lines, re_lines_text |
| 33 | +from tests.helpers import change_dir, re_line, re_lines, re_lines_text |
34 | 34 |
|
35 | 35 |
|
36 | 36 | class ProcessTest(CoverageTest):
|
@@ -1627,6 +1627,45 @@ def f2():
|
1627 | 1627 | data.read()
|
1628 | 1628 | assert line_counts(data)["subfunctions.py"] == 11
|
1629 | 1629 |
|
| 1630 | + def test_subprocess_dir_with_source(self) -> None: |
| 1631 | + # https://github.com/nedbat/coveragepy/issues/1499 |
| 1632 | + self.make_file("main/d/README", "A sub-directory") |
| 1633 | + self.make_file( |
| 1634 | + "main/main.py", |
| 1635 | + """\ |
| 1636 | + import os, subprocess, sys |
| 1637 | + orig = os.getcwd() |
| 1638 | + os.chdir("./d") |
| 1639 | + subprocess.run([sys.executable, f"{orig}/sub.py"]) |
| 1640 | + os.chdir(orig) |
| 1641 | + """, |
| 1642 | + ) |
| 1643 | + self.make_file("lib/other.py", "print('Other', flush=True)") |
| 1644 | + self.make_file( |
| 1645 | + "main/sub.py", |
| 1646 | + """ |
| 1647 | + import other |
| 1648 | + print("Hello, world!", flush=True) |
| 1649 | + """, |
| 1650 | + ) |
| 1651 | + self.make_file( |
| 1652 | + "main/pyproject.toml", |
| 1653 | + """\ |
| 1654 | + [tool.coverage.run] |
| 1655 | + patch = ["subprocess"] |
| 1656 | + source = [".", "other"] |
| 1657 | + disable_warnings = ["module-not-imported"] |
| 1658 | + """, |
| 1659 | + ) |
| 1660 | + self.set_environ("PYTHONPATH", os.path.abspath("lib")) |
| 1661 | + with change_dir("main"): |
| 1662 | + out = self.run_command("coverage run main.py") |
| 1663 | + assert out == "Other\nHello, world!\n" |
| 1664 | + self.run_command("coverage combine") |
| 1665 | + data = coverage.CoverageData() |
| 1666 | + data.read() |
| 1667 | + assert line_counts(data) == {"main.py": 5, "sub.py": 2, "other.py": 1} |
| 1668 | + |
1630 | 1669 |
|
1631 | 1670 | @pytest.fixture
|
1632 | 1671 | def _clean_pth_files() -> Iterable[None]:
|
|
0 commit comments