summaryrefslogtreecommitdiff
path: root/tests/test_process.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2014-11-27 11:04:13 -0500
committerNed Batchelder <ned@nedbatchelder.com>2014-11-27 11:04:13 -0500
commit483a3700cca817c37192979499b155a9c1200ad6 (patch)
tree9a3133a17987d77cc96d864bcb57efa1b39ec7fc /tests/test_process.py
parent1fa418c6c1c33117308e3d94f18f8fc1a9193393 (diff)
downloadpython-coveragepy-git-483a3700cca817c37192979499b155a9c1200ad6.tar.gz
A better way to find places to write .pth files for the tests? #339.
Diffstat (limited to 'tests/test_process.py')
-rw-r--r--tests/test_process.py35
1 files changed, 24 insertions, 11 deletions
diff --git a/tests/test_process.py b/tests/test_process.py
index 1185c25c..e13ec955 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -664,23 +664,36 @@ class FailUnderTest(CoverageTest):
self.assertEqual(st, 2)
+def possible_pth_dirs():
+ """Produce a sequence of directories for trying to write .pth files."""
+ # First look through sys.path, and we find a .pth file, then it's a good
+ # place to put ours.
+ for d in sys.path:
+ g = glob.glob(os.path.join(d, "*.pth"))
+ if g:
+ yield d
+
+ # If we're still looking, then try the Python library directory.
+ # https://bitbucket.org/ned/coveragepy/issue/339/pth-test-malfunctions
+ import distutils.sysconfig
+ yield distutils.sysconfig.get_python_lib()
+
+
class ProcessCoverageMixin(object):
"""Set up a .pth file that causes all sub-processes to be coverage'd"""
def setUp(self):
super(ProcessCoverageMixin, self).setUp()
# Find a place to put a .pth file.
pth_contents = "import coverage; coverage.process_startup()\n"
- for d in sys.path: # pragma: part covered
- g = glob.glob(os.path.join(d, "*.pth"))
- if g:
- pth_path = os.path.join(d, "subcover.pth")
- with open(pth_path, "w") as pth:
- try:
- pth.write(pth_contents)
- self.pth_path = pth_path
- break
- except (IOError, OSError): # pragma: not covered
- pass
+ for pth_dir in possible_pth_dirs(): # pragma: part covered
+ pth_path = os.path.join(pth_dir, "subcover.pth")
+ with open(pth_path, "w") as pth:
+ try:
+ pth.write(pth_contents)
+ self.pth_path = pth_path
+ break
+ except (IOError, OSError): # pragma: not covered
+ pass
else: # pragma: not covered
raise Exception("Couldn't find a place for the .pth file")