@@ -1691,13 +1691,37 @@ def render(filename, linenum):
1691
1691
def fourth(x):
1692
1692
return 4 * x
1693
1693
""" )
1694
+ # Some namespace packages.
1695
+ make_file ("third_pkg/nspkg/fifth/__init__.py" , """\
1696
+ def fifth(x):
1697
+ return 5 * x
1698
+ """ )
1699
+ # The setup.py to install everything.
1694
1700
make_file ("third_pkg/setup.py" , """\
1695
1701
import setuptools
1696
- setuptools.setup(name="third", packages=["third", "fourth"])
1702
+ setuptools.setup(
1703
+ name="third",
1704
+ packages=["third", "fourth", "nspkg.fifth"],
1705
+ )
1706
+ """ )
1707
+
1708
+ # Some namespace packages.
1709
+ make_file ("another_pkg/nspkg/sixth/__init__.py" , """\
1710
+ def sixth(x):
1711
+ return 6 * x
1712
+ """ )
1713
+ # The setup.py to install everything.
1714
+ make_file ("another_pkg/setup.py" , """\
1715
+ import setuptools
1716
+ setuptools.setup(
1717
+ name="another",
1718
+ packages=["nspkg.sixth"],
1719
+ )
1697
1720
""" )
1698
1721
1699
1722
# Install the third-party packages.
1700
1723
run_in_venv ("python -m pip install --no-index ./third_pkg" )
1724
+ run_in_venv ("python -m pip install --no-index -e ./another_pkg" )
1701
1725
shutil .rmtree ("third_pkg" )
1702
1726
1703
1727
# Install coverage.
@@ -1719,17 +1743,22 @@ def coverage_command_fixture(request):
1719
1743
class VirtualenvTest (CoverageTest ):
1720
1744
"""Tests of virtualenv considerations."""
1721
1745
1746
+ expected_stdout = "33\n 110\n 198\n 1.5\n "
1747
+
1722
1748
@pytest .fixture (autouse = True )
1723
1749
def in_venv_world_fixture (self , venv_world ):
1724
1750
"""For running tests inside venv_world, and cleaning up made files."""
1725
1751
with change_dir (venv_world ):
1726
1752
self .make_file ("myproduct.py" , """\
1727
1753
import colorsys
1728
1754
import third
1755
+ import nspkg.fifth
1756
+ import nspkg.sixth
1729
1757
print(third.third(11))
1758
+ print(nspkg.fifth.fifth(22))
1759
+ print(nspkg.sixth.sixth(33))
1730
1760
print(sum(colorsys.rgb_to_hls(1, 0, 0)))
1731
1761
""" )
1732
- self .expected_stdout = "33\n 1.5\n " # pylint: disable=attribute-defined-outside-init
1733
1762
1734
1763
self .del_environ ("COVERAGE_TESTING" ) # To avoid needing contracts installed.
1735
1764
self .set_environ ("COVERAGE_DEBUG_FILE" , "debug_out.txt" )
@@ -1738,7 +1767,7 @@ def in_venv_world_fixture(self, venv_world):
1738
1767
yield
1739
1768
1740
1769
for fname in os .listdir ("." ):
1741
- if fname != "venv" :
1770
+ if fname not in { "venv" , "another_pkg" } :
1742
1771
os .remove (fname )
1743
1772
1744
1773
def get_trace_output (self ):
@@ -1829,3 +1858,45 @@ def test_venv_with_dynamic_plugin(self, coverage_command):
1829
1858
# The output should not have this warning:
1830
1859
# Already imported a file that will be measured: ...third/render.py (already-imported)
1831
1860
assert out == "HTML: hello.html@1723\n "
1861
+
1862
+ def test_installed_namespace_packages (self , coverage_command ):
1863
+ # https://github.com/nedbat/coveragepy/issues/1231
1864
+ # When namespace packages were installed, they were considered
1865
+ # third-party packages. Test that isn't still happening.
1866
+ out = run_in_venv (coverage_command + " run --source=nspkg myproduct.py" )
1867
+ # In particular, this warning doesn't appear:
1868
+ # Already imported a file that will be measured: .../coverage/__main__.py
1869
+ assert out == self .expected_stdout
1870
+
1871
+ # Check that our tracing was accurate. Files are mentioned because
1872
+ # --source refers to a file.
1873
+ debug_out = self .get_trace_output ()
1874
+ assert re_lines (
1875
+ debug_out ,
1876
+ r"^Not tracing .*\bexecfile.py': " +
1877
+ "module 'coverage.execfile' falls outside the --source spec"
1878
+ )
1879
+ assert re_lines (
1880
+ debug_out ,
1881
+ r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec"
1882
+ )
1883
+ assert re_lines (
1884
+ debug_out ,
1885
+ r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec"
1886
+ )
1887
+
1888
+ out = run_in_venv ("python -m coverage report" )
1889
+
1890
+ # Name Stmts Miss Cover
1891
+ # ------------------------------------------------------------------------------
1892
+ # another_pkg/nspkg/sixth/__init__.py 2 0 100%
1893
+ # venv/lib/python3.9/site-packages/nspkg/fifth/__init__.py 2 0 100%
1894
+ # ------------------------------------------------------------------------------
1895
+ # TOTAL 4 0 100%
1896
+
1897
+ assert "myproduct.py" not in out
1898
+ assert "third" not in out
1899
+ assert "coverage" not in out
1900
+ assert "colorsys" not in out
1901
+ assert "fifth" in out
1902
+ assert "sixth" in out
0 commit comments