Commits

another_thomas committed c11cf9d

switched to shutil.rmtree as the os.walk-based implementation failed on symbolic links

Comments (0)

Files changed (3)

requirements_test.txt

+mock==1.0b1
+pylint==0.25.2
 import tempfile
-import os
+import shutil
+
 class TempDir(object):
     """ class for temporary directories
 creates a (named) directory which is deleted after use.
 All files created within the directory are destroyed
 Might not work on windows when the files are still opened
 """
-    def __init__(self, suffix="", prefix="tmp", dir=None):
-        self.name=tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
+    def __init__(self, suffix="", prefix="tmp", basedir=None):
+        self.name = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=basedir)
 
     def __del__(self):
         if "name" in self.__dict__:
         return self.dissolve()
 
     def dissolve(self):
+        """remove all files and directories created within the tempdir"""
         if self.name:
-            for (dir, dirs, files) in os.walk(self.name, topdown=False):
-                for f in files:
-                    os.unlink(os.path.join(dir, f))
-                os.rmdir(dir)
+            shutil.rmtree(self.name)
         self.name = ""
 
     def __str__(self):
         if self.name:
-            return "temporary directory at: %s"%(self.name,)
+            return "temporary directory at: %s" % (self.name,)
         else:
             return "dissolved temporary directory"
 
+"""test cases for tempdir"""
 from __future__ import with_statement
 
 import os
-import sys
 import tempdir
 from mock import Mock
 try:
     import unittest2 as unittest
-except:
+except ImportError:
     import unittest
-#from nose.tools import raises, assert_true
 
 class TestTempDir(unittest.TestCase):
     temp_name = "dirname"
 
-    def _setup_mock_os(self, nested = []):
-        if nested:
-            tempdir.os.walk = Mock(return_value=nested)
-        else:
-            tempdir.os.walk = Mock(return_value=[(self.tmp_name, [], [])])
+    def _setup_mock_shutil(self):
+        tempdir.shutil.rmtree = Mock()
  
     def _setup_mock_allow_init(self, name=None):
         tempdir.tempfile = Mock(return_value=self.tmp_name)
-        self._setup_mock_os()
+        self._setup_mock_shutil()
         tempdir.os.rmdir(self.tmp_name)
 
     def _setup_mock_throw_in_init(self, name=None):
         tempdir.tempfile.mkdtemp = Mock(side_effect=Exception)
 
-    def _setup_mock_with_nested_file(self):
-        tempdir.tempfile.mkdtemp = Mock(return_value=self.tmp_name)
-        self._setup_mock_os(nested=[(self.tmp_name, [], ["foo"])])
-
-    def _setup_mock_with_nested_dir(self, name=None):
-        tempdir.tempfile.mkdtemp = Mock(return_value=self.tmp_name)
-        inner_dir = os.path.join(self.tmp_name,"foodir")
-        dirs = [inner_dir, self.tmp_name]
-        self._setup_mock_os(nested=[(d, [], []) for d in dirs])
-
     def setUp(self):
         tempdir.os = Mock()
         tempdir.os.path = os.path
         t.__del__()
 
     def use_tempdir(self, name=None):
-        with tempdir.TempDir(dir=name) as t:
+        with tempdir.TempDir(basedir=name):
             pass
 
     def test_context_use(self):
         with self.assertRaises(Exception):
             self.use_tempdir()
 
-    def test_cleanup_files(self):
-        self._setup_mock_with_nested_file()
-        self.use_tempdir()
-
-    def test_cleanup_dirs(self):
-        self._setup_mock_with_nested_dir()
-        self.use_tempdir()
-
     def test_named_tempdir(self):
         self.tmp_name = "foo"
         self._setup_mock_allow_init(name="foo")