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