diff -r 63b45c959a2a Lib/test/test_os.py --- a/Lib/test/test_os.py Sun Nov 04 07:00:04 2012 +0200 +++ b/Lib/test/test_os.py Sun Nov 04 21:19:57 2012 -0500 @@ -49,12 +49,48 @@ else: USING_LINUXTHREADS = False -# Tests creating TESTFN +# Tests creating TESTFN and os.rename class FileTests(unittest.TestCase): def setUp(self): if os.path.exists(support.TESTFN): os.unlink(support.TESTFN) - tearDown = setUp + + self.rename_src_file = "%s_%d_tmp" % ("test_rename_src_file", \ + os.getpid()) + if os.path.exists(self.rename_src_file): + os.unlink(self.rename_src_file) + self.rename_dst_file = "%s_%d_tmp" % ("test_rename_dst_file", \ + os.getpid()) + if os.path.exists(self.rename_dst_file): + os.unlink(self.rename_dst_file) + self.rename_dst_directory = "%s_%d_tmp" % \ + ("test_rename_dst_directory", os.getpid()) + if os.path.exists(self.rename_dst_directory + "/file.txt"): + os.unlink(self.rename_dst_directory + "/file.txt") + if os.path.exists(self.rename_dst_directory): + os.rmdir(self.rename_dst_directory) + self.rename_src_directory = "%s_%d_tmp" % \ + ("test_src_directory", os.getpid()) + if os.path.exists(self.rename_src_directory + "/file.txt"): + os.unlink(self.rename_src_directory + "/file.txt") + if (os.path.exists(self.rename_src_directory)): + os.rmdir(self.rename_src_directory) + + def tearDown(self): + if os.path.exists(support.TESTFN): + os.unlink(support.TESTFN) + if os.path.exists(self.rename_src_file): + os.unlink(self.rename_src_file) + if os.path.exists(self.rename_dst_file): + os.unlink(self.rename_dst_file) + if os.path.exists(self.rename_dst_directory + "/file.txt"): + os.unlink(self.rename_dst_directory + "/file.txt") + if os.path.exists(self.rename_dst_directory): + os.rmdir(self.rename_dst_directory) + if os.path.exists(self.rename_src_directory + "/file.txt"): + os.unlink(self.rename_src_directory + "/file.txt") + if (os.path.exists(self.rename_src_directory)): + os.rmdir(self.rename_src_directory) def test_access(self): f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) @@ -90,6 +126,344 @@ new = sys.getrefcount(path) self.assertEqual(old, new) + # http://bugs.python.org/issue16278 + # the os.rename docuemntation states: + # "On Unix, if dst exists and is a file, it will be replaced silently if + # the user has permission." + # + # "On Windows, if dst already exists, OSError will be raised even if it + # is a file; there may be no way to implement an atomic rename when dst + # names an existing file." + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_dest_file_exist(self): + src = self.rename_src_file + dst = self.rename_dst_file + fd = os.open(src, 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(dst, os.O_CREAT | os.O_WRONLY) + os.write(fd, b"dst file already exists and has data\n") + os.close(fd) + if ((sys.platform == 'darwin') or (sys.platform.startswith == "linux")): + os.rename(src, dst) + with open(dst, "rb") as fobj: + self.assertEqual(fobj.read().splitlines(), + [b"beans", b"bacon", b"eggs", b"spam"]) + elif (sys.platform == 'win'): + self.assertRaises(OSError, os.rename, src, dst) + with open(dst, "rb") as fobj: + self.assertEqual(fobj.read(), + b"destination file already exists and has data\n") + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_dst_not_exist(self): + src = self.rename_src_file + dst = self.rename_dst_file + fd = os.open(src, 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(src, dst) + with open(dst, "rb") as fobj: + self.assertEqual(fobj.read().splitlines(), + [b"beans", b"bacon", b"eggs", b"spam"]) + + # http://bugs.python.org/issue16278 + # the os.rename documentation should state: + # Rename the file src to dst. If src is a file and dst is a + # directory that is empty or not empty, OSError will be raised. + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_dst_directory_empty(self): + src = self.rename_src_file + dst = self.rename_dst_directory + fd = os.open(src, 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.mkdir(dst) + self.assertRaises(OSError, os.rename, src, dst) + + # http://bugs.python.org/issue16278 + # the os.rename documentation should state: + # Rename the file or directory src to dst. If src is a file and dst is a + # directory that is empty or not empty, OSError will be raised. + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_dst_directory_not_empty(self): + src = self.rename_src_file + dst = self.rename_dst_directory + fd = os.open(src, 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.mkdir(dst) + fd = os.open(dst + "/file.txt", os.O_CREAT | os.O_WRONLY) + os.write(fd, b"dst file already exists and has data") + os.close(fd) + self.assertRaises(OSError, os.rename, src, dst) + with open(dst + "/file.txt", "rb") as fobj: + self.assertEqual(fobj.read(), + b"dst file already exists and has data") + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_empty_dst_file_exist(self): + src = self.rename_src_directory + dst = self.rename_dst_file + os.mkdir(src) + fd = os.open(dst, os.O_CREAT | os.O_WRONLY) + os.write(fd, b"dst file already exists and has data") + os.close(fd) + self.assertRaises(OSError, os.rename, src, dst) + with open(dst, "rb") as fobj: + self.assertEqual(fobj.read(), + b"dst file already exists and has data") + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_empty_dst_not_exist(self): + src = self.rename_src_directory + dst = self.rename_dst_directory + os.mkdir(src) + os.rename(src, dst) + self.assertEqual(os.path.exists(dst), True) + self.assertEqual(os.path.exists(src), False) + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_empty_dst_directory_empty(self): + src = self.rename_src_directory + dst = self.rename_dst_directory + os.mkdir(src) + os.mkdir(dst) + if ((sys.platform.startswith == 'linux') or (sys.platform == 'darwin')): + os.rename(src, dst) + self.assertEqual(os.path.exists(dst), True) + self.assertEqual(os.path.exists(src), False) + elif (sys.platform == 'win32'): + self.assertRaises(FileExistsError, os.rename, src, dst) + # both src and dst will still be intact on windows + self.assertEqual(os.path.exists(dst), True) + self.assertEqual(os.path.exists(src), True) + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_empty_dst_directory_not_empty(self): + src = self.rename_src_directory + dst = self.rename_dst_directory + os.mkdir(src) + os.mkdir(dst) + fd = os.open(dst + "/file.txt", os.O_CREAT | os.O_WRONLY) + os.write(fd, b"destination file already exists and has data") + os.close(fd) + self.assertRaises(OSError, os.rename, src, dst) + # destination file should be in tact + with open(dst + "/file.txt", "rb") as fobj: + self.assertEqual(fobj.read(), + b"destination file already exists and has data") + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_not_empty_dst_file_exist(self): + src = self.rename_src_directory + dst = self.rename_dst_file + os.mkdir(src) + fd = os.open(src + "/file.txt", 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(dst, os.O_CREAT | os.O_WRONLY) + os.write(fd, b"dst file already exists and has data") + os.close(fd) + self.assertRaises(OSError, os.rename, src, dst) + with open(dst, "rb") as fobj: + self.assertEqual(fobj.read(), + b"dst file already exists and has data") + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_not_empty_dst_not_exist(self): + src = self.rename_src_directory + dst = self.rename_dst_directory + os.mkdir(src) + fd = os.open(src + "/file.txt", 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(src, dst) + with open(dst + "/file.txt", "rb") as fobj: + self.assertEqual(fobj.read().splitlines(), + [b"beans", b"bacon", b"eggs", b"spam"]) + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_not_empty_dst_directory_empty(self): + src = self.rename_src_directory + dst = self.rename_dst_directory + os.mkdir(src) + fd = os.open(src + "/file.txt", 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.mkdir(dst) + if ((sys.platform.startswith == 'linux') or (sys.platform == 'darwin')): + os.rename(src, dst) + with open(dst + "/file.txt", "rb") as fobj: + self.assertEqual(fobj.read().splitlines(), + [b"beans", b"bacon", b"eggs", b"spam"]) + elif (sys.platform == 'win32'): + self.assertRaises(FileExistsError, os.rename, src, dst) + # both src and dst will still be intact on windows + self.assertEqual(os.path.exists(dst), True) + self.assertEqual(os.path.exists(src), True) + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_directory_not_empty_dst_directory_not_empty(self): + src = self.rename_src_directory + dst = self.rename_dst_directory + os.mkdir(src) + fd = os.open(src + "/file.txt", 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.mkdir(dst) + fd = os.open(dst + "/file.txt", os.O_CREAT | os.O_WRONLY) + os.write(fd, b"destination file already exists and has data") + os.close(fd) + self.assertRaises(OSError, os.rename, src, dst) + # destination file should be in tact + with open(dst + "/file.txt", "rb") as fobj: + self.assertEqual(fobj.read(), + b"destination file already exists and has data") + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_or_directory_not_exist_dst_file_exist(self): + src = self.rename_src_file + "does_not_exist" + dst = self.rename_dst_file + fd = os.open(dst, os.O_CREAT | os.O_WRONLY) + os.write(fd, b"destination file already exists and has data") + os.close(fd) + self.assertRaises(OSError, os.rename, src, dst) + # destination file should be in tact + with open(dst, "rb") as fobj: + self.assertEqual(fobj.read(), + b"destination file already exists and has data") + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_or_directory_not_exist_dst_not_exist(self): + src = self.rename_src_file + "does_not_exist" + dst = self.rename_dst_file + "does_not_exist" + self.assertRaises(OSError, os.rename, src, dst) + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_or_directory_not_exist_dst_directory_empty(self): + src = self.rename_src_file + "does_not_exist" + dst = self.rename_dst_directory + os.mkdir(dst) + self.assertRaises(OSError, os.rename, src, dst) + self.assertEqual(os.path.exists(dst), True) + + # http://bugs.python.org/issue16278 + @support.cpython_only + @unittest.skipUnless(( (sys.platform == 'darwin') or \ + (sys.platform.startswith == 'linux') or \ + (sys.platform == 'win32') ), \ + 'test specific to windows or linux or darwin only') + def test_rename_src_file_or_directory_not_exist_dst_directory_not_empty(self): + src = self.rename_src_file + "does_not_exist" + dst = self.rename_dst_directory + os.mkdir(dst) + fd = os.open(dst + "/file.txt", os.O_CREAT | os.O_WRONLY) + os.write(fd, b"destination file already exists and has data") + os.close(fd) + self.assertRaises(OSError, os.rename, src, dst) + self.assertEqual(os.path.exists(dst), True) + # destination file should be in tact + with open(dst + "/file.txt", "rb") as fobj: + self.assertEqual(fobj.read(), + b"destination file already exists and has data") + def test_read(self): with open(support.TESTFN, "w+b") as fobj: fobj.write(b"spam")