Skip to content

Commit eff683c

Browse files
committed
test(fix): keep SourceIncludeOmitTest's from clobbering each other
Because they cd'd into a folder in the shared source tree, their data files would collide, leading to flaky tests. Also, add a check that the tests collected some data, and add sys.path to the debug=trace output.
1 parent 0865590 commit eff683c

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

coverage/inorout.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ def debug(msg):
243243
if self.debug:
244244
self.debug.write(msg)
245245

246+
# Generally useful information
247+
debug("sys.path:" + "".join(f"\n {p}" for p in sys.path))
248+
246249
# Create the matchers we need for should_trace
247250
if self.source or self.source_pkgs:
248251
against = []

tests/test_api.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,17 @@ def test_omit_and_include(self):
857857
class SourceIncludeOmitTest(IncludeOmitTestsMixin, CoverageTest):
858858
"""Test using `source`, `include`, and `omit` when measuring code."""
859859

860+
def setUp(self):
861+
super().setUp()
862+
863+
# These tests use the TESTS_DIR/modules files, but they cd into it. To
864+
# keep tests from cross-contaminating, we make a copy of the files.
865+
# Since we need to import from there, we also add it to the beginning
866+
# of sys.path.
867+
868+
shutil.copytree(nice_file(TESTS_DIR, "modules"), "tests_dir_modules")
869+
sys.path.insert(0, abs_file("tests_dir_modules"))
870+
860871
def coverage_usepkgs(self, **kwargs):
861872
"""Run coverage on usepkgs and return the line summary.
862873
@@ -867,7 +878,8 @@ def coverage_usepkgs(self, **kwargs):
867878
cov.start()
868879
import usepkgs # pragma: nested # pylint: disable=import-error, unused-import
869880
cov.stop() # pragma: nested
870-
data = cov.get_data()
881+
with self.assert_warnings(cov, []):
882+
data = cov.get_data()
871883
summary = line_counts(data)
872884
for k, v in list(summary.items()):
873885
assert k.endswith(".py")
@@ -889,7 +901,7 @@ def test_source_package_as_package(self):
889901
assert lines['p1c'] == 0
890902

891903
def test_source_package_as_dir(self):
892-
os.chdir(nice_file(TESTS_DIR, "modules"))
904+
os.chdir("tests_dir_modules")
893905
assert os.path.isdir("pkg1")
894906
lines = self.coverage_usepkgs(source=["pkg1"])
895907
self.filenames_in(lines, "p1a p1b")
@@ -915,7 +927,7 @@ def test_source_package_part_omitted(self):
915927
# the search for unexecuted files, and given a score of 0%.
916928

917929
# The omit arg is by path, so need to be in the modules directory.
918-
os.chdir(nice_file(TESTS_DIR, "modules"))
930+
os.chdir("tests_dir_modules")
919931
lines = self.coverage_usepkgs(source=["pkg1"], omit=["pkg1/p1b.py"])
920932
self.filenames_in(lines, "p1a")
921933
self.filenames_not_in(lines, "p1b")
@@ -929,16 +941,16 @@ def test_source_package_as_package_part_omitted(self):
929941
assert lines['p1c'] == 0
930942

931943
def test_ambiguous_source_package_as_dir(self):
932-
# pkg1 is a directory and a pkg, since we cd into tests/modules/ambiguous
933-
os.chdir(nice_file(TESTS_DIR, "modules", "ambiguous"))
934-
# pkg1 defaults to directory because tests/modules/ambiguous/pkg1 exists
944+
# pkg1 is a directory and a pkg, since we cd into tests_dir_modules/ambiguous
945+
os.chdir("tests_dir_modules/ambiguous")
946+
# pkg1 defaults to directory because tests_dir_modules/ambiguous/pkg1 exists
935947
lines = self.coverage_usepkgs(source=["pkg1"])
936948
self.filenames_in(lines, "ambiguous")
937949
self.filenames_not_in(lines, "p1a p1b p1c")
938950

939951
def test_ambiguous_source_package_as_package(self):
940-
# pkg1 is a directory and a pkg, since we cd into tests/modules/ambiguous
941-
os.chdir(nice_file(TESTS_DIR, "modules", "ambiguous"))
952+
# pkg1 is a directory and a pkg, since we cd into tests_dir_modules/ambiguous
953+
os.chdir("tests_dir_modules/ambiguous")
942954
lines = self.coverage_usepkgs(source_pkgs=["pkg1"])
943955
self.filenames_in(lines, "p1a p1b")
944956
self.filenames_not_in(lines, "p2a p2b othera otherb osa osb ambiguous")

0 commit comments

Comments
 (0)