diff test/filesupport.py @ 185:e1c7cd896310

Rework test/filesupport.py so that test cases always use their own temporary directory. Before, the test cases had to ensure this themselves. Adapt the test cases.
author Bernhard Herzog <bh@intevation.de>
date Thu, 26 Jun 2008 16:23:26 +0000
parents 007d7f2aa184
children e3ab8aca2b08
line wrap: on
line diff
--- a/test/filesupport.py	Wed Jun 25 15:15:30 2008 +0000
+++ b/test/filesupport.py	Thu Jun 26 16:23:26 2008 +0000
@@ -33,42 +33,54 @@
 
 class FileTestMixin:
 
-    """Mixin class for tests that use files in the temporary directory
+    """Mixin class for tests that use temporary files.
+
+    Instances of this class create a test-specific sub-directory under
+    the main test temp directory.  The name of the sub-directory is the
+    return value of the test's id() method.
     """
 
-    def temp_file_name(self, basename, remove=False):
-        """Returns the full name of the file named basename in the temp. dir.
-        If the remove parameter is true, the file is removed if already exists.
+    _test_specific_directory_created = False
+
+    def create_test_specific_temp_dir(self):
+        """Creates the test specific directory and returns its absolute name.
+        When this method is called for the first time and the directory
+        exists, if is removed, so that a specific test instance always
+        starts with an empty directory.
         """
-        filename = os.path.join(create_temp_dir(), basename)
-        if remove and os.path.exists(filename):
-            os.remove(filename)
-        return filename
+        dirname = os.path.join(create_temp_dir(), self.id())
+        if not self._test_specific_directory_created:
+            if os.path.exists(dirname):
+                shutil.rmtree(dirname)
+            os.mkdir(dirname)
+            self._test_specific_directory_created = True
+        return dirname
 
-    def create_temp_file(self, basename, contents, mode = None):
+    def temp_file_name(self, basename):
+        """Returns the full name of the file named basename in the temp. dir.
+        """
+        return os.path.join(self.create_test_specific_temp_dir(), basename)
+
+    def create_temp_file(self, basename, contents, permissions=None):
         """Creates a file in the temp directory with the given contents.
-        The optional parameter mode should either be None (the default)
-        or an int specifying the file permissions (same format as the
-        second parameter of os.chmod).  The method returns the absolute
-        name of the created file.
+        The optional parameter permissions should either be None (the
+        default) or an int specifying the file permissions (same format
+        as the second parameter of os.chmod).  The method returns the
+        absolute name of the created file.
         """
         filename = self.temp_file_name(basename)
         file = open(filename, "w")
         file.write(contents)
         file.close()
-        if mode is not None:
-            os.chmod(filename, mode)
+        if permissions is not None:
+            os.chmod(filename, permissions)
         return filename
 
-    def create_temp_dir(self, basename, remove=True):
+    def create_temp_dir(self, basename):
         """Creates the directory basename in the temporary directory.
-        If the optional parameter remove is true (the default), the
-        directory and all its contents are deleted with shutil.rmtree.
         The method returns the absolute name of the created directory.
         """
-        dirname = os.path.join(create_temp_dir(), basename)
-        if remove and os.path.exists(dirname):
-            shutil.rmtree(dirname)
+        dirname = self.temp_file_name(basename)
         os.mkdir(dirname)
         return dirname
 
@@ -84,8 +96,11 @@
         the contents of the file.  The permissions if present, should be
         an int specifying the files permissions (setting permissions
         doesn't work yet for directories).
+
+        The method returns the absolute name of the toplevel directory
+        created by the method.
         """
-        shutil.rmtree(directory, True)
+        directory = self.temp_file_name(directory)
         os.makedirs(directory)
         for item in filedesc:
             if len(item) == 3:
@@ -95,11 +110,10 @@
                 permissions = None # use default permissions
             if isinstance(contents, list):
                 # a list as contents indicates a directory
-                newdir = os.path.join(directory, name)
-                os.mkdir(newdir)
-                self.create_files(newdir, contents)
+                self.create_files(os.path.join(directory, name), contents)
             else:
                 writefile(os.path.join(directory, name), contents, permissions)
+        return directory
 
     def checkFileContents(self, filename, contents):
         """check the contents of a file"""
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)