* 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.
This commit is contained in:
Collin Winter 2007-03-10 02:23:40 +00:00
parent 9453e5dce5
commit dbead56cb6
1 changed files with 65 additions and 108 deletions

View File

@ -9,8 +9,22 @@ from posixpath import realpath, abspath, join, dirname, basename
ABSTFN = abspath(test_support.TESTFN) ABSTFN = abspath(test_support.TESTFN)
def safe_rmdir(dirname):
try:
os.rmdir(dirname)
except OSError:
pass
class PosixPathTest(unittest.TestCase): 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): def assertIs(self, a, b):
self.assert_(a is b) self.assert_(a is b)
@ -125,7 +139,6 @@ class PosixPathTest(unittest.TestCase):
finally: finally:
if not f.closed: if not f.closed:
f.close() f.close()
os.remove(test_support.TESTFN)
def test_time(self): def test_time(self):
f = open(test_support.TESTFN, "wb") f = open(test_support.TESTFN, "wb")
@ -147,9 +160,8 @@ class PosixPathTest(unittest.TestCase):
finally: finally:
if not f.closed: if not f.closed:
f.close() f.close()
os.remove(test_support.TESTFN)
def test_islink(self): def test_islink(self):
self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False) self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
f = open(test_support.TESTFN + "1", "wb") f = open(test_support.TESTFN + "1", "wb")
try: try:
@ -166,14 +178,6 @@ class PosixPathTest(unittest.TestCase):
finally: finally:
if not f.close(): if not f.close():
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) self.assertRaises(TypeError, posixpath.islink)
@ -188,10 +192,6 @@ class PosixPathTest(unittest.TestCase):
finally: finally:
if not f.close(): if not f.close():
f.close() f.close()
try:
os.remove(test_support.TESTFN)
except os.error:
pass
self.assertRaises(TypeError, posixpath.exists) self.assertRaises(TypeError, posixpath.exists)
@ -209,14 +209,6 @@ class PosixPathTest(unittest.TestCase):
finally: finally:
if not f.close(): if not f.close():
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) self.assertRaises(TypeError, posixpath.isdir)
@ -234,67 +226,51 @@ class PosixPathTest(unittest.TestCase):
finally: finally:
if not f.close(): if not f.close():
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) self.assertRaises(TypeError, posixpath.isdir)
def test_samefile(self): def test_samefile(self):
f = open(test_support.TESTFN + "1", "wb") f = open(test_support.TESTFN + "1", "wb")
try: try:
f.write("foo") 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() f.close()
self.assertIs( self.assertIs(
posixpath.samefile( 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 + "1",
test_support.TESTFN + "2" test_support.TESTFN + "2"
) ),
self.assertIs( False
posixpath.samefile( )
test_support.TESTFN + "1", finally:
test_support.TESTFN + "2" if not f.close():
), f.close()
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
self.assertRaises(TypeError, posixpath.samefile) self.assertRaises(TypeError, posixpath.samefile)
def test_samestat(self): def test_samestat(self):
f = open(test_support.TESTFN + "1", "wb") f = open(test_support.TESTFN + "1", "wb")
@ -334,14 +310,6 @@ class PosixPathTest(unittest.TestCase):
finally: finally:
if not f.close(): if not f.close():
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) self.assertRaises(TypeError, posixpath.samestat)
@ -421,7 +389,7 @@ class PosixPathTest(unittest.TestCase):
os.symlink(ABSTFN+"1", ABSTFN) os.symlink(ABSTFN+"1", ABSTFN)
self.assertEqual(realpath(ABSTFN), ABSTFN+"1") self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
finally: finally:
self.safe_remove(ABSTFN) test_support.unlink(ABSTFN)
def test_realpath_symlink_loops(self): def test_realpath_symlink_loops(self):
# Bug #930024, return the path unchanged if we get into an infinite # 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) self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
finally: finally:
os.chdir(old_path) os.chdir(old_path)
self.safe_remove(ABSTFN) test_support.unlink(ABSTFN)
self.safe_remove(ABSTFN+"1") test_support.unlink(ABSTFN+"1")
self.safe_remove(ABSTFN+"2") test_support.unlink(ABSTFN+"2")
def test_realpath_resolve_parents(self): def test_realpath_resolve_parents(self):
# We also need to resolve any symlinks in the parents of a relative # 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") self.assertEqual(realpath("a"), ABSTFN + "/y/a")
finally: finally:
os.chdir(old_path) os.chdir(old_path)
self.safe_remove(ABSTFN + "/k") test_support.unlink(ABSTFN + "/k")
self.safe_rmdir(ABSTFN + "/y") safe_rmdir(ABSTFN + "/y")
self.safe_rmdir(ABSTFN) safe_rmdir(ABSTFN)
def test_realpath_resolve_before_normalizing(self): def test_realpath_resolve_before_normalizing(self):
# Bug #990669: Symbolic links should be resolved before we # 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") self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), ABSTFN + "/k")
finally: finally:
os.chdir(old_path) os.chdir(old_path)
self.safe_remove(ABSTFN + "/link-y") test_support.unlink(ABSTFN + "/link-y")
self.safe_rmdir(ABSTFN + "/k/y") safe_rmdir(ABSTFN + "/k/y")
self.safe_rmdir(ABSTFN + "/k") safe_rmdir(ABSTFN + "/k")
self.safe_rmdir(ABSTFN) safe_rmdir(ABSTFN)
def test_realpath_resolve_first(self): def test_realpath_resolve_first(self):
# Bug #1213894: The first component of the path, if not absolute, # 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") self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
finally: finally:
os.chdir(old_path) os.chdir(old_path)
self.safe_remove(ABSTFN + "link") test_support.unlink(ABSTFN + "link")
self.safe_rmdir(ABSTFN + "/k") safe_rmdir(ABSTFN + "/k")
self.safe_rmdir(ABSTFN) 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)
def test_main(): def test_main():
test_support.run_unittest(PosixPathTest) test_support.run_unittest(PosixPathTest)