test_dir_util.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """Tests for distutils.dir_util."""
  2. import unittest
  3. import os
  4. import stat
  5. import sys
  6. from unittest.mock import patch
  7. from distutils import dir_util, errors
  8. from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree,
  9. ensure_relative)
  10. from distutils import log
  11. from distutils.tests import support
  12. from test.support import run_unittest
  13. class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
  14. def _log(self, msg, *args):
  15. if len(args) > 0:
  16. self._logs.append(msg % args)
  17. else:
  18. self._logs.append(msg)
  19. def setUp(self):
  20. super(DirUtilTestCase, self).setUp()
  21. self._logs = []
  22. tmp_dir = self.mkdtemp()
  23. self.root_target = os.path.join(tmp_dir, 'deep')
  24. self.target = os.path.join(self.root_target, 'here')
  25. self.target2 = os.path.join(tmp_dir, 'deep2')
  26. self.old_log = log.info
  27. log.info = self._log
  28. def tearDown(self):
  29. log.info = self.old_log
  30. super(DirUtilTestCase, self).tearDown()
  31. def test_mkpath_remove_tree_verbosity(self):
  32. mkpath(self.target, verbose=0)
  33. wanted = []
  34. self.assertEqual(self._logs, wanted)
  35. remove_tree(self.root_target, verbose=0)
  36. mkpath(self.target, verbose=1)
  37. wanted = ['creating %s' % self.root_target,
  38. 'creating %s' % self.target]
  39. self.assertEqual(self._logs, wanted)
  40. self._logs = []
  41. remove_tree(self.root_target, verbose=1)
  42. wanted = ["removing '%s' (and everything under it)" % self.root_target]
  43. self.assertEqual(self._logs, wanted)
  44. @unittest.skipIf(sys.platform.startswith('win'),
  45. "This test is only appropriate for POSIX-like systems.")
  46. def test_mkpath_with_custom_mode(self):
  47. # Get and set the current umask value for testing mode bits.
  48. umask = os.umask(0o002)
  49. os.umask(umask)
  50. mkpath(self.target, 0o700)
  51. self.assertEqual(
  52. stat.S_IMODE(os.stat(self.target).st_mode), 0o700 & ~umask)
  53. mkpath(self.target2, 0o555)
  54. self.assertEqual(
  55. stat.S_IMODE(os.stat(self.target2).st_mode), 0o555 & ~umask)
  56. def test_create_tree_verbosity(self):
  57. create_tree(self.root_target, ['one', 'two', 'three'], verbose=0)
  58. self.assertEqual(self._logs, [])
  59. remove_tree(self.root_target, verbose=0)
  60. wanted = ['creating %s' % self.root_target]
  61. create_tree(self.root_target, ['one', 'two', 'three'], verbose=1)
  62. self.assertEqual(self._logs, wanted)
  63. remove_tree(self.root_target, verbose=0)
  64. def test_copy_tree_verbosity(self):
  65. mkpath(self.target, verbose=0)
  66. copy_tree(self.target, self.target2, verbose=0)
  67. self.assertEqual(self._logs, [])
  68. remove_tree(self.root_target, verbose=0)
  69. mkpath(self.target, verbose=0)
  70. a_file = os.path.join(self.target, 'ok.txt')
  71. with open(a_file, 'w') as f:
  72. f.write('some content')
  73. wanted = ['copying %s -> %s' % (a_file, self.target2)]
  74. copy_tree(self.target, self.target2, verbose=1)
  75. self.assertEqual(self._logs, wanted)
  76. remove_tree(self.root_target, verbose=0)
  77. remove_tree(self.target2, verbose=0)
  78. def test_copy_tree_skips_nfs_temp_files(self):
  79. mkpath(self.target, verbose=0)
  80. a_file = os.path.join(self.target, 'ok.txt')
  81. nfs_file = os.path.join(self.target, '.nfs123abc')
  82. for f in a_file, nfs_file:
  83. with open(f, 'w') as fh:
  84. fh.write('some content')
  85. copy_tree(self.target, self.target2)
  86. self.assertEqual(os.listdir(self.target2), ['ok.txt'])
  87. remove_tree(self.root_target, verbose=0)
  88. remove_tree(self.target2, verbose=0)
  89. def test_ensure_relative(self):
  90. if os.sep == '/':
  91. self.assertEqual(ensure_relative('/home/foo'), 'home/foo')
  92. self.assertEqual(ensure_relative('some/path'), 'some/path')
  93. else: # \\
  94. self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo')
  95. self.assertEqual(ensure_relative('home\\foo'), 'home\\foo')
  96. def test_copy_tree_exception_in_listdir(self):
  97. """
  98. An exception in listdir should raise a DistutilsFileError
  99. """
  100. with patch("os.listdir", side_effect=OSError()), \
  101. self.assertRaises(errors.DistutilsFileError):
  102. src = self.tempdirs[-1]
  103. dir_util.copy_tree(src, None)
  104. def test_suite():
  105. return unittest.makeSuite(DirUtilTestCase)
  106. if __name__ == "__main__":
  107. run_unittest(test_suite())