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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 6 years, 5 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 | « Lib/test/test_set.py ('k') | Lib/test/test_slice.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 for attr in 'st_atime', 'st_mtime': 376 for attr in 'st_atime', 'st_mtime':
377 # The modification times may be truncated in the new file. 377 # The modification times may be truncated in the new file.
378 self.assertLessEqual(getattr(src_link_stat, attr), 378 self.assertLessEqual(getattr(src_link_stat, attr),
379 getattr(dst_link_stat, attr) + 1) 379 getattr(dst_link_stat, attr) + 1)
380 if hasattr(os, 'lchmod'): 380 if hasattr(os, 'lchmod'):
381 self.assertEqual(src_link_stat.st_mode, dst_link_stat.st_mode) 381 self.assertEqual(src_link_stat.st_mode, dst_link_stat.st_mode)
382 if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): 382 if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'):
383 self.assertEqual(src_link_stat.st_flags, dst_link_stat.st_flags) 383 self.assertEqual(src_link_stat.st_flags, dst_link_stat.st_flags)
384 # tell to follow but dst is not a link 384 # tell to follow but dst is not a link
385 shutil.copystat(src_link, dst, follow_symlinks=False) 385 shutil.copystat(src_link, dst, follow_symlinks=False)
386 self.assertTrue(abs(os.stat(src).st_mtime - os.stat(dst).st_mtime) < 386 self.assertAlmostEqual(os.stat(src).st_mtime, os.stat(dst).st_mtime,
387 00000.1) 387 delta=0.1)
388 388
389 @unittest.skipUnless(hasattr(os, 'chflags') and 389 @unittest.skipUnless(hasattr(os, 'chflags') and
390 hasattr(errno, 'EOPNOTSUPP') and 390 hasattr(errno, 'EOPNOTSUPP') and
391 hasattr(errno, 'ENOTSUP'), 391 hasattr(errno, 'ENOTSUP'),
392 "requires os.chflags, EOPNOTSUPP & ENOTSUP") 392 "requires os.chflags, EOPNOTSUPP & ENOTSUP")
393 def test_copystat_handles_harmless_chflags_errors(self): 393 def test_copystat_handles_harmless_chflags_errors(self):
394 tmpdir = self.mkdtemp() 394 tmpdir = self.mkdtemp()
395 file1 = os.path.join(tmpdir, 'file1') 395 file1 = os.path.join(tmpdir, 'file1')
396 file2 = os.path.join(tmpdir, 'file2') 396 file2 = os.path.join(tmpdir, 'file2')
397 write_file(file1, 'xxx') 397 write_file(file1, 'xxx')
(...skipping 1146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1544 if filename == 'srcfile': 1544 if filename == 'srcfile':
1545 return srcfile 1545 return srcfile
1546 if filename == 'destfile': 1546 if filename == 'destfile':
1547 raise OSError('Cannot open "destfile"') 1547 raise OSError('Cannot open "destfile"')
1548 assert 0 # shouldn't reach here. 1548 assert 0 # shouldn't reach here.
1549 1549
1550 self._set_shutil_open(_open) 1550 self._set_shutil_open(_open)
1551 1551
1552 shutil.copyfile('srcfile', 'destfile') 1552 shutil.copyfile('srcfile', 'destfile')
1553 self.assertTrue(srcfile._entered) 1553 self.assertTrue(srcfile._entered)
1554 self.assertTrue(srcfile._exited_with[0] is OSError) 1554 self.assertIs(srcfile._exited_with[0], OSError)
1555 self.assertEqual(srcfile._exited_with[1].args, 1555 self.assertEqual(srcfile._exited_with[1].args,
1556 ('Cannot open "destfile"',)) 1556 ('Cannot open "destfile"',))
1557 1557
1558 def test_w_dest_close_fails(self): 1558 def test_w_dest_close_fails(self):
1559 1559
1560 srcfile = self.Faux() 1560 srcfile = self.Faux()
1561 destfile = self.Faux(True) 1561 destfile = self.Faux(True)
1562 1562
1563 def _open(filename, mode='r'): 1563 def _open(filename, mode='r'):
1564 if filename == 'srcfile': 1564 if filename == 'srcfile':
1565 return srcfile 1565 return srcfile
1566 if filename == 'destfile': 1566 if filename == 'destfile':
1567 return destfile 1567 return destfile
1568 assert 0 # shouldn't reach here. 1568 assert 0 # shouldn't reach here.
1569 1569
1570 self._set_shutil_open(_open) 1570 self._set_shutil_open(_open)
1571 1571
1572 shutil.copyfile('srcfile', 'destfile') 1572 shutil.copyfile('srcfile', 'destfile')
1573 self.assertTrue(srcfile._entered) 1573 self.assertTrue(srcfile._entered)
1574 self.assertTrue(destfile._entered) 1574 self.assertTrue(destfile._entered)
1575 self.assertTrue(destfile._raised) 1575 self.assertTrue(destfile._raised)
1576 self.assertTrue(srcfile._exited_with[0] is OSError) 1576 self.assertIs(srcfile._exited_with[0], OSError)
1577 self.assertEqual(srcfile._exited_with[1].args, 1577 self.assertEqual(srcfile._exited_with[1].args,
1578 ('Cannot close',)) 1578 ('Cannot close',))
1579 1579
1580 def test_w_source_close_fails(self): 1580 def test_w_source_close_fails(self):
1581 1581
1582 srcfile = self.Faux(True) 1582 srcfile = self.Faux(True)
1583 destfile = self.Faux() 1583 destfile = self.Faux()
1584 1584
1585 def _open(filename, mode='r'): 1585 def _open(filename, mode='r'):
1586 if filename == 'srcfile': 1586 if filename == 'srcfile':
1587 return srcfile 1587 return srcfile
1588 if filename == 'destfile': 1588 if filename == 'destfile':
1589 return destfile 1589 return destfile
1590 assert 0 # shouldn't reach here. 1590 assert 0 # shouldn't reach here.
1591 1591
1592 self._set_shutil_open(_open) 1592 self._set_shutil_open(_open)
1593 1593
1594 self.assertRaises(OSError, 1594 self.assertRaises(OSError,
1595 shutil.copyfile, 'srcfile', 'destfile') 1595 shutil.copyfile, 'srcfile', 'destfile')
1596 self.assertTrue(srcfile._entered) 1596 self.assertTrue(srcfile._entered)
1597 self.assertTrue(destfile._entered) 1597 self.assertTrue(destfile._entered)
1598 self.assertFalse(destfile._raised) 1598 self.assertFalse(destfile._raised)
1599 self.assertTrue(srcfile._exited_with[0] is None) 1599 self.assertIsNone(srcfile._exited_with[0])
1600 self.assertTrue(srcfile._raised) 1600 self.assertTrue(srcfile._raised)
1601 1601
1602 def test_move_dir_caseinsensitive(self): 1602 def test_move_dir_caseinsensitive(self):
1603 # Renames a folder to the same name 1603 # Renames a folder to the same name
1604 # but a different case. 1604 # but a different case.
1605 1605
1606 self.src_dir = tempfile.mkdtemp() 1606 self.src_dir = tempfile.mkdtemp()
1607 self.addCleanup(shutil.rmtree, self.src_dir, True) 1607 self.addCleanup(shutil.rmtree, self.src_dir, True)
1608 dst_dir = os.path.join( 1608 dst_dir = os.path.join(
1609 os.path.dirname(self.src_dir), 1609 os.path.dirname(self.src_dir),
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1661 1661
1662 self.assertEqual(expected, actual) 1662 self.assertEqual(expected, actual)
1663 1663
1664 1664
1665 def test_main(): 1665 def test_main():
1666 support.run_unittest(TestShutil, TestMove, TestCopyFile, 1666 support.run_unittest(TestShutil, TestMove, TestCopyFile,
1667 TermsizeTests, TestWhich) 1667 TermsizeTests, TestWhich)
1668 1668
1669 if __name__ == '__main__': 1669 if __name__ == '__main__':
1670 test_main() 1670 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_set.py ('k') | Lib/test/test_slice.py » ('j') | no next file with comments »

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