[Inteproxy-commits] r203 - in trunk: . test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Sep 11 15:19:47 CEST 2009


Author: bh
Date: 2009-09-11 15:19:47 +0200 (Fri, 11 Sep 2009)
New Revision: 203

Modified:
   trunk/ChangeLog
   trunk/test/support.py
Log:
* test/support.py (FileTestMixin): Upgrade to a newer version of
FileTestMixin.  This one uses test specific directory for all
files created by a test case and has better documentation.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-09-10 20:24:12 UTC (rev 202)
+++ trunk/ChangeLog	2009-09-11 13:19:47 UTC (rev 203)
@@ -1,3 +1,9 @@
+2009-09-11  Bernhard Herzog  <bh at intevation.de>
+
+	* test/support.py (FileTestMixin): Upgrade to a newer version of
+	FileTestMixin.  This one uses test specific directory for all
+	files created by a test case and has better documentation.
+
 2009-09-10  Bernhard Herzog  <bh at intevation.de>
 
 	* inteproxy/transcoder.py (TranscoderMap.add_rule)

Modified: trunk/test/support.py
===================================================================
--- trunk/test/support.py	2009-09-10 20:24:12 UTC (rev 202)
+++ trunk/test/support.py	2009-09-11 13:19:47 UTC (rev 203)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007, 2008 by Intevation GmbH
+# Copyright (C) 2007, 2008, 2009 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
 #
@@ -8,6 +8,7 @@
 """Support code for the test cases"""
 
 import os
+import shutil
 
 def create_temp_dir():
     """Create a temporary directory for the test-suite and return its name.
@@ -30,21 +31,49 @@
 
 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.
     """
 
+    _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.
+        """
+        dirname = os.path.join(create_temp_dir(), self.id())
+        if not self._test_specific_directory_created:
+            if os.path.islink(dirname) or os.path.isfile(dirname):
+                os.remove(dirname)
+            elif os.path.isdir(dirname):
+                shutil.rmtree(dirname)
+            os.mkdir(dirname)
+            self._test_specific_directory_created = True
+        return dirname
+
     def temp_file_name(self, basename):
-        """Return the full name of the file named basename in the temp. dir"""
-        return os.path.join(create_temp_dir(), 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, mode = None):
-        """Create a file in the temp directory"""
+    def create_temp_file(self, basename, contents, permissions=None):
+        """Creates a file in the temp directory with the given contents.
+        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
 
 



More information about the Inteproxy-commits mailing list