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

Delta Between Two Patch Sets: Lib/test/test_shutil.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 6 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_selectors.py ('k') | Lib/test/test_slice.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Copyright (C) 2003 Python Software Foundation 1 # Copyright (C) 2003 Python Software Foundation
2 2
3 import unittest 3 import unittest
4 import shutil 4 import shutil
5 import tempfile 5 import tempfile
6 import sys 6 import sys
7 import stat 7 import stat
8 import os 8 import os
9 import os.path 9 import os.path
10 import errno 10 import errno
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 write_file(src, 'foo') 281 write_file(src, 'foo')
282 write_file(dst, 'foo') 282 write_file(dst, 'foo')
283 os.symlink(src, src_link) 283 os.symlink(src, src_link)
284 os.symlink(dst, dst_link) 284 os.symlink(dst, dst_link)
285 os.chmod(src, stat.S_IRWXU|stat.S_IRWXG) 285 os.chmod(src, stat.S_IRWXU|stat.S_IRWXG)
286 # file to file 286 # file to file
287 os.chmod(dst, stat.S_IRWXO) 287 os.chmod(dst, stat.S_IRWXO)
288 self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode) 288 self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
289 shutil.copymode(src, dst) 289 shutil.copymode(src, dst)
290 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) 290 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
291 # follow src link 291 # On Windows, os.chmod does not follow symlinks (issue #15411)
292 os.chmod(dst, stat.S_IRWXO) 292 if os.name != 'nt':
293 shutil.copymode(src_link, dst) 293 # follow src link
294 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) 294 os.chmod(dst, stat.S_IRWXO)
295 # follow dst link 295 shutil.copymode(src_link, dst)
296 os.chmod(dst, stat.S_IRWXO) 296 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
297 shutil.copymode(src, dst_link) 297 # follow dst link
298 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) 298 os.chmod(dst, stat.S_IRWXO)
299 # follow both links 299 shutil.copymode(src, dst_link)
300 os.chmod(dst, stat.S_IRWXO) 300 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
301 shutil.copymode(src_link, dst) 301 # follow both links
302 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) 302 os.chmod(dst, stat.S_IRWXO)
303 shutil.copymode(src_link, dst_link)
304 self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
303 305
304 @unittest.skipUnless(hasattr(os, 'lchmod'), 'requires os.lchmod') 306 @unittest.skipUnless(hasattr(os, 'lchmod'), 'requires os.lchmod')
305 @support.skip_unless_symlink 307 @support.skip_unless_symlink
306 def test_copymode_symlink_to_symlink(self): 308 def test_copymode_symlink_to_symlink(self):
307 tmp_dir = self.mkdtemp() 309 tmp_dir = self.mkdtemp()
308 src = os.path.join(tmp_dir, 'foo') 310 src = os.path.join(tmp_dir, 'foo')
309 dst = os.path.join(tmp_dir, 'bar') 311 dst = os.path.join(tmp_dir, 'bar')
310 src_link = os.path.join(tmp_dir, 'baz') 312 src_link = os.path.join(tmp_dir, 'baz')
311 dst_link = os.path.join(tmp_dir, 'quux') 313 dst_link = os.path.join(tmp_dir, 'quux')
312 write_file(src, 'foo') 314 write_file(src, 'foo')
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 write_file(src, 'foo') 424 write_file(src, 'foo')
423 dst = os.path.join(tmp_dir, 'bar') 425 dst = os.path.join(tmp_dir, 'bar')
424 write_file(dst, 'bar') 426 write_file(dst, 'bar')
425 427
426 # no xattr == no problem 428 # no xattr == no problem
427 shutil._copyxattr(src, dst) 429 shutil._copyxattr(src, dst)
428 # common case 430 # common case
429 os.setxattr(src, 'user.foo', b'42') 431 os.setxattr(src, 'user.foo', b'42')
430 os.setxattr(src, 'user.bar', b'43') 432 os.setxattr(src, 'user.bar', b'43')
431 shutil._copyxattr(src, dst) 433 shutil._copyxattr(src, dst)
432 self.assertEqual(os.listxattr(src), os.listxattr(dst)) 434 self.assertEqual(sorted(os.listxattr(src)), sorted(os.listxattr(dst)))
433 self.assertEqual( 435 self.assertEqual(
434 os.getxattr(src, 'user.foo'), 436 os.getxattr(src, 'user.foo'),
435 os.getxattr(dst, 'user.foo')) 437 os.getxattr(dst, 'user.foo'))
436 # check errors don't affect other attrs 438 # check errors don't affect other attrs
437 os.remove(dst) 439 os.remove(dst)
438 write_file(dst, 'bar') 440 write_file(dst, 'bar')
439 os_error = OSError(errno.EPERM, 'EPERM') 441 os_error = OSError(errno.EPERM, 'EPERM')
440 442
441 def _raise_on_user_foo(fname, attr, val, **kwargs): 443 def _raise_on_user_foo(fname, attr, val, **kwargs):
442 if attr == 'user.foo': 444 if attr == 'user.foo':
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
746 self.assertEqual(os.stat(src_dir).st_mode, os.stat(dst_dir).st_mode) 748 self.assertEqual(os.stat(src_dir).st_mode, os.stat(dst_dir).st_mode)
747 self.assertEqual(os.stat(os.path.join(src_dir, 'permissive.txt')).st_mod e, 749 self.assertEqual(os.stat(os.path.join(src_dir, 'permissive.txt')).st_mod e,
748 os.stat(os.path.join(dst_dir, 'permissive.txt')).st_mo de) 750 os.stat(os.path.join(dst_dir, 'permissive.txt')).st_mo de)
749 self.assertEqual(os.stat(os.path.join(src_dir, 'restrictive.txt')).st_mo de, 751 self.assertEqual(os.stat(os.path.join(src_dir, 'restrictive.txt')).st_mo de,
750 os.stat(os.path.join(dst_dir, 'restrictive.txt')).st_m ode) 752 os.stat(os.path.join(dst_dir, 'restrictive.txt')).st_m ode)
751 restrictive_subdir_dst = os.path.join(dst_dir, 753 restrictive_subdir_dst = os.path.join(dst_dir,
752 os.path.split(restrictive_subdir)[ 1]) 754 os.path.split(restrictive_subdir)[ 1])
753 self.assertEqual(os.stat(restrictive_subdir).st_mode, 755 self.assertEqual(os.stat(restrictive_subdir).st_mode,
754 os.stat(restrictive_subdir_dst).st_mode) 756 os.stat(restrictive_subdir_dst).st_mode)
755 757
758 @unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
756 @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') 759 @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
757 def test_dont_copy_file_onto_link_to_itself(self): 760 def test_dont_copy_file_onto_link_to_itself(self):
758 # Temporarily disable test on Windows.
759 if os.name == 'nt':
760 return
761 # bug 851123. 761 # bug 851123.
762 os.mkdir(TESTFN) 762 os.mkdir(TESTFN)
763 src = os.path.join(TESTFN, 'cheese') 763 src = os.path.join(TESTFN, 'cheese')
764 dst = os.path.join(TESTFN, 'shop') 764 dst = os.path.join(TESTFN, 'shop')
765 try: 765 try:
766 with open(src, 'w') as f: 766 with open(src, 'w') as f:
767 f.write('cheddar') 767 f.write('cheddar')
768 os.link(src, dst) 768 os.link(src, dst)
769 self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) 769 self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst)
770 with open(src, 'r') as f: 770 with open(src, 'r') as f:
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 1549
1550 @support.skip_unless_symlink 1550 @support.skip_unless_symlink
1551 @mock_rename 1551 @mock_rename
1552 def test_move_dangling_symlink(self): 1552 def test_move_dangling_symlink(self):
1553 src = os.path.join(self.src_dir, 'baz') 1553 src = os.path.join(self.src_dir, 'baz')
1554 dst = os.path.join(self.src_dir, 'bar') 1554 dst = os.path.join(self.src_dir, 'bar')
1555 os.symlink(src, dst) 1555 os.symlink(src, dst)
1556 dst_link = os.path.join(self.dst_dir, 'quux') 1556 dst_link = os.path.join(self.dst_dir, 'quux')
1557 shutil.move(dst, dst_link) 1557 shutil.move(dst, dst_link)
1558 self.assertTrue(os.path.islink(dst_link)) 1558 self.assertTrue(os.path.islink(dst_link))
1559 self.assertEqual(os.path.realpath(src), os.path.realpath(dst_link)) 1559 # On Windows, os.path.realpath does not follow symlinks (issue #9949)
1560 if os.name == 'nt':
1561 self.assertEqual(os.path.realpath(src), os.readlink(dst_link))
1562 else:
1563 self.assertEqual(os.path.realpath(src), os.path.realpath(dst_link))
1560 1564
1561 @support.skip_unless_symlink 1565 @support.skip_unless_symlink
1562 @mock_rename 1566 @mock_rename
1563 def test_move_dir_symlink(self): 1567 def test_move_dir_symlink(self):
1564 src = os.path.join(self.src_dir, 'baz') 1568 src = os.path.join(self.src_dir, 'baz')
1565 dst = os.path.join(self.src_dir, 'bar') 1569 dst = os.path.join(self.src_dir, 'bar')
1566 os.mkdir(src) 1570 os.mkdir(src)
1567 os.symlink(src, dst) 1571 os.symlink(src, dst)
1568 dst_link = os.path.join(self.dst_dir, 'quux') 1572 dst_link = os.path.join(self.dst_dir, 'quux')
1569 shutil.move(dst, dst_link) 1573 shutil.move(dst, dst_link)
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1741 with support.EnvironmentVarGuard() as env: 1745 with support.EnvironmentVarGuard() as env:
1742 del env['LINES'] 1746 del env['LINES']
1743 del env['COLUMNS'] 1747 del env['COLUMNS']
1744 actual = shutil.get_terminal_size() 1748 actual = shutil.get_terminal_size()
1745 1749
1746 self.assertEqual(expected, actual) 1750 self.assertEqual(expected, actual)
1747 1751
1748 1752
1749 if __name__ == '__main__': 1753 if __name__ == '__main__':
1750 unittest.main() 1754 unittest.main()
LEFTRIGHT

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