From dbead56cb603e5e34bad0dc6e066d7dec411b422 Mon Sep 17 00:00:00 2001 From: Collin Winter Date: Sat, 10 Mar 2007 02:23:40 +0000 Subject: [PATCH] * 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. --- Lib/test/test_posixpath.py | 173 ++++++++++++++----------------------- 1 file changed, 65 insertions(+), 108 deletions(-) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index d34c7e261cd..8ac58537440 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -9,8 +9,22 @@ from posixpath import realpath, abspath, join, dirname, basename 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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") + 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 + "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 + "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 + "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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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 @@ class PosixPathTest(unittest.TestCase): 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)