@@ -1643,30 +1643,33 @@ def test_script_pkg_sub(self):
1643
1643
self .assert_pth_and_source_work_together ('' , 'pkg' , 'sub' )
1644
1644
1645
1645
1646
- def run_in_venv (args ):
1647
- """Run python with `args` in the "venv" virtualenv.
1646
+ def run_in_venv (cmd ):
1647
+ r"""Run `cmd` in the virtualenv at `venv`.
1648
+
1649
+ The first word of the command will be adjusted to run it from the
1650
+ venv/bin or venv\Scripts directory.
1648
1651
1649
1652
Returns the text output of the command.
1650
1653
"""
1654
+ words = cmd .split ()
1651
1655
if env .WINDOWS :
1652
- cmd = r".\venv\ Scripts\python .exe "
1656
+ words [ 0 ] = r"{}\ Scripts\{} .exe" . format ( "venv" , words [ 0 ])
1653
1657
else :
1654
- cmd = "./venv/bin/python "
1655
- cmd += args
1656
- status , output = run_command (cmd )
1657
- print (output )
1658
+ words [0 ] = "{}/bin/{}" .format ("venv" , words [0 ])
1659
+ status , output = run_command (" " .join (words ))
1658
1660
assert status == 0
1659
1661
return output
1660
1662
1661
1663
1662
- @pytest .fixture (scope = "session" , name = "venv_factory " )
1663
- def venv_factory_fixture (tmp_path_factory ):
1664
- """Produce a function which can copy a venv template to a new directory .
1664
+ @pytest .fixture (scope = "session" , name = "venv_world " )
1665
+ def venv_world_fixture (tmp_path_factory ):
1666
+ """Create a virtualenv with a few test packages for VirtualenvTest to use .
1665
1667
1666
- The function accepts one argument, the directory to use for the venv.
1668
+ Returns the directory containing the " venv" virtualenv .
1667
1669
"""
1668
- tmpdir = tmp_path_factory .mktemp ("venv_template" )
1669
- with change_dir (str (tmpdir )):
1670
+
1671
+ venv_world = tmp_path_factory .mktemp ("venv_world" )
1672
+ with change_dir (venv_world ):
1670
1673
# Create a virtualenv.
1671
1674
run_command ("python -m virtualenv venv" )
1672
1675
@@ -1686,55 +1689,119 @@ def fourth(x):
1686
1689
""" )
1687
1690
1688
1691
# Install the third-party packages.
1689
- run_in_venv ("-m pip install --no-index ./third_pkg" )
1692
+ run_in_venv ("python -m pip install --no-index ./third_pkg" )
1693
+ shutil .rmtree ("third_pkg" )
1690
1694
1691
1695
# Install coverage.
1692
1696
coverage_src = nice_file (TESTS_DIR , ".." )
1693
- run_in_venv ("-m pip install --no-index {}" .format (coverage_src ))
1697
+ run_in_venv ("python -m pip install --no-index {}" .format (coverage_src ))
1694
1698
1695
- def factory (dst ):
1696
- """The venv factory function.
1699
+ return venv_world
1697
1700
1698
- Copies the venv template to `dst`.
1699
- """
1700
- shutil .copytree (str (tmpdir / "venv" ), dst , symlinks = (not env .WINDOWS ))
1701
1701
1702
- return factory
1702
+ @pytest .fixture (params = [
1703
+ "coverage" ,
1704
+ "python -m coverage" ,
1705
+ ], name = "coverage_command" )
1706
+ def coverage_command_fixture (request ):
1707
+ """Parametrized fixture to use multiple forms of "coverage" command."""
1708
+ return request .param
1703
1709
1704
1710
1705
1711
class VirtualenvTest (CoverageTest ):
1706
1712
"""Tests of virtualenv considerations."""
1707
1713
1708
- def setup_test (self ):
1709
- self .make_file ("myproduct.py" , """\
1710
- import third
1711
- print(third.third(11))
1712
- """ )
1713
- self .del_environ ("COVERAGE_TESTING" ) # To avoid needing contracts installed.
1714
- super (VirtualenvTest , self ).setup_test ()
1715
-
1716
- def test_third_party_venv_isnt_measured (self , venv_factory ):
1717
- venv_factory ("venv" )
1718
- out = run_in_venv ("-m coverage run --source=. myproduct.py" )
1714
+ @pytest .fixture (autouse = True )
1715
+ def in_venv_world_fixture (self , venv_world ):
1716
+ """For running tests inside venv_world, and cleaning up made files."""
1717
+ with change_dir (venv_world ):
1718
+ self .make_file ("myproduct.py" , """\
1719
+ import colorsys
1720
+ import third
1721
+ print(third.third(11))
1722
+ print(sum(colorsys.rgb_to_hls(1, 0, 0)))
1723
+ """ )
1724
+ self .expected_stdout = "33\n 1.5\n " # pylint: disable=attribute-defined-outside-init
1725
+
1726
+ self .del_environ ("COVERAGE_TESTING" ) # To avoid needing contracts installed.
1727
+ self .set_environ ("COVERAGE_DEBUG_FILE" , "debug_out.txt" )
1728
+ self .set_environ ("COVERAGE_DEBUG" , "trace" )
1729
+
1730
+ yield
1731
+
1732
+ for fname in os .listdir ("." ):
1733
+ if fname != "venv" :
1734
+ os .remove (fname )
1735
+
1736
+ def get_trace_output (self ):
1737
+ """Get the debug output of coverage.py"""
1738
+ with open ("debug_out.txt" ) as f :
1739
+ return f .read ()
1740
+
1741
+ def test_third_party_venv_isnt_measured (self , coverage_command ):
1742
+ out = run_in_venv (coverage_command + " run --source=. myproduct.py" )
1719
1743
# In particular, this warning doesn't appear:
1720
1744
# Already imported a file that will be measured: .../coverage/__main__.py
1721
- assert out == "33\n "
1722
- out = run_in_venv ("-m coverage report" )
1745
+ assert out == self .expected_stdout
1746
+
1747
+ # Check that our tracing was accurate. Files are mentioned because
1748
+ # --source refers to a file.
1749
+ debug_out = self .get_trace_output ()
1750
+ assert re_lines (
1751
+ debug_out ,
1752
+ r"^Not tracing .*\bexecfile.py': inside --source, but is part of coverage.py"
1753
+ )
1754
+ assert re_lines (debug_out , r"^Tracing .*\bmyproduct.py" )
1755
+ assert re_lines (
1756
+ debug_out ,
1757
+ r"^Not tracing .*\bcolorsys.py': falls outside the --source spec"
1758
+ )
1759
+
1760
+ out = run_in_venv ("python -m coverage report" )
1723
1761
assert "myproduct.py" in out
1724
1762
assert "third" not in out
1763
+ assert "coverage" not in out
1764
+ assert "colorsys" not in out
1765
+
1766
+ def test_us_in_venv_isnt_measured (self , coverage_command ):
1767
+ out = run_in_venv (coverage_command + " run --source=third myproduct.py" )
1768
+ assert out == self .expected_stdout
1769
+
1770
+ # Check that our tracing was accurate. Modules are mentioned because
1771
+ # --source refers to a module.
1772
+ debug_out = self .get_trace_output ()
1773
+ assert re_lines (
1774
+ debug_out ,
1775
+ r"^Not tracing .*\bexecfile.py': " +
1776
+ "module 'coverage.execfile' falls outside the --source spec"
1777
+ )
1778
+ print (re_lines (debug_out , "myproduct" ))
1779
+ assert re_lines (
1780
+ debug_out ,
1781
+ r"^Not tracing .*\bmyproduct.py': module u?'myproduct' falls outside the --source spec"
1782
+ )
1783
+ assert re_lines (
1784
+ debug_out ,
1785
+ r"^Not tracing .*\bcolorsys.py': module u?'colorsys' falls outside the --source spec"
1786
+ )
1725
1787
1726
- def test_us_in_venv_is_measured (self , venv_factory ):
1727
- venv_factory ("venv" )
1728
- out = run_in_venv ("-m coverage run --source=third myproduct.py" )
1729
- assert out == "33\n "
1730
- out = run_in_venv ("-m coverage report" )
1788
+ out = run_in_venv ("python -m coverage report" )
1731
1789
assert "myproduct.py" not in out
1732
1790
assert "third" in out
1791
+ assert "coverage" not in out
1792
+ assert "colorsys" not in out
1793
+
1794
+ def test_venv_isnt_measured (self , coverage_command ):
1795
+ out = run_in_venv (coverage_command + " run myproduct.py" )
1796
+ assert out == self .expected_stdout
1797
+
1798
+ debug_out = self .get_trace_output ()
1799
+ assert re_lines (debug_out , r"^Not tracing .*\bexecfile.py': is part of coverage.py" )
1800
+ assert re_lines (debug_out , r"^Tracing .*\bmyproduct.py" )
1801
+ assert re_lines (debug_out , r"^Not tracing .*\bcolorsys.py': is in the stdlib" )
1733
1802
1734
- def test_venv_isnt_measured (self , venv_factory ):
1735
- venv_factory ("venv" )
1736
- out = run_in_venv ("-m coverage run myproduct.py" )
1737
- assert out == "33\n "
1738
- out = run_in_venv ("-m coverage report" )
1803
+ out = run_in_venv ("python -m coverage report" )
1739
1804
assert "myproduct.py" in out
1740
1805
assert "third" not in out
1806
+ assert "coverage" not in out
1807
+ assert "colorsys" not in out
0 commit comments