Skip to content

Commit

Permalink
test(fix): keep SourceIncludeOmitTest's from clobbering each other
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nedbat committed Nov 24, 2021
1 parent 0865590 commit eff683c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
3 changes: 3 additions & 0 deletions coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ def debug(msg):
if self.debug:
self.debug.write(msg)

# Generally useful information
debug("sys.path:" + "".join(f"\n {p}" for p in sys.path))

# Create the matchers we need for should_trace
if self.source or self.source_pkgs:
against = []
Expand Down
28 changes: 20 additions & 8 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,17 @@ def test_omit_and_include(self):
class SourceIncludeOmitTest(IncludeOmitTestsMixin, CoverageTest):
"""Test using `source`, `include`, and `omit` when measuring code."""

def setUp(self):
super().setUp()

# These tests use the TESTS_DIR/modules files, but they cd into it. To
# keep tests from cross-contaminating, we make a copy of the files.
# Since we need to import from there, we also add it to the beginning
# of sys.path.

shutil.copytree(nice_file(TESTS_DIR, "modules"), "tests_dir_modules")
sys.path.insert(0, abs_file("tests_dir_modules"))

def coverage_usepkgs(self, **kwargs):
"""Run coverage on usepkgs and return the line summary.
Expand All @@ -867,7 +878,8 @@ def coverage_usepkgs(self, **kwargs):
cov.start()
import usepkgs # pragma: nested # pylint: disable=import-error, unused-import
cov.stop() # pragma: nested
data = cov.get_data()
with self.assert_warnings(cov, []):
data = cov.get_data()
summary = line_counts(data)
for k, v in list(summary.items()):
assert k.endswith(".py")
Expand All @@ -889,7 +901,7 @@ def test_source_package_as_package(self):
assert lines['p1c'] == 0

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

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

def test_ambiguous_source_package_as_dir(self):
# pkg1 is a directory and a pkg, since we cd into tests/modules/ambiguous
os.chdir(nice_file(TESTS_DIR, "modules", "ambiguous"))
# pkg1 defaults to directory because tests/modules/ambiguous/pkg1 exists
# pkg1 is a directory and a pkg, since we cd into tests_dir_modules/ambiguous
os.chdir("tests_dir_modules/ambiguous")
# pkg1 defaults to directory because tests_dir_modules/ambiguous/pkg1 exists
lines = self.coverage_usepkgs(source=["pkg1"])
self.filenames_in(lines, "ambiguous")
self.filenames_not_in(lines, "p1a p1b p1c")

def test_ambiguous_source_package_as_package(self):
# pkg1 is a directory and a pkg, since we cd into tests/modules/ambiguous
os.chdir(nice_file(TESTS_DIR, "modules", "ambiguous"))
# pkg1 is a directory and a pkg, since we cd into tests_dir_modules/ambiguous
os.chdir("tests_dir_modules/ambiguous")
lines = self.coverage_usepkgs(source_pkgs=["pkg1"])
self.filenames_in(lines, "p1a p1b")
self.filenames_not_in(lines, "p2a p2b othera otherb osa osb ambiguous")
Expand Down

0 comments on commit eff683c

Please sign in to comment.