2009-02-05 20:38:35 -04:00
|
|
|
"""Tests for distutils.dir_util."""
|
|
|
|
import unittest
|
|
|
|
import os
|
2010-09-17 13:35:37 -03:00
|
|
|
import stat
|
2010-09-17 23:55:03 -03:00
|
|
|
import sys
|
2014-08-31 18:31:32 -03:00
|
|
|
from unittest.mock import patch
|
2009-02-05 20:38:35 -04:00
|
|
|
|
2014-08-31 16:02:42 -03:00
|
|
|
from distutils import dir_util, errors
|
2009-05-17 08:14:15 -03:00
|
|
|
from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree,
|
|
|
|
ensure_relative)
|
2009-02-05 20:38:35 -04:00
|
|
|
|
|
|
|
from distutils import log
|
2009-02-14 10:35:51 -04:00
|
|
|
from distutils.tests import support
|
2010-11-05 23:44:43 -03:00
|
|
|
from test.support import run_unittest
|
2009-02-05 20:38:35 -04:00
|
|
|
|
2014-08-31 16:02:42 -03:00
|
|
|
|
2009-02-14 10:35:51 -04:00
|
|
|
class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
|
2009-02-05 20:38:35 -04:00
|
|
|
|
|
|
|
def _log(self, msg, *args):
|
|
|
|
if len(args) > 0:
|
|
|
|
self._logs.append(msg % args)
|
|
|
|
else:
|
|
|
|
self._logs.append(msg)
|
|
|
|
|
|
|
|
def setUp(self):
|
2009-05-29 06:14:04 -03:00
|
|
|
super(DirUtilTestCase, self).setUp()
|
2009-02-05 20:38:35 -04:00
|
|
|
self._logs = []
|
2009-02-14 10:35:51 -04:00
|
|
|
tmp_dir = self.mkdtemp()
|
|
|
|
self.root_target = os.path.join(tmp_dir, 'deep')
|
2009-02-05 20:38:35 -04:00
|
|
|
self.target = os.path.join(self.root_target, 'here')
|
2009-02-14 10:35:51 -04:00
|
|
|
self.target2 = os.path.join(tmp_dir, 'deep2')
|
2009-02-05 20:38:35 -04:00
|
|
|
self.old_log = log.info
|
|
|
|
log.info = self._log
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
log.info = self.old_log
|
2009-05-29 06:14:04 -03:00
|
|
|
super(DirUtilTestCase, self).tearDown()
|
2009-02-05 20:38:35 -04:00
|
|
|
|
|
|
|
def test_mkpath_remove_tree_verbosity(self):
|
|
|
|
|
|
|
|
mkpath(self.target, verbose=0)
|
|
|
|
wanted = []
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:38:35 -04:00
|
|
|
remove_tree(self.root_target, verbose=0)
|
|
|
|
|
|
|
|
mkpath(self.target, verbose=1)
|
|
|
|
wanted = ['creating %s' % self.root_target,
|
|
|
|
'creating %s' % self.target]
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:38:35 -04:00
|
|
|
self._logs = []
|
|
|
|
|
|
|
|
remove_tree(self.root_target, verbose=1)
|
|
|
|
wanted = ["removing '%s' (and everything under it)" % self.root_target]
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:38:35 -04:00
|
|
|
|
2010-09-17 23:55:03 -03:00
|
|
|
@unittest.skipIf(sys.platform.startswith('win'),
|
2014-08-31 16:00:47 -03:00
|
|
|
"This test is only appropriate for POSIX-like systems.")
|
2010-09-17 13:35:37 -03:00
|
|
|
def test_mkpath_with_custom_mode(self):
|
2010-09-19 00:09:54 -03:00
|
|
|
# Get and set the current umask value for testing mode bits.
|
|
|
|
umask = os.umask(0o002)
|
|
|
|
os.umask(umask)
|
2010-09-17 13:35:37 -03:00
|
|
|
mkpath(self.target, 0o700)
|
2010-09-19 00:09:54 -03:00
|
|
|
self.assertEqual(
|
|
|
|
stat.S_IMODE(os.stat(self.target).st_mode), 0o700 & ~umask)
|
2010-09-17 13:35:37 -03:00
|
|
|
mkpath(self.target2, 0o555)
|
2010-09-19 00:09:54 -03:00
|
|
|
self.assertEqual(
|
|
|
|
stat.S_IMODE(os.stat(self.target2).st_mode), 0o555 & ~umask)
|
2010-09-17 13:35:37 -03:00
|
|
|
|
2009-02-05 20:38:35 -04:00
|
|
|
def test_create_tree_verbosity(self):
|
|
|
|
|
|
|
|
create_tree(self.root_target, ['one', 'two', 'three'], verbose=0)
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(self._logs, [])
|
2009-02-05 20:38:35 -04:00
|
|
|
remove_tree(self.root_target, verbose=0)
|
|
|
|
|
|
|
|
wanted = ['creating %s' % self.root_target]
|
|
|
|
create_tree(self.root_target, ['one', 'two', 'three'], verbose=1)
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:38:35 -04:00
|
|
|
|
|
|
|
remove_tree(self.root_target, verbose=0)
|
|
|
|
|
|
|
|
def test_copy_tree_verbosity(self):
|
|
|
|
|
|
|
|
mkpath(self.target, verbose=0)
|
|
|
|
|
|
|
|
copy_tree(self.target, self.target2, verbose=0)
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(self._logs, [])
|
2009-02-05 20:38:35 -04:00
|
|
|
|
|
|
|
remove_tree(self.root_target, verbose=0)
|
|
|
|
|
|
|
|
mkpath(self.target, verbose=0)
|
|
|
|
a_file = os.path.join(self.target, 'ok.txt')
|
2012-12-08 15:21:51 -04:00
|
|
|
with open(a_file, 'w') as f:
|
2010-11-05 20:51:56 -03:00
|
|
|
f.write('some content')
|
2009-02-05 20:38:35 -04:00
|
|
|
|
|
|
|
wanted = ['copying %s -> %s' % (a_file, self.target2)]
|
|
|
|
copy_tree(self.target, self.target2, verbose=1)
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:38:35 -04:00
|
|
|
|
|
|
|
remove_tree(self.root_target, verbose=0)
|
|
|
|
remove_tree(self.target2, verbose=0)
|
|
|
|
|
2012-12-08 15:21:51 -04:00
|
|
|
def test_copy_tree_skips_nfs_temp_files(self):
|
|
|
|
mkpath(self.target, verbose=0)
|
|
|
|
|
|
|
|
a_file = os.path.join(self.target, 'ok.txt')
|
|
|
|
nfs_file = os.path.join(self.target, '.nfs123abc')
|
|
|
|
for f in a_file, nfs_file:
|
|
|
|
with open(f, 'w') as fh:
|
|
|
|
fh.write('some content')
|
|
|
|
|
|
|
|
copy_tree(self.target, self.target2)
|
|
|
|
self.assertEqual(os.listdir(self.target2), ['ok.txt'])
|
|
|
|
|
|
|
|
remove_tree(self.root_target, verbose=0)
|
|
|
|
remove_tree(self.target2, verbose=0)
|
|
|
|
|
2009-05-17 08:14:15 -03:00
|
|
|
def test_ensure_relative(self):
|
|
|
|
if os.sep == '/':
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(ensure_relative('/home/foo'), 'home/foo')
|
|
|
|
self.assertEqual(ensure_relative('some/path'), 'some/path')
|
2009-05-17 08:14:15 -03:00
|
|
|
else: # \\
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo')
|
|
|
|
self.assertEqual(ensure_relative('home\\foo'), 'home\\foo')
|
2009-05-17 08:14:15 -03:00
|
|
|
|
2014-09-20 12:53:12 -03:00
|
|
|
def test_copy_tree_exception_in_listdir(self):
|
2014-08-31 16:02:42 -03:00
|
|
|
"""
|
|
|
|
An exception in listdir should raise a DistutilsFileError
|
|
|
|
"""
|
2014-09-20 12:53:12 -03:00
|
|
|
with patch("os.listdir", side_effect=OSError()), \
|
|
|
|
self.assertRaises(errors.DistutilsFileError):
|
2014-08-31 18:51:22 -03:00
|
|
|
src = self.tempdirs[-1]
|
|
|
|
dir_util.copy_tree(src, None)
|
2014-08-31 16:02:42 -03:00
|
|
|
|
|
|
|
|
2009-02-05 20:38:35 -04:00
|
|
|
def test_suite():
|
|
|
|
return unittest.makeSuite(DirUtilTestCase)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-11-05 23:44:43 -03:00
|
|
|
run_unittest(test_suite())
|