test_file_util.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """Tests for distutils.file_util."""
  2. import unittest
  3. import os
  4. import errno
  5. from unittest.mock import patch
  6. from distutils.file_util import move_file, copy_file
  7. from distutils import log
  8. from distutils.tests import support
  9. from distutils.errors import DistutilsFileError
  10. from test.support import run_unittest, unlink
  11. class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
  12. def _log(self, msg, *args):
  13. if len(args) > 0:
  14. self._logs.append(msg % args)
  15. else:
  16. self._logs.append(msg)
  17. def setUp(self):
  18. super(FileUtilTestCase, self).setUp()
  19. self._logs = []
  20. self.old_log = log.info
  21. log.info = self._log
  22. tmp_dir = self.mkdtemp()
  23. self.source = os.path.join(tmp_dir, 'f1')
  24. self.target = os.path.join(tmp_dir, 'f2')
  25. self.target_dir = os.path.join(tmp_dir, 'd1')
  26. def tearDown(self):
  27. log.info = self.old_log
  28. super(FileUtilTestCase, self).tearDown()
  29. def test_move_file_verbosity(self):
  30. f = open(self.source, 'w')
  31. try:
  32. f.write('some content')
  33. finally:
  34. f.close()
  35. move_file(self.source, self.target, verbose=0)
  36. wanted = []
  37. self.assertEqual(self._logs, wanted)
  38. # back to original state
  39. move_file(self.target, self.source, verbose=0)
  40. move_file(self.source, self.target, verbose=1)
  41. wanted = ['moving %s -> %s' % (self.source, self.target)]
  42. self.assertEqual(self._logs, wanted)
  43. # back to original state
  44. move_file(self.target, self.source, verbose=0)
  45. self._logs = []
  46. # now the target is a dir
  47. os.mkdir(self.target_dir)
  48. move_file(self.source, self.target_dir, verbose=1)
  49. wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
  50. self.assertEqual(self._logs, wanted)
  51. def test_move_file_exception_unpacking_rename(self):
  52. # see issue 22182
  53. with patch("os.rename", side_effect=OSError("wrong", 1)), \
  54. self.assertRaises(DistutilsFileError):
  55. with open(self.source, 'w') as fobj:
  56. fobj.write('spam eggs')
  57. move_file(self.source, self.target, verbose=0)
  58. def test_move_file_exception_unpacking_unlink(self):
  59. # see issue 22182
  60. with patch("os.rename", side_effect=OSError(errno.EXDEV, "wrong")), \
  61. patch("os.unlink", side_effect=OSError("wrong", 1)), \
  62. self.assertRaises(DistutilsFileError):
  63. with open(self.source, 'w') as fobj:
  64. fobj.write('spam eggs')
  65. move_file(self.source, self.target, verbose=0)
  66. def test_copy_file_hard_link(self):
  67. with open(self.source, 'w') as f:
  68. f.write('some content')
  69. # Check first that copy_file() will not fall back on copying the file
  70. # instead of creating the hard link.
  71. try:
  72. os.link(self.source, self.target)
  73. except OSError as e:
  74. self.skipTest('os.link: %s' % e)
  75. else:
  76. unlink(self.target)
  77. st = os.stat(self.source)
  78. copy_file(self.source, self.target, link='hard')
  79. st2 = os.stat(self.source)
  80. st3 = os.stat(self.target)
  81. self.assertTrue(os.path.samestat(st, st2), (st, st2))
  82. self.assertTrue(os.path.samestat(st2, st3), (st2, st3))
  83. with open(self.source, 'r') as f:
  84. self.assertEqual(f.read(), 'some content')
  85. def test_copy_file_hard_link_failure(self):
  86. # If hard linking fails, copy_file() falls back on copying file
  87. # (some special filesystems don't support hard linking even under
  88. # Unix, see issue #8876).
  89. with open(self.source, 'w') as f:
  90. f.write('some content')
  91. st = os.stat(self.source)
  92. with patch("os.link", side_effect=OSError(0, "linking unsupported")):
  93. copy_file(self.source, self.target, link='hard')
  94. st2 = os.stat(self.source)
  95. st3 = os.stat(self.target)
  96. self.assertTrue(os.path.samestat(st, st2), (st, st2))
  97. self.assertFalse(os.path.samestat(st2, st3), (st2, st3))
  98. for fn in (self.source, self.target):
  99. with open(fn, 'r') as f:
  100. self.assertEqual(f.read(), 'some content')
  101. def test_suite():
  102. return unittest.makeSuite(FileUtilTestCase)
  103. if __name__ == "__main__":
  104. run_unittest(test_suite())