Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More elegant tempdirs.

  • Loading branch information...
commit 7223e54908c61a73aed4af9dc3c50be636fea4b2 1 parent e651670
@cortesi authored
Showing with 33 additions and 23 deletions.
  1. +17 −16 libpry/test.py
  2. +1 −1  pry
  3. +15 −6 test/test_test.py
View
33 libpry/test.py
@@ -45,22 +45,6 @@ def raises(exc, obj, *args, **kwargs):
raise AssertionError("No exception raised.")
-class TmpDirMixin:
- """
- A utility mixin that creates a temporary directory during setup, and
- removes it during teardown. The directory path is inserted into the
- test namespace as follows:
-
- self["tmpdir"] = path
- """
- def setUp(self):
- self["tmpdir"] = tempfile.mkdtemp()
-
- def tearDown(self):
- if os.path.isdir(self["tmpdir"]):
- shutil.rmtree(self["tmpdir"])
-
-
class _Error:
def __init__(self, node, msg):
self.node, self.msg = node, msg
@@ -631,13 +615,28 @@ def _states(self):
self.tearDownState,
]
+ def tmpdir(self):
+ f = tempfile.mkdtemp()
+ self._tmpDirs.append(f)
+ return f
+
+ def _rmdirs(self, l):
+ for i in l:
+ if os.path.isdir(i):
+ shutil.rmtree(i)
+ l[:] = []
+
def _run(self, output, repeat, profile):
"""
Run the tests contained in this suite.
"""
+ # Temp dirs requested during setUpAll, and during a specific test
+ allTmpDirs, oneTmpDirs = [], []
+ self._tmpDirs = allTmpDirs
if self._runCallable(self.setUpAll, self, "setUpAll", 1, None):
output.setUpAllError(self)
return
+ self._tmpDirs = oneTmpDirs
for i in self.children:
output.nodePre(i)
@@ -655,12 +654,14 @@ def _run(self, output, repeat, profile):
output.tearDownError(i)
output.nodePost(i)
return
+ self._rmdirs(oneTmpDirs)
output.nodePost(i)
if self._runCallable(self.tearDownAll, self, "tearDownAll", 1, None):
output.tearDownAllError(self)
return
+ self._rmdirs(allTmpDirs)
class AutoTree(TestContainer):
View
2  pry
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.5
+#!/usr/bin/env python
import sys
import libpry
View
21 test/test_test.py
@@ -30,13 +30,22 @@ def test_noexception(self):
)
-class uTmpDirMixin(libpry.test.TmpDirMixin, libpry.test.AutoTree):
- def tearDown(self):
- libpry.test.TmpDirMixin.tearDown(self)
- assert not os.path.isdir(self["tmpdir"])
+class uTmpDir(libpry.test.AutoTree):
+ def setUpAll(self):
+ self.d = []
+ self.d.append(self.tmpdir())
+
+ def setUp(self):
+ self.d.append(self.tmpdir())
+
+ def tearDownAll(self):
+ assert os.path.isdir(self.d[0])
+ assert not os.path.isdir(self.d[1])
def test_check(self):
- assert os.path.isdir(self["tmpdir"])
+ assert len(self.d) == 2
+ assert os.path.isdir(self.d[0])
+ assert os.path.isdir(self.d[1])
class TSetupCheckRoot(libpry.test.AutoTree):
@@ -570,5 +579,5 @@ def x(): pass
uAutoTree(),
u_Output(),
uCallableNode(),
- uTmpDirMixin(),
+ uTmpDir(),
]
Please sign in to comment.
Something went wrong with that request. Please try again.