diff -r d24befb680d6 Lib/test/test_os.py --- a/Lib/test/test_os.py Tue Oct 30 20:20:54 2012 -0400 +++ b/Lib/test/test_os.py Tue Oct 30 23:52:40 2012 -0400 @@ -54,7 +54,16 @@ def setUp(self): if os.path.exists(support.TESTFN): os.unlink(support.TESTFN) - tearDown = setUp + self.rename_dest_file = "%s_%d_tmp" % ("test_rename_dest_file", \ + os.getpid()) + if os.path.exists(self.rename_dest_file): + os.unlink(self.rename_dest_file) + + def tearDown(self): + if os.path.exists(support.TESTFN): + os.unlink(support.TESTFN) + if os.path.exists(self.rename_dest_file): + os.unlink(self.rename_dest_file) def test_access(self): f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) @@ -90,6 +99,41 @@ new = sys.getrefcount(path) self.assertEqual(old, new) + def test_rename_src_file_dest_file_not_exist(self): + source = support.TESTFN + destination = self.rename_dest_file + fd = os.open(source, os.O_CREAT | os.O_WRONLY) + os.write(fd, b"beans\n") + os.write(fd, b"bacon\n") + os.write(fd, b"eggs\n") + os.write(fd, b"spam\n") + os.close(fd) + os.rename(source, destination) + with open(destination, "rb") as fobj: + self.assertEqual(fobj.read().splitlines(), + [b"beans", b"bacon", b"eggs", b"spam"]) + + # the file name in variable destination should not be replaced because it + # already exists + # this test case actually makes the source file disapear which might be + # a bug + def test_rename_src_file_dest_file_exists(self): + source = support.TESTFN + destination = self.rename_dest_file + fd = os.open(source, os.O_CREAT | os.O_WRONLY) + os.write(fd, b"beans\n") + os.write(fd, b"bacon\n") + os.write(fd, b"eggs\n") + os.write(fd, b"spam\n") + os.close(fd) + fd = os.open(source, os.O_CREAT | os.O_WRONLY) + os.write(fd, b"destination file already exists and has data\n") + os.close(fd) + os.rename(source, destination) + with open(destination, "rb") as fobj: + self.assertNotEqual(fobj.read().splitlines(), + [b"beans", b"bacon", b"eggs", b"spam"]) + def test_read(self): with open(support.TESTFN, "w+b") as fobj: fobj.write(b"spam")