Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(170031)

Side by Side Diff: Lib/test/test_os.py

Issue 16278: os.rename documentation slightly inaccurate
Patch Set: Created 6 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/os.rst ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # As a test suite for the os module, this is woefully inadequate, but this 1 # As a test suite for the os module, this is woefully inadequate, but this
2 # does add tests for a few functions which have been determined to be more 2 # does add tests for a few functions which have been determined to be more
3 # portable than they had been thought to be. 3 # portable than they had been thought to be.
4 4
5 import os 5 import os
6 import errno 6 import errno
7 import unittest 7 import unittest
8 import warnings 8 import warnings
9 import sys 9 import sys
10 import signal 10 import signal
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 # and unmaintained) linuxthreads threading library. There's an issue 44 # and unmaintained) linuxthreads threading library. There's an issue
45 # when combining linuxthreads with a failed execv call: see 45 # when combining linuxthreads with a failed execv call: see
46 # http://bugs.python.org/issue4970. 46 # http://bugs.python.org/issue4970.
47 if hasattr(sys, 'thread_info') and sys.thread_info.version: 47 if hasattr(sys, 'thread_info') and sys.thread_info.version:
48 USING_LINUXTHREADS = sys.thread_info.version.startswith("linuxthreads") 48 USING_LINUXTHREADS = sys.thread_info.version.startswith("linuxthreads")
49 else: 49 else:
50 USING_LINUXTHREADS = False 50 USING_LINUXTHREADS = False
51 51
52 # Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group. 52 # Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group.
53 HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0 53 HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0
54
54 55
55 # Tests creating TESTFN 56 # Tests creating TESTFN
56 class FileTests(unittest.TestCase): 57 class FileTests(unittest.TestCase):
57 def setUp(self): 58 def setUp(self):
58 if os.path.exists(support.TESTFN): 59 if os.path.exists(support.TESTFN):
59 os.unlink(support.TESTFN) 60 os.unlink(support.TESTFN)
60 tearDown = setUp 61 tearDown = setUp
61 62
62 def test_access(self): 63 def test_access(self):
63 f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) 64 f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
(...skipping 2132 matching lines...) Expand 10 before | Expand all | Expand 10 after
2196 funcs.append((self.filenames, os.readlink,)) 2197 funcs.append((self.filenames, os.readlink,))
2197 2198
2198 for filenames, func, *func_args in funcs: 2199 for filenames, func, *func_args in funcs:
2199 for name in filenames: 2200 for name in filenames:
2200 try: 2201 try:
2201 func(name, *func_args) 2202 func(name, *func_args)
2202 except OSError as err: 2203 except OSError as err:
2203 self.assertIs(err.filename, name) 2204 self.assertIs(err.filename, name)
2204 else: 2205 else:
2205 self.fail("No exception thrown by {}".format(func)) 2206 self.fail("No exception thrown by {}".format(func))
2207
2208 # Tests os.rename
2209 class RenameTests(unittest.TestCase):
vstepanov 2013/03/11 00:45:35 I have some comments to this patch. I think would
Todd.Rovito 2013/03/11 03:15:07 I actually like it in a single class because alot
2210 def setUp(self):
2211 if os.path.exists(support.TESTFN):
2212 os.unlink(support.TESTFN)
2213
2214 # create rename_src_file and place data in the file
2215 self.rename_src_file = "%s_%d_tmp" % ("test_rename_src_file",
vstepanov 2013/03/11 00:45:35 All variables has prefix ``rename_`` (rename_src_f
Todd.Rovito 2013/03/11 03:15:07 Good point....this is a side effect from me moving
2216 os.getpid())
vstepanov 2013/03/11 00:45:35 should not call this method everywhere, use a vari
Todd.Rovito 2013/03/11 03:15:07 Yes that will be more efficient, thanks!
2217 if os.path.exists(self.rename_src_file):
vstepanov 2013/03/11 00:45:35 There is remove_file_or_directory method, use it f
Todd.Rovito 2013/03/11 03:15:07 Good point, I don't know how I missed the remove_f
2218 os.unlink(self.rename_src_file)
2219 fd = os.open(self.rename_src_file, os.O_CREAT | os.O_WRONLY)
2220 os.write(fd, b"beans\n")
vstepanov 2013/03/11 00:45:35 Really need to write many lines in a file and then
Todd.Rovito 2013/03/11 03:15:07 I got the original code from test_os.py line 111 i
2221 os.write(fd, b"bacon\n")
2222 os.write(fd, b"eggs\n")
2223 os.write(fd, b"spam\n")
2224 os.close(fd)
2225
2226 # create rename_dst_file and place data in the file
2227 self.rename_dst_file = "%s_%d_tmp" % ("test_rename_dst_file",
2228 os.getpid())
2229 if os.path.exists(self.rename_dst_file):
2230 os.unlink(self.rename_dst_file)
2231 fd = os.open(self.rename_dst_file, os.O_CREAT | os.O_WRONLY)
2232 os.write(fd, b"dst file already exists and has data")
2233 os.close(fd)
2234
2235 # create rename_dst_directory
2236 self.rename_dst_directory = "%s_%d_tmp" % (
2237 "test_rename_dst_directory", os.getpid())
2238 self.rename_dst_directory_file = os.path.join(self.rename_dst_directory,
2239 "file.txt")
2240 if os.path.exists(self.rename_dst_directory_file):
2241 os.unlink(self.rename_dst_directory_file)
2242 if os.path.exists(self.rename_dst_directory):
2243 os.rmdir(self.rename_dst_directory)
2244 os.mkdir(self.rename_dst_directory)
2245 fd = os.open(self.rename_dst_directory_file,
2246 os.O_CREAT | os.O_WRONLY)
2247 os.write(fd, b"dst file already exists and has data")
2248 os.close(fd)
2249
2250 # create rename_src_directory
2251 self.rename_src_directory = "%s_%d_tmp" % (
2252 "test_src_directory", os.getpid())
2253 self.rename_src_directory_file = os.path.join(self.rename_src_directory,
2254 "file.txt")
2255 if os.path.exists(self.rename_src_directory_file):
2256 os.unlink(self.rename_src_directory_file)
2257 if os.path.exists(self.rename_src_directory):
2258 os.rmdir(self.rename_src_directory)
2259 os.mkdir(self.rename_src_directory)
2260 fd = os.open(self.rename_src_directory_file,
2261 os.O_CREAT | os.O_WRONLY)
2262 os.write(fd, b"beans\n")
2263 os.write(fd, b"bacon\n")
2264 os.write(fd, b"eggs\n")
2265 os.write(fd, b"spam\n")
2266 os.close(fd)
2267
2268 def tearDown(self):
2269 files = [support.TESTFN, self.rename_src_file, self.rename_dst_file,
2270 self.rename_dst_directory_file, self.rename_dst_directory,
2271 self.rename_src_directory_file, self.rename_src_directory]
2272 for file in files:
2273 self.remove_file_or_directory(file)
2274
2275 def remove_file_or_directory(self, file):
2276 if os.path.exists(file):
2277 if (os.path.isfile(file)):
vstepanov 2013/03/11 00:45:35 outer parentheses are unnecessary
Todd.Rovito 2013/03/11 03:15:07 Yes too much scheme sorry I missed those, they are
2278 os.unlink(file)
2279 elif (os.path.isdir(file)):
2280 os.rmdir(file)
2281
2282 def test_rename_src_file_dest_file_exist(self):
2283 if sys.platform == 'darwin' or sys.platform.startswith == "linux":
2284 os.rename(self.rename_src_file, self.rename_dst_file)
2285 with open(self.rename_dst_file, "rb") as fobj:
2286 self.assertEqual(fobj.read().splitlines(),
2287 [b"beans", b"bacon", b"eggs", b"spam"])
2288 elif sys.platform == 'win':
2289 self.assertRaises(OSError, os.rename, src, dst)
vstepanov 2013/03/11 00:45:35 I do not see where `src` and `dst` variables are d
Todd.Rovito 2013/03/11 03:15:07 Oops I didn't catch that because I have not tested
2290 with open(self.rename_dst_file, "rb") as fobj:
2291 self.assertEqual(fobj.read(),
2292 b"dst file already exists and has data")
2293
2294 def test_rename_src_file_dst_not_exist(self):
2295 self.remove_file_or_directory(self.rename_dst_file)
vstepanov 2013/03/11 00:45:35 I think it would be better to create the files/dir
Todd.Rovito 2013/03/11 03:15:07 I have been wondering if there is a better approac
2296 os.rename(self.rename_src_file, self.rename_dst_file)
2297 with open(self.rename_dst_file, "rb") as fobj:
2298 self.assertEqual(fobj.read().splitlines(),
2299 [b"beans", b"bacon", b"eggs", b"spam"])
2300
2301 def test_rename_src_file_dst_directory_empty(self):
2302 self.remove_file_or_directory(self.rename_dst_directory_file)
2303 self.assertRaises(OSError, os.rename, self.rename_src_file,
2304 self.rename_dst_directory)
2305
2306 def test_rename_src_file_dst_directory_not_empty(self):
2307 self.assertRaises(OSError, os.rename, self.rename_src_file,
2308 self.rename_dst_directory)
2309 with open(self.rename_dst_directory_file, "rb") as fobj:
2310 self.assertEqual(fobj.read(),
2311 b"dst file already exists and has data")
2312
2313 def test_rename_src_directory_empty_dst_file_exist(self):
2314 self.remove_file_or_directory(self.rename_src_directory_file)
2315 self.assertRaises(OSError, os.rename, self.rename_src_directory,
2316 self.rename_dst_file)
2317 with open(self.rename_dst_file, "rb") as fobj:
2318 self.assertEqual(fobj.read(),
2319 b"dst file already exists and has data")
2320
2321 def test_rename_src_directory_empty_dst_not_exist(self):
2322 self.remove_file_or_directory(self.rename_src_directory_file)
2323 self.remove_file_or_directory(self.rename_dst_directory_file)
2324 self.remove_file_or_directory(self.rename_dst_directory)
2325 os.rename(self.rename_src_directory, self.rename_dst_directory)
2326 self.assertEqual(os.path.exists(self.rename_dst_directory), True)
vstepanov 2013/03/11 00:45:35 There are unittest.TestCase.assertTrue and unittes
Todd.Rovito 2013/03/11 03:15:07 Yes I fixed some of them with V2 but V3 now has al
2327 self.assertEqual(os.path.exists(self.rename_src_directory), False)
2328
2329 def test_rename_src_directory_empty_dst_directory_empty(self):
2330 self.remove_file_or_directory(self.rename_src_directory_file)
2331 self.remove_file_or_directory(self.rename_dst_directory_file)
2332 if sys.platform.startswith == 'linux' or sys.platform == 'darwin':
2333 os.rename(self.rename_src_directory, self.rename_dst_directory)
2334 self.assertEqual(os.path.exists(self.rename_dst_directory), True)
2335 self.assertEqual(os.path.exists(self.rename_src_directory), False)
2336 elif (sys.platform == 'win32'):
2337 self.assertRaises(FileExistsError, os.rename,
2338 self.rename_src_directory, self.rename_dst_directory)
2339 # both src and dst will still be intact on windows
2340 self.assertEqual(os.path.exists(self.rename_dst_directory), True)
2341 self.assertEqual(os.path.exists(self.rename_src_directory), True)
2342
2343 def test_rename_src_directory_empty_dst_directory_not_empty(self):
2344 self.remove_file_or_directory(self.rename_src_directory_file)
2345 self.assertRaises(OSError, os.rename, self.rename_src_directory,
2346 self.rename_dst_directory)
2347 # destination file should be in tact
2348 with open(self.rename_dst_directory_file, "rb") as fobj:
2349 self.assertEqual(fobj.read(),
2350 b"dst file already exists and has data")
2351
2352 def test_rename_src_directory_not_empty_dst_file_exist(self):
2353 self.assertRaises(OSError, os.rename, self.rename_src_directory,
2354 self.rename_dst_file)
2355 with open(self.rename_dst_file, "rb") as fobj:
2356 self.assertEqual(fobj.read(),
2357 b"dst file already exists and has data")
2358
2359 def test_rename_src_directory_not_empty_dst_not_exist(self):
2360 # remove the dst directory and file
2361 self.remove_file_or_directory(self.rename_dst_directory_file)
2362 self.remove_file_or_directory(self.rename_dst_directory)
2363 os.rename(self.rename_src_directory, self.rename_dst_directory)
2364 with open(self.rename_dst_directory_file, "rb") as fobj:
2365 self.assertEqual(fobj.read().splitlines(),
2366 [b"beans", b"bacon", b"eggs", b"spam"])
2367
2368 def test_rename_src_directory_not_empty_dst_directory_empty(self):
2369 self.remove_file_or_directory(self.rename_dst_directory_file)
2370 if sys.platform.startswith == 'linux' or sys.platform == 'darwin':
2371 os.rename(self.rename_src_directory, self.rename_dst_directory)
2372 with open(self.rename_dst_directory_file, "rb") as fobj:
2373 self.assertEqual(fobj.read().splitlines(),
2374 [b"beans", b"bacon", b"eggs", b"spam"])
2375 elif sys.platform == 'win32':
2376 self.assertRaises(FileExistsError, os.rename,
2377 self.rename_src_directory, self.rename_dst_directory)
2378 # both src and dst will still be intact on windows
2379 self.assertEqual(os.path.exists(self.rename_dst_directory), True)
2380 self.assertEqual(os.path.exists(self.rename_src_directory), True)
2381
2382 def test_rename_src_directory_not_empty_dst_directory_not_empty(self):
2383 self.assertRaises(OSError, os.rename, self.rename_src_directory,
2384 self.rename_dst_directory)
2385 # destination file should be in tact
2386 with open(self.rename_dst_directory_file, "rb") as fobj:
2387 self.assertEqual(fobj.read(),
2388 b"dst file already exists and has data")
2389
2390 def test_rename_src_file_or_directory_not_exist_dst_file_exist(self):
2391 self.remove_file_or_directory(self.rename_src_file)
2392 self.assertRaises(OSError, os.rename, self.rename_src_file,
2393 self.rename_dst_file)
2394 # destination file should be in tact
2395 with open(self.rename_dst_file, "rb") as fobj:
2396 self.assertEqual(fobj.read(),
2397 b"dst file already exists and has data")
2398
2399 def test_rename_src_file_or_directory_not_exist_dst_not_exist(self):
2400 self.remove_file_or_directory(self.rename_src_file)
2401 self.remove_file_or_directory(self.rename_dst_file)
2402 src = self.rename_src_file + "does_not_exist"
vstepanov 2013/03/11 00:45:35 I don't see where `src` and `dst` are used.
Todd.Rovito 2013/03/11 03:15:07 You are correct I don't need those thanks! I wil
2403 dst = self.rename_dst_file + "does_not_exist"
2404 self.assertRaises(OSError, os.rename, self.rename_src_file,
2405 self.rename_dst_file)
2406
2407 def test_rename_src_file_or_directory_not_exist_dst_directory_empty(self):
2408 self.remove_file_or_directory(self.rename_src_file)
2409 self.remove_file_or_directory(self.rename_dst_directory_file)
2410 self.assertRaises(OSError, os.rename, self.rename_src_file,
2411 self.rename_dst_directory)
2412 self.assertEqual(os.path.exists(self.rename_dst_directory), True)
2413
2414 def test_rename_src_file_or_directory_not_exist_dst_directory_not_empty(self ):
2415 self.remove_file_or_directory(self.rename_src_file)
2416 self.assertRaises(OSError, os.rename, self.rename_src_file,
2417 self.rename_dst_directory)
2418 self.assertTrue(os.path.exists(self.rename_dst_directory))
2419 # destination file should be in tact
2420 with open(self.rename_dst_directory_file, "rb") as fobj:
2421 self.assertEqual(fobj.read(),
2422 b"dst file already exists and has data")
2423
2206 2424
2207 @support.reap_threads 2425 @support.reap_threads
2208 def test_main(): 2426 def test_main():
2209 support.run_unittest( 2427 support.run_unittest(
2210 FileTests, 2428 FileTests,
2211 StatAttributeTests, 2429 StatAttributeTests,
2212 EnvironTests, 2430 EnvironTests,
2213 WalkTests, 2431 WalkTests,
2214 FwalkTests, 2432 FwalkTests,
2215 MakedirTests, 2433 MakedirTests,
(...skipping 11 matching lines...) Expand all
2227 PidTests, 2445 PidTests,
2228 LoginTests, 2446 LoginTests,
2229 LinkTests, 2447 LinkTests,
2230 TestSendfile, 2448 TestSendfile,
2231 ProgramPriorityTests, 2449 ProgramPriorityTests,
2232 ExtendedAttributeTests, 2450 ExtendedAttributeTests,
2233 Win32DeprecatedBytesAPI, 2451 Win32DeprecatedBytesAPI,
2234 TermsizeTests, 2452 TermsizeTests,
2235 OSErrorTests, 2453 OSErrorTests,
2236 RemoveDirsTests, 2454 RemoveDirsTests,
2455 RenameTests,
2237 ) 2456 )
2238 2457
2239 if __name__ == "__main__": 2458 if __name__ == "__main__":
2240 test_main() 2459 test_main()
OLDNEW
« no previous file with comments | « Doc/library/os.rst ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+