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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 7 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_selectors.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 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 for attr in 'st_atime', 'st_mtime': 378 for attr in 'st_atime', 'st_mtime':
379 # The modification times may be truncated in the new file. 379 # The modification times may be truncated in the new file.
380 self.assertLessEqual(getattr(src_link_stat, attr), 380 self.assertLessEqual(getattr(src_link_stat, attr),
381 getattr(dst_link_stat, attr) + 1) 381 getattr(dst_link_stat, attr) + 1)
382 if hasattr(os, 'lchmod'): 382 if hasattr(os, 'lchmod'):
383 self.assertEqual(src_link_stat.st_mode, dst_link_stat.st_mode) 383 self.assertEqual(src_link_stat.st_mode, dst_link_stat.st_mode)
384 if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): 384 if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'):
385 self.assertEqual(src_link_stat.st_flags, dst_link_stat.st_flags) 385 self.assertEqual(src_link_stat.st_flags, dst_link_stat.st_flags)
386 # tell to follow but dst is not a link 386 # tell to follow but dst is not a link
387 shutil.copystat(src_link, dst, follow_symlinks=False) 387 shutil.copystat(src_link, dst, follow_symlinks=False)
388 self.assertTrue(abs(os.stat(src).st_mtime - os.stat(dst).st_mtime) < 388 self.assertAlmostEqual(os.stat(src).st_mtime, os.stat(dst).st_mtime,
389 00000.1) 389 delta=0.1)
390 390
391 @unittest.skipUnless(hasattr(os, 'chflags') and 391 @unittest.skipUnless(hasattr(os, 'chflags') and
392 hasattr(errno, 'EOPNOTSUPP') and 392 hasattr(errno, 'EOPNOTSUPP') and
393 hasattr(errno, 'ENOTSUP'), 393 hasattr(errno, 'ENOTSUP'),
394 "requires os.chflags, EOPNOTSUPP & ENOTSUP") 394 "requires os.chflags, EOPNOTSUPP & ENOTSUP")
395 def test_copystat_handles_harmless_chflags_errors(self): 395 def test_copystat_handles_harmless_chflags_errors(self):
396 tmpdir = self.mkdtemp() 396 tmpdir = self.mkdtemp()
397 file1 = os.path.join(tmpdir, 'file1') 397 file1 = os.path.join(tmpdir, 'file1')
398 file2 = os.path.join(tmpdir, 'file2') 398 file2 = os.path.join(tmpdir, 'file2')
399 write_file(file1, 'xxx') 399 write_file(file1, 'xxx')
(...skipping 1232 matching lines...) Expand 10 before | Expand all | Expand 10 after
1632 if filename == 'srcfile': 1632 if filename == 'srcfile':
1633 return srcfile 1633 return srcfile
1634 if filename == 'destfile': 1634 if filename == 'destfile':
1635 raise OSError('Cannot open "destfile"') 1635 raise OSError('Cannot open "destfile"')
1636 assert 0 # shouldn't reach here. 1636 assert 0 # shouldn't reach here.
1637 1637
1638 self._set_shutil_open(_open) 1638 self._set_shutil_open(_open)
1639 1639
1640 shutil.copyfile('srcfile', 'destfile') 1640 shutil.copyfile('srcfile', 'destfile')
1641 self.assertTrue(srcfile._entered) 1641 self.assertTrue(srcfile._entered)
1642 self.assertTrue(srcfile._exited_with[0] is OSError) 1642 self.assertIs(srcfile._exited_with[0], OSError)
1643 self.assertEqual(srcfile._exited_with[1].args, 1643 self.assertEqual(srcfile._exited_with[1].args,
1644 ('Cannot open "destfile"',)) 1644 ('Cannot open "destfile"',))
1645 1645
1646 def test_w_dest_close_fails(self): 1646 def test_w_dest_close_fails(self):
1647 1647
1648 srcfile = self.Faux() 1648 srcfile = self.Faux()
1649 destfile = self.Faux(True) 1649 destfile = self.Faux(True)
1650 1650
1651 def _open(filename, mode='r'): 1651 def _open(filename, mode='r'):
1652 if filename == 'srcfile': 1652 if filename == 'srcfile':
1653 return srcfile 1653 return srcfile
1654 if filename == 'destfile': 1654 if filename == 'destfile':
1655 return destfile 1655 return destfile
1656 assert 0 # shouldn't reach here. 1656 assert 0 # shouldn't reach here.
1657 1657
1658 self._set_shutil_open(_open) 1658 self._set_shutil_open(_open)
1659 1659
1660 shutil.copyfile('srcfile', 'destfile') 1660 shutil.copyfile('srcfile', 'destfile')
1661 self.assertTrue(srcfile._entered) 1661 self.assertTrue(srcfile._entered)
1662 self.assertTrue(destfile._entered) 1662 self.assertTrue(destfile._entered)
1663 self.assertTrue(destfile._raised) 1663 self.assertTrue(destfile._raised)
1664 self.assertTrue(srcfile._exited_with[0] is OSError) 1664 self.assertIs(srcfile._exited_with[0], OSError)
1665 self.assertEqual(srcfile._exited_with[1].args, 1665 self.assertEqual(srcfile._exited_with[1].args,
1666 ('Cannot close',)) 1666 ('Cannot close',))
1667 1667
1668 def test_w_source_close_fails(self): 1668 def test_w_source_close_fails(self):
1669 1669
1670 srcfile = self.Faux(True) 1670 srcfile = self.Faux(True)
1671 destfile = self.Faux() 1671 destfile = self.Faux()
1672 1672
1673 def _open(filename, mode='r'): 1673 def _open(filename, mode='r'):
1674 if filename == 'srcfile': 1674 if filename == 'srcfile':
1675 return srcfile 1675 return srcfile
1676 if filename == 'destfile': 1676 if filename == 'destfile':
1677 return destfile 1677 return destfile
1678 assert 0 # shouldn't reach here. 1678 assert 0 # shouldn't reach here.
1679 1679
1680 self._set_shutil_open(_open) 1680 self._set_shutil_open(_open)
1681 1681
1682 self.assertRaises(OSError, 1682 self.assertRaises(OSError,
1683 shutil.copyfile, 'srcfile', 'destfile') 1683 shutil.copyfile, 'srcfile', 'destfile')
1684 self.assertTrue(srcfile._entered) 1684 self.assertTrue(srcfile._entered)
1685 self.assertTrue(destfile._entered) 1685 self.assertTrue(destfile._entered)
1686 self.assertFalse(destfile._raised) 1686 self.assertFalse(destfile._raised)
1687 self.assertTrue(srcfile._exited_with[0] is None) 1687 self.assertIsNone(srcfile._exited_with[0])
1688 self.assertTrue(srcfile._raised) 1688 self.assertTrue(srcfile._raised)
1689 1689
1690 def test_move_dir_caseinsensitive(self): 1690 def test_move_dir_caseinsensitive(self):
1691 # Renames a folder to the same name 1691 # Renames a folder to the same name
1692 # but a different case. 1692 # but a different case.
1693 1693
1694 self.src_dir = tempfile.mkdtemp() 1694 self.src_dir = tempfile.mkdtemp()
1695 self.addCleanup(shutil.rmtree, self.src_dir, True) 1695 self.addCleanup(shutil.rmtree, self.src_dir, True)
1696 dst_dir = os.path.join( 1696 dst_dir = os.path.join(
1697 os.path.dirname(self.src_dir), 1697 os.path.dirname(self.src_dir),
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1745 with support.EnvironmentVarGuard() as env: 1745 with support.EnvironmentVarGuard() as env:
1746 del env['LINES'] 1746 del env['LINES']
1747 del env['COLUMNS'] 1747 del env['COLUMNS']
1748 actual = shutil.get_terminal_size() 1748 actual = shutil.get_terminal_size()
1749 1749
1750 self.assertEqual(expected, actual) 1750 self.assertEqual(expected, actual)
1751 1751
1752 1752
1753 if __name__ == '__main__': 1753 if __name__ == '__main__':
1754 unittest.main() 1754 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_selectors.py ('k') | Lib/test/test_slice.py » ('j') | no next file with comments »

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