[Python-checkins] r54252 - python/trunk/Lib/test/test_posixpath.py

collin.winter python-checkins at python.org
Sat Mar 10 03:23:49 CET 2007


Author: collin.winter
Date: Sat Mar 10 03:23:40 2007
New Revision: 54252

Modified:
   python/trunk/Lib/test/test_posixpath.py
Log:
* Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
* Drop safe_remove() in favor of test_support.unlink().
* Fix the indentation of test_samefile so that it runs.



Modified: python/trunk/Lib/test/test_posixpath.py
==============================================================================
--- python/trunk/Lib/test/test_posixpath.py	(original)
+++ python/trunk/Lib/test/test_posixpath.py	Sat Mar 10 03:23:40 2007
@@ -9,8 +9,22 @@
 
 ABSTFN = abspath(test_support.TESTFN)
 
+def safe_rmdir(dirname):
+    try:
+        os.rmdir(dirname)
+    except OSError:
+        pass
+
 class PosixPathTest(unittest.TestCase):
 
+    def setUp(self):
+        self.tearDown()
+
+    def tearDown(self):
+        for suffix in ["", "1", "2"]:
+            test_support.unlink(test_support.TESTFN + suffix)
+            safe_rmdir(test_support.TESTFN + suffix)
+            
     def assertIs(self, a, b):
         self.assert_(a is b)
 
@@ -125,7 +139,6 @@
         finally:
             if not f.closed:
                 f.close()
-            os.remove(test_support.TESTFN)
 
     def test_time(self):
         f = open(test_support.TESTFN, "wb")
@@ -147,9 +160,8 @@
         finally:
             if not f.closed:
                 f.close()
-            os.remove(test_support.TESTFN)
 
-    def test_islink(self):
+    def test_islink(self):    
         self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
         f = open(test_support.TESTFN + "1", "wb")
         try:
@@ -166,14 +178,6 @@
         finally:
             if not f.close():
                 f.close()
-            try:
-                os.remove(test_support.TESTFN + "1")
-            except os.error:
-                pass
-            try:
-                os.remove(test_support.TESTFN + "2")
-            except os.error:
-                pass
 
         self.assertRaises(TypeError, posixpath.islink)
 
@@ -188,10 +192,6 @@
         finally:
             if not f.close():
                 f.close()
-            try:
-                os.remove(test_support.TESTFN)
-            except os.error:
-                pass
 
         self.assertRaises(TypeError, posixpath.exists)
 
@@ -209,14 +209,6 @@
         finally:
             if not f.close():
                 f.close()
-            try:
-                os.remove(test_support.TESTFN)
-            except os.error:
-                pass
-            try:
-                os.rmdir(test_support.TESTFN)
-            except os.error:
-                pass
 
         self.assertRaises(TypeError, posixpath.isdir)
 
@@ -234,67 +226,51 @@
         finally:
             if not f.close():
                 f.close()
-            try:
-                os.remove(test_support.TESTFN)
-            except os.error:
-                pass
-            try:
-                os.rmdir(test_support.TESTFN)
-            except os.error:
-                pass
 
         self.assertRaises(TypeError, posixpath.isdir)
 
-        def test_samefile(self):
-            f = open(test_support.TESTFN + "1", "wb")
-            try:
-                f.write("foo")
-                f.close()
+    def test_samefile(self):
+        f = open(test_support.TESTFN + "1", "wb")
+        try:
+            f.write("foo")
+            f.close()
+            self.assertIs(
+                posixpath.samefile(
+                    test_support.TESTFN + "1",
+                    test_support.TESTFN + "1"
+                ),
+                True
+            )
+            # If we don't have links, assume that os.stat doesn't return resonable
+            # inode information and thus, that samefile() doesn't work
+            if hasattr(os, "symlink"):
+                os.symlink(
+                    test_support.TESTFN + "1",
+                    test_support.TESTFN + "2"
+                )
                 self.assertIs(
                     posixpath.samefile(
                         test_support.TESTFN + "1",
-                        test_support.TESTFN + "1"
+                        test_support.TESTFN + "2"
                     ),
                     True
                 )
-                # If we don't have links, assume that os.stat doesn't return resonable
-                # inode information and thus, that samefile() doesn't work
-                if hasattr(os, "symlink"):
-                    os.symlink(
+                os.remove(test_support.TESTFN + "2")
+                f = open(test_support.TESTFN + "2", "wb")
+                f.write("bar")
+                f.close()
+                self.assertIs(
+                    posixpath.samefile(
                         test_support.TESTFN + "1",
                         test_support.TESTFN + "2"
-                    )
-                    self.assertIs(
-                        posixpath.samefile(
-                            test_support.TESTFN + "1",
-                            test_support.TESTFN + "2"
-                        ),
-                        True
-                    )
-                    os.remove(test_support.TESTFN + "2")
-                    f = open(test_support.TESTFN + "2", "wb")
-                    f.write("bar")
-                    f.close()
-                    self.assertIs(
-                        posixpath.samefile(
-                            test_support.TESTFN + "1",
-                            test_support.TESTFN + "2"
-                        ),
-                        False
-                    )
-            finally:
-                if not f.close():
-                    f.close()
-                try:
-                    os.remove(test_support.TESTFN + "1")
-                except os.error:
-                    pass
-                try:
-                    os.remove(test_support.TESTFN + "2")
-                except os.error:
-                    pass
+                    ),
+                    False
+                )
+        finally:
+            if not f.close():
+                f.close()
 
-            self.assertRaises(TypeError, posixpath.samefile)
+        self.assertRaises(TypeError, posixpath.samefile)
 
     def test_samestat(self):
         f = open(test_support.TESTFN + "1", "wb")
@@ -334,14 +310,6 @@
         finally:
             if not f.close():
                 f.close()
-            try:
-                os.remove(test_support.TESTFN + "1")
-            except os.error:
-                pass
-            try:
-                os.remove(test_support.TESTFN + "2")
-            except os.error:
-                pass
 
         self.assertRaises(TypeError, posixpath.samestat)
 
@@ -421,7 +389,7 @@
                 os.symlink(ABSTFN+"1", ABSTFN)
                 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
             finally:
-                self.safe_remove(ABSTFN)
+                test_support.unlink(ABSTFN)
 
         def test_realpath_symlink_loops(self):
             # Bug #930024, return the path unchanged if we get into an infinite
@@ -441,9 +409,9 @@
                 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
             finally:
                 os.chdir(old_path)
-                self.safe_remove(ABSTFN)
-                self.safe_remove(ABSTFN+"1")
-                self.safe_remove(ABSTFN+"2")
+                test_support.unlink(ABSTFN)
+                test_support.unlink(ABSTFN+"1")
+                test_support.unlink(ABSTFN+"2")
 
         def test_realpath_resolve_parents(self):
             # We also need to resolve any symlinks in the parents of a relative
@@ -460,9 +428,9 @@
                 self.assertEqual(realpath("a"), ABSTFN + "/y/a")
             finally:
                 os.chdir(old_path)
-                self.safe_remove(ABSTFN + "/k")
-                self.safe_rmdir(ABSTFN + "/y")
-                self.safe_rmdir(ABSTFN)
+                test_support.unlink(ABSTFN + "/k")
+                safe_rmdir(ABSTFN + "/y")
+                safe_rmdir(ABSTFN)
 
         def test_realpath_resolve_before_normalizing(self):
             # Bug #990669: Symbolic links should be resolved before we
@@ -486,10 +454,10 @@
                 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), ABSTFN + "/k")
             finally:
                 os.chdir(old_path)
-                self.safe_remove(ABSTFN + "/link-y")
-                self.safe_rmdir(ABSTFN + "/k/y")
-                self.safe_rmdir(ABSTFN + "/k")
-                self.safe_rmdir(ABSTFN)
+                test_support.unlink(ABSTFN + "/link-y")
+                safe_rmdir(ABSTFN + "/k/y")
+                safe_rmdir(ABSTFN + "/k")
+                safe_rmdir(ABSTFN)
 
         def test_realpath_resolve_first(self):
             # Bug #1213894: The first component of the path, if not absolute,
@@ -507,20 +475,9 @@
                 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
             finally:
                 os.chdir(old_path)
-                self.safe_remove(ABSTFN + "link")
-                self.safe_rmdir(ABSTFN + "/k")
-                self.safe_rmdir(ABSTFN)
-
-        # Convenience functions for removing temporary files.
-        def pass_os_error(self, func, filename):
-            try: func(filename)
-            except OSError: pass
-
-        def safe_remove(self, filename):
-            self.pass_os_error(os.remove, filename)
-
-        def safe_rmdir(self, dirname):
-            self.pass_os_error(os.rmdir, dirname)
+                test_support.unlink(ABSTFN + "link")
+                safe_rmdir(ABSTFN + "/k")
+                safe_rmdir(ABSTFN)
 
 def test_main():
     test_support.run_unittest(PosixPathTest)


More information about the Python-checkins mailing list