changeset 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 83c77307ffb1
children 2622bf5ed5bd
files test/filesupport.py test/test_builder.py test/test_debian.py test/test_packager.py test/test_readconfig.py test/test_util.py
diffstat 6 files changed, 126 insertions(+), 115 deletions(-) [+]
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"""
--- a/test/test_builder.py	Wed Jun 25 15:15:30 2008 +0000
+++ b/test/test_builder.py	Thu Jun 26 16:23:26 2008 +0000
@@ -45,7 +45,7 @@
 
     def test_init_pbuilder(self):
         """Tests the PBuilder.init_pbuilder method."""
-        basedir = self.create_temp_dir(self.id())
+        basedir = self.create_temp_dir("pbuilder")
         pbuilderrc = os.path.join(basedir, "pbuilderrc")
         builder = PBuilder(pbuilderrc, self.root_command)
         old_stdout = sys.stdout
@@ -106,7 +106,7 @@
     def test_init_pbuilder_run_twice(self):
         """Tests whether PBuilder.init_pbuilder prints an error when run twice.
         """
-        basedir = self.create_temp_dir(self.id())
+        basedir = self.create_temp_dir("pbuilder")
 
         # run it once
         pbuilderrc = os.path.join(basedir, "pbuilderrc")
@@ -220,7 +220,7 @@
         binary_dir_name = self.temp_file_name("binary")
         if os.path.exists(binary_dir_name):
             os.rmdir(binary_dir_name)
-        env_test_file = self.temp_file_name(self.id() + "_envtest", remove=True)
+        env_test_file = self.temp_file_name("envtest")
         # sanity check: the binary directory must not exist yet.
         self.failIf(os.path.exists(binary_dir_name))
         # sanity check: the environment variable TREEPKG_TEST must not
@@ -341,7 +341,7 @@
 
     def setUp(self):
         PBuilderTests.setUp(self)
-        self.temp_base_dir = self.create_temp_dir(self.id())
+        self.temp_base_dir = self.create_temp_dir("pbuilder")
         self.minimal_packge_dir = os.path.join(self.temp_base_dir,
                                                "minimal-1.0")
         self.create_files(self.minimal_packge_dir, self.minimal_package)
--- a/test/test_debian.py	Wed Jun 25 15:15:30 2008 +0000
+++ b/test/test_debian.py	Thu Jun 26 16:23:26 2008 +0000
@@ -46,8 +46,7 @@
 """
 
     def test(self):
-        filename = self.create_temp_file(self.id() + "-control",
-                                         self.control_contents)
+        filename = self.create_temp_file("control", self.control_contents)
         parsed = DebianControlFile(filename)
         self.assertEquals(parsed.packages,
                           [('libksba-dev', 'any'), ('libksba8', 'any')])
--- a/test/test_packager.py	Wed Jun 25 15:15:30 2008 +0000
+++ b/test/test_packager.py	Thu Jun 26 16:23:26 2008 +0000
@@ -101,10 +101,9 @@
 
 
     def setUp(self):
-        self.svndir = self.create_temp_dir(self.id() + "-svn")
-        self.svnworkdir = self.create_temp_dir(self.id() + "-svnwork")
-        self.trackdir = self.create_temp_dir(self.id() + "-track")
-        self.create_files(self.trackdir, self.debian_files)
+        self.svndir = self.create_temp_dir("svn")
+        self.svnworkdir = self.create_temp_dir("svnwork")
+        self.trackdir = self.create_files("track", self.debian_files)
         self.svn_url = create_svn_repository(self.svndir) + "/trunk"
         subversion.checkout(self.svn_url, self.svnworkdir)
         for message, files in self.revisions:
@@ -200,7 +199,7 @@
 class TestPackageGroupStop(unittest.TestCase, FileTestMixin):
 
     def setUp(self):
-        self.trackdir = self.create_temp_dir(self.id() + "-track")
+        self.trackdir = self.create_temp_dir("track")
         self.instructions_file = os.path.join(self.trackdir, "instructions")
 
     def group(self, do_package=True, do_stop=True):
@@ -246,74 +245,74 @@
 
 class TestPackageTrack(unittest.TestCase, FileTestMixin):
 
-    def setUp(self):
-        self.trackdir = self.create_temp_dir(self.id() + "-track")
+    # def setUp(self):
+    #     self.trackdir = self.create_temp_dir("track")
 
     def test_get_revision_numbers(self):
         # Note: The revisions in the pkg dir are not ordered so that we
         # can check whether get_revision_numbers returns a sorted list
         # of revisions
-        self.create_files(self.trackdir,
-                          [("pkg",
-                            [("704195-1",
-                              [("status", ""),
-                               ("src", []),
-                               ("binary", [])]),
-                             ("702432-1",
-                              [("status", ""),
-                               ("src", []),
-                               ("binary", [])])])])
-        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
+        trackdir = self.create_files("track",
+                                     [("pkg",
+                                       [("704195-1",
+                                         [("status", ""),
+                                          ("src", []),
+                                          ("binary", [])]),
+                                        ("702432-1",
+                                         [("status", ""),
+                                          ("src", []),
+                                          ("binary", [])])])])
+        track = PackageTrack("testtrack", trackdir, "", "", "", "", "")
         self.assertEquals(track.get_revision_numbers(), [702432, 704195])
 
     def test_last_packaged_revision_with_no_revisions(self):
-        self.create_files(self.trackdir, [("pkg", [])])
-        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
+        trackdir = self.create_files("track", [("pkg", [])])
+        track = PackageTrack("testtrack", trackdir, "", "", "", "", "")
         self.assertEquals(track.last_packaged_revision(), -1)
 
     def test_last_packaged_revision_several_revisions(self):
-        self.create_files(self.trackdir,
-                          [("pkg",
-                            [("704195-1",
-                              [("status", ""),
-                               ("src", []),
-                               ("binary", [])]),
-                             ("702432-1",
-                              [("status", ""),
-                               ("src", []),
-                               ("binary", [])])])])
-        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
+        trackdir = self.create_files("track",
+                                     [("pkg",
+                                       [("704195-1",
+                                         [("status", ""),
+                                          ("src", []),
+                                          ("binary", [])]),
+                                        ("702432-1",
+                                         [("status", ""),
+                                          ("src", []),
+                                          ("binary", [])])])])
+        track = PackageTrack("testtrack", trackdir, "", "", "", "", "")
         self.assertEquals(track.last_packaged_revision(), 704195)
 
 
 class TestRevisionPackager(unittest.TestCase, FileTestMixin):
 
-    def setUp(self):
-        self.trackdir = self.create_temp_dir(self.id() + "-track")
+    # def setUp(self):
+    #     self.trackdir = self.create_temp_dir("track")
 
     def test_list_source_files(self):
         # Note: The revisions in the pkg dir are not ordered so that we
         # can check whether get_revision_numbers returns a sorted list
         # of revisions
-        self.create_files(self.trackdir,
-                          [("pkg",
-                            [("704195-1",
-                              [("status",
-                                ("TreePackagerStatus 0.0\n"
-                                 "status: binary_package_created\n"
-                                 "start: 2007-09-10 17:16:48\n"
-                                 "stop: 2007-09-11 00:07:36\n")),
-                               ("src", [("test_1.0.orig.tar.gz", ""),
-                                        ("test_1.0-1.diff.gz", ""),
-                                        ("test_1.0-1.dsc", "")]),
-                               ("binary", [])]),
-                             ("702432-1",
-                              [("status", ""),
-                               ("src", []),
-                               ("binary", [])])])])
-        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
+        trackdir = self.create_files("track",
+                                     [("pkg",
+                                       [("704195-1",
+                                         [("status",
+                                           ("TreePackagerStatus 0.0\n"
+                                            "status: binary_package_created\n"
+                                            "start: 2007-09-10 17:16:48\n"
+                                            "stop: 2007-09-11 00:07:36\n")),
+                                          ("src", [("test_1.0.orig.tar.gz", ""),
+                                                   ("test_1.0-1.diff.gz", ""),
+                                                   ("test_1.0-1.dsc", "")]),
+                                          ("binary", [])]),
+                                        ("702432-1",
+                                         [("status", ""),
+                                          ("src", []),
+                                          ("binary", [])])])])
+        track = PackageTrack("testtrack", trackdir, "", "", "", "", "")
         revpkg = RevisionPackager(track, 704195)
-        srcdir = os.path.join(self.trackdir, "pkg", "704195-1", "src")
+        srcdir = os.path.join(trackdir, "pkg", "704195-1", "src")
         self.assertEquals(revpkg.list_source_files(),
                           [os.path.join(srcdir, filename)
                            for filename in ["test_1.0-1.diff.gz",
@@ -324,24 +323,25 @@
         # Note: The revisions in the pkg dir are not ordered so that we
         # can check whether get_revision_numbers returns a sorted list
         # of revisions
-        self.create_files(self.trackdir,
-                          [("pkg",
-                            [("704195-1",
-                              [("status",
-                                ("TreePackagerStatus 0.0\n"
-                                 "status: binary_package_created\n"
-                                 "start: 2007-09-10 17:16:48\n"
-                                 "stop: 2007-09-11 00:07:36\n")),
-                               ("src", []),
-                               ("binary", [("test_1.0-1_i386.deb", ""),
-                                           ("test_1.0-1_i386.changes", "")])]),
-                             ("702432-1",
-                              [("status", ""),
-                               ("src", []),
-                               ("binary", [])])])])
-        track = PackageTrack("testtrack", self.trackdir, "", "", "", "", "")
+        trackdir = self.create_files("track",
+                                     [("pkg",
+                                       [("704195-1",
+                                         [("status",
+                                           ("TreePackagerStatus 0.0\n"
+                                            "status: binary_package_created\n"
+                                            "start: 2007-09-10 17:16:48\n"
+                                            "stop: 2007-09-11 00:07:36\n")),
+                                          ("src", []),
+                                          ("binary",
+                                           [("test_1.0-1_i386.deb", ""),
+                                            ("test_1.0-1_i386.changes", "")])]),
+                                        ("702432-1",
+                                         [("status", ""),
+                                          ("src", []),
+                                          ("binary", [])])])])
+        track = PackageTrack("testtrack", trackdir, "", "", "", "", "")
         revpkg = RevisionPackager(track, 704195)
-        bindir = os.path.join(self.trackdir, "pkg", "704195-1", "binary")
+        bindir = os.path.join(trackdir, "pkg", "704195-1", "binary")
         self.assertEquals(revpkg.list_binary_files(),
                           [os.path.join(bindir, filename)
                            for filename in ["test_1.0-1_i386.changes",
@@ -368,8 +368,7 @@
                                          ""]))])]
 
     def setUp(self):
-        self.directory = self.create_temp_dir(self.id())
-        self.create_files(self.directory, self.files)
+        self.directory = self.create_files("a_module", self.files)
         self.old_path = sys.path
         sys.path = [self.directory] + sys.path
 
--- a/test/test_readconfig.py	Wed Jun 25 15:15:30 2008 +0000
+++ b/test/test_readconfig.py	Thu Jun 26 16:23:26 2008 +0000
@@ -62,8 +62,7 @@
                            ""]))])]
 
     def setUp(self):
-        self.directory = self.create_temp_dir(self.id())
-        self.create_files(self.directory, self.files)
+        self.directory = self.create_files("treepkg", self.files)
         self.old_path = sys.path
         sys.path = [self.directory] + sys.path
 
--- a/test/test_util.py	Wed Jun 25 15:15:30 2008 +0000
+++ b/test/test_util.py	Thu Jun 26 16:23:26 2008 +0000
@@ -18,7 +18,7 @@
 class TestReplaceInFile(unittest.TestCase, FileTestMixin):
 
     def runtest(self, orig_contents, expected_contents, pattern, replacement):
-        filename = self.create_temp_file(self.id(), orig_contents)
+        filename = self.create_temp_file("orig", orig_contents)
         changed = replace_in_file(filename, pattern, replacement)
         self.assertEquals(changed, orig_contents != expected_contents)
         self.checkFileContents(filename, expected_contents)
@@ -54,21 +54,21 @@
 class TestListDirAbs(unittest.TestCase, FileTestMixin):
 
     def setUp(self):
-        self.directory = self.create_temp_dir(self.id())
+        self.directory = self.create_temp_dir("a_directory")
 
     def test_listdir_abs(self):
-        self.create_files(self.directory, [("foo.orig.tgz", ""),
-                                           ("foo.dsc", ""),
-                                           ("foo.diff.gz", ""),])
-        self.assertEquals(sorted(listdir_abs(self.directory)),
-                          sorted([os.path.join(self.directory, d)
+        directory = self.create_files("dir", [("foo.orig.tgz", ""),
+                                              ("foo.dsc", ""),
+                                              ("foo.diff.gz", ""),])
+        self.assertEquals(sorted(listdir_abs(directory)),
+                          sorted([os.path.join(directory, d)
                                   for d in ["foo.orig.tgz", "foo.dsc",
                                             "foo.diff.gz"]]))
 
     def test_listdir_abs_pattern(self):
-        self.create_files(self.directory, [("foo.orig.tgz", ""),
-                                           ("foo.dsc", ""),
-                                           ("foo.diff.gz", ""),])
-        self.assertEquals(sorted(listdir_abs(self.directory, '*.dsc')),
-                          [os.path.join(self.directory, "foo.dsc")])
+        directory = self.create_files("dir", [("foo.orig.tgz", ""),
+                                              ("foo.dsc", ""),
+                                              ("foo.diff.gz", ""),])
+        self.assertEquals(sorted(listdir_abs(directory, '*.dsc')),
+                          [os.path.join(directory, "foo.dsc")])
 
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)