Skip to content

Commit 7589066

Browse files
committed
refactor(test): the virtualenv tests deserve their own file
1 parent 3b7f8c1 commit 7589066

File tree

2 files changed

+277
-263
lines changed

2 files changed

+277
-263
lines changed

Diff for: tests/test_process.py

+1-263
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
import os.path
99
import re
10-
import shutil
1110
import stat
1211
import sys
1312
import sysconfig
@@ -23,8 +22,7 @@
2322
from coverage.files import abs_file, python_reported_file
2423

2524
from tests.coveragetest import CoverageTest, TESTS_DIR
26-
from tests.helpers import change_dir, make_file, nice_file, os_sep
27-
from tests.helpers import re_lines, re_lines_text, run_command
25+
from tests.helpers import os_sep, re_lines_text
2826

2927

3028
class ProcessTest(CoverageTest):
@@ -1662,263 +1660,3 @@ def test_dashm_pkg_sub(self):
16621660

16631661
def test_script_pkg_sub(self):
16641662
self.assert_pth_and_source_work_together('', 'pkg', 'sub')
1665-
1666-
1667-
def run_in_venv(cmd):
1668-
r"""Run `cmd` in the virtualenv at `venv`.
1669-
1670-
The first word of the command will be adjusted to run it from the
1671-
venv/bin or venv\Scripts directory.
1672-
1673-
Returns the text output of the command.
1674-
"""
1675-
words = cmd.split()
1676-
if env.WINDOWS:
1677-
words[0] = fr"venv\Scripts\{words[0]}.exe"
1678-
else:
1679-
words[0] = fr"venv/bin/{words[0]}"
1680-
status, output = run_command(" ".join(words))
1681-
assert status == 0
1682-
return output
1683-
1684-
1685-
@pytest.fixture(scope="session", name="venv_world")
1686-
def venv_world_fixture(tmp_path_factory):
1687-
"""Create a virtualenv with a few test packages for VirtualenvTest to use.
1688-
1689-
Returns the directory containing the "venv" virtualenv.
1690-
"""
1691-
1692-
venv_world = tmp_path_factory.mktemp("venv_world")
1693-
with change_dir(venv_world):
1694-
# Create a virtualenv.
1695-
run_command("python -m venv venv")
1696-
1697-
# A third-party package that installs a few different packages.
1698-
make_file("third_pkg/third/__init__.py", """\
1699-
import fourth
1700-
def third(x):
1701-
return 3 * x
1702-
""")
1703-
# Use plugin2.py as third.plugin
1704-
with open(os.path.join(os.path.dirname(__file__), "plugin2.py")) as f:
1705-
make_file("third_pkg/third/plugin.py", f.read())
1706-
# A render function for plugin2 to use for dynamic file names.
1707-
make_file("third_pkg/third/render.py", """\
1708-
def render(filename, linenum):
1709-
return "HTML: {}@{}".format(filename, linenum)
1710-
""")
1711-
# Another package that third can use.
1712-
make_file("third_pkg/fourth/__init__.py", """\
1713-
def fourth(x):
1714-
return 4 * x
1715-
""")
1716-
# Some namespace packages.
1717-
make_file("third_pkg/nspkg/fifth/__init__.py", """\
1718-
def fifth(x):
1719-
return 5 * x
1720-
""")
1721-
# The setup.py to install everything.
1722-
make_file("third_pkg/setup.py", """\
1723-
import setuptools
1724-
setuptools.setup(
1725-
name="third",
1726-
packages=["third", "fourth", "nspkg.fifth"],
1727-
)
1728-
""")
1729-
1730-
# Some namespace packages.
1731-
make_file("another_pkg/nspkg/sixth/__init__.py", """\
1732-
def sixth(x):
1733-
return 6 * x
1734-
""")
1735-
# The setup.py to install everything.
1736-
make_file("another_pkg/setup.py", """\
1737-
import setuptools
1738-
setuptools.setup(
1739-
name="another",
1740-
packages=["nspkg.sixth"],
1741-
)
1742-
""")
1743-
1744-
# Install the third-party packages.
1745-
run_in_venv("python -m pip install --no-index ./third_pkg")
1746-
run_in_venv("python -m pip install --no-index -e ./another_pkg")
1747-
shutil.rmtree("third_pkg")
1748-
1749-
# Install coverage.
1750-
coverage_src = nice_file(TESTS_DIR, "..")
1751-
run_in_venv(f"python -m pip install --no-index {coverage_src}")
1752-
1753-
return venv_world
1754-
1755-
1756-
@pytest.fixture(params=[
1757-
"coverage",
1758-
"python -m coverage",
1759-
], name="coverage_command")
1760-
def coverage_command_fixture(request):
1761-
"""Parametrized fixture to use multiple forms of "coverage" command."""
1762-
return request.param
1763-
1764-
1765-
class VirtualenvTest(CoverageTest):
1766-
"""Tests of virtualenv considerations."""
1767-
1768-
expected_stdout = "33\n110\n198\n1.5\n"
1769-
1770-
@pytest.fixture(autouse=True)
1771-
def in_venv_world_fixture(self, venv_world):
1772-
"""For running tests inside venv_world, and cleaning up made files."""
1773-
with change_dir(venv_world):
1774-
self.make_file("myproduct.py", """\
1775-
import colorsys
1776-
import third
1777-
import nspkg.fifth
1778-
import nspkg.sixth
1779-
print(third.third(11))
1780-
print(nspkg.fifth.fifth(22))
1781-
print(nspkg.sixth.sixth(33))
1782-
print(sum(colorsys.rgb_to_hls(1, 0, 0)))
1783-
""")
1784-
1785-
self.del_environ("COVERAGE_TESTING") # To avoid needing contracts installed.
1786-
self.set_environ("COVERAGE_DEBUG_FILE", "debug_out.txt")
1787-
self.set_environ("COVERAGE_DEBUG", "trace")
1788-
1789-
yield
1790-
1791-
for fname in os.listdir("."):
1792-
if fname not in {"venv", "another_pkg"}:
1793-
os.remove(fname)
1794-
1795-
def get_trace_output(self):
1796-
"""Get the debug output of coverage.py"""
1797-
with open("debug_out.txt") as f:
1798-
return f.read()
1799-
1800-
def test_third_party_venv_isnt_measured(self, coverage_command):
1801-
out = run_in_venv(coverage_command + " run --source=. myproduct.py")
1802-
# In particular, this warning doesn't appear:
1803-
# Already imported a file that will be measured: .../coverage/__main__.py
1804-
assert out == self.expected_stdout
1805-
1806-
# Check that our tracing was accurate. Files are mentioned because
1807-
# --source refers to a file.
1808-
debug_out = self.get_trace_output()
1809-
assert re_lines(
1810-
r"^Not tracing .*\bexecfile.py': inside --source, but is third-party",
1811-
debug_out,
1812-
)
1813-
assert re_lines(r"^Tracing .*\bmyproduct.py", debug_out)
1814-
assert re_lines(
1815-
r"^Not tracing .*\bcolorsys.py': falls outside the --source spec",
1816-
debug_out,
1817-
)
1818-
1819-
out = run_in_venv("python -m coverage report")
1820-
assert "myproduct.py" in out
1821-
assert "third" not in out
1822-
assert "coverage" not in out
1823-
assert "colorsys" not in out
1824-
1825-
def test_us_in_venv_isnt_measured(self, coverage_command):
1826-
out = run_in_venv(coverage_command + " run --source=third myproduct.py")
1827-
assert out == self.expected_stdout
1828-
1829-
# Check that our tracing was accurate. Modules are mentioned because
1830-
# --source refers to a module.
1831-
debug_out = self.get_trace_output()
1832-
assert re_lines(
1833-
r"^Not tracing .*\bexecfile.py': " +
1834-
"module 'coverage.execfile' falls outside the --source spec",
1835-
debug_out,
1836-
)
1837-
assert re_lines(
1838-
r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec",
1839-
debug_out,
1840-
)
1841-
assert re_lines(
1842-
r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec",
1843-
debug_out,
1844-
)
1845-
1846-
out = run_in_venv("python -m coverage report")
1847-
assert "myproduct.py" not in out
1848-
assert "third" in out
1849-
assert "coverage" not in out
1850-
assert "colorsys" not in out
1851-
1852-
def test_venv_isnt_measured(self, coverage_command):
1853-
out = run_in_venv(coverage_command + " run myproduct.py")
1854-
assert out == self.expected_stdout
1855-
1856-
debug_out = self.get_trace_output()
1857-
assert re_lines(r"^Not tracing .*\bexecfile.py': is part of coverage.py", debug_out)
1858-
assert re_lines(r"^Tracing .*\bmyproduct.py", debug_out)
1859-
assert re_lines(r"^Not tracing .*\bcolorsys.py': is in the stdlib", debug_out)
1860-
1861-
out = run_in_venv("python -m coverage report")
1862-
assert "myproduct.py" in out
1863-
assert "third" not in out
1864-
assert "coverage" not in out
1865-
assert "colorsys" not in out
1866-
1867-
@pytest.mark.skipif(not env.C_TRACER, reason="Plugins are only supported with the C tracer.")
1868-
def test_venv_with_dynamic_plugin(self, coverage_command):
1869-
# https://github.com/nedbat/coveragepy/issues/1150
1870-
# Django coverage plugin was incorrectly getting warnings:
1871-
# "Already imported: ... django/template/blah.py"
1872-
# It happened because coverage imported the plugin, which imported
1873-
# Django, and then the Django files were reported as traceable.
1874-
self.make_file(".coveragerc", "[run]\nplugins=third.plugin\n")
1875-
self.make_file("myrender.py", """\
1876-
import third.render
1877-
print(third.render.render("hello.html", 1723))
1878-
""")
1879-
out = run_in_venv(coverage_command + " run --source=. myrender.py")
1880-
# The output should not have this warning:
1881-
# Already imported a file that will be measured: ...third/render.py (already-imported)
1882-
assert out == "HTML: hello.html@1723\n"
1883-
1884-
def test_installed_namespace_packages(self, coverage_command):
1885-
# https://github.com/nedbat/coveragepy/issues/1231
1886-
# When namespace packages were installed, they were considered
1887-
# third-party packages. Test that isn't still happening.
1888-
out = run_in_venv(coverage_command + " run --source=nspkg myproduct.py")
1889-
# In particular, this warning doesn't appear:
1890-
# Already imported a file that will be measured: .../coverage/__main__.py
1891-
assert out == self.expected_stdout
1892-
1893-
# Check that our tracing was accurate. Files are mentioned because
1894-
# --source refers to a file.
1895-
debug_out = self.get_trace_output()
1896-
assert re_lines(
1897-
r"^Not tracing .*\bexecfile.py': " +
1898-
"module 'coverage.execfile' falls outside the --source spec",
1899-
debug_out,
1900-
)
1901-
assert re_lines(
1902-
r"^Not tracing .*\bmyproduct.py': module 'myproduct' falls outside the --source spec",
1903-
debug_out,
1904-
)
1905-
assert re_lines(
1906-
r"^Not tracing .*\bcolorsys.py': module 'colorsys' falls outside the --source spec",
1907-
debug_out,
1908-
)
1909-
1910-
out = run_in_venv("python -m coverage report")
1911-
1912-
# Name Stmts Miss Cover
1913-
# ------------------------------------------------------------------------------
1914-
# another_pkg/nspkg/sixth/__init__.py 2 0 100%
1915-
# venv/lib/python3.9/site-packages/nspkg/fifth/__init__.py 2 0 100%
1916-
# ------------------------------------------------------------------------------
1917-
# TOTAL 4 0 100%
1918-
1919-
assert "myproduct.py" not in out
1920-
assert "third" not in out
1921-
assert "coverage" not in out
1922-
assert "colorsys" not in out
1923-
assert "fifth" in out
1924-
assert "sixth" in out

0 commit comments

Comments
 (0)