Home | History | Annotate | Download | only in tests
      1 """Tests for distutils.file_util."""
      2 import unittest
      3 import os
      4 import shutil
      5 
      6 from distutils.file_util import move_file, write_file, copy_file
      7 from distutils import log
      8 from distutils.tests import support
      9 from test.test_support import run_unittest
     10 
     11 class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
     12 
     13     def _log(self, msg, *args):
     14         if len(args) > 0:
     15             self._logs.append(msg % args)
     16         else:
     17             self._logs.append(msg)
     18 
     19     def setUp(self):
     20         super(FileUtilTestCase, self).setUp()
     21         self._logs = []
     22         self.old_log = log.info
     23         log.info = self._log
     24         tmp_dir = self.mkdtemp()
     25         self.source = os.path.join(tmp_dir, 'f1')
     26         self.target = os.path.join(tmp_dir, 'f2')
     27         self.target_dir = os.path.join(tmp_dir, 'd1')
     28 
     29     def tearDown(self):
     30         log.info = self.old_log
     31         super(FileUtilTestCase, self).tearDown()
     32 
     33     def test_move_file_verbosity(self):
     34         f = open(self.source, 'w')
     35         try:
     36             f.write('some content')
     37         finally:
     38             f.close()
     39 
     40         move_file(self.source, self.target, verbose=0)
     41         wanted = []
     42         self.assertEqual(self._logs, wanted)
     43 
     44         # back to original state
     45         move_file(self.target, self.source, verbose=0)
     46 
     47         move_file(self.source, self.target, verbose=1)
     48         wanted = ['moving %s -> %s' % (self.source, self.target)]
     49         self.assertEqual(self._logs, wanted)
     50 
     51         # back to original state
     52         move_file(self.target, self.source, verbose=0)
     53 
     54         self._logs = []
     55         # now the target is a dir
     56         os.mkdir(self.target_dir)
     57         move_file(self.source, self.target_dir, verbose=1)
     58         wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
     59         self.assertEqual(self._logs, wanted)
     60 
     61     def test_write_file(self):
     62         lines = ['a', 'b', 'c']
     63         dir = self.mkdtemp()
     64         foo = os.path.join(dir, 'foo')
     65         write_file(foo, lines)
     66         content = [line.strip() for line in open(foo).readlines()]
     67         self.assertEqual(content, lines)
     68 
     69     def test_copy_file(self):
     70         src_dir = self.mkdtemp()
     71         foo = os.path.join(src_dir, 'foo')
     72         write_file(foo, 'content')
     73         dst_dir = self.mkdtemp()
     74         copy_file(foo, dst_dir)
     75         self.assertTrue(os.path.exists(os.path.join(dst_dir, 'foo')))
     76 
     77 def test_suite():
     78     return unittest.makeSuite(FileUtilTestCase)
     79 
     80 if __name__ == "__main__":
     81     run_unittest(test_suite())
     82