NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

test_file_util.py (2446B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\tests\test_file_util.py
      2 import unittest
      3 import os
      4 import shutil
      5 from distutils.file_util import move_file, write_file, copy_file
      6 from distutils import log
      7 from distutils.tests import support
      8 
      9 class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
     10 
     11     def _log(self, msg, *args):
     12         if len(args) > 0:
     13             self._logs.append(msg % args)
     14         else:
     15             self._logs.append(msg)
     16 
     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 
     27     def tearDown(self):
     28         log.info = self.old_log
     29         super(FileUtilTestCase, self).tearDown()
     30 
     31     def test_move_file_verbosity(self):
     32         f = open(self.source, 'w')
     33         try:
     34             f.write('some content')
     35         finally:
     36             f.close()
     37 
     38         move_file(self.source, self.target, verbose=0)
     39         wanted = []
     40         self.assertEqual(self._logs, wanted)
     41         move_file(self.target, self.source, verbose=0)
     42         move_file(self.source, self.target, verbose=1)
     43         wanted = ['moving %s -> %s' % (self.source, self.target)]
     44         self.assertEqual(self._logs, wanted)
     45         move_file(self.target, self.source, verbose=0)
     46         self._logs = []
     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 
     52     def test_write_file(self):
     53         lines = ['a', 'b', 'c']
     54         dir = self.mkdtemp()
     55         foo = os.path.join(dir, 'foo')
     56         write_file(foo, lines)
     57         content = [ line.strip() for line in open(foo).readlines() ]
     58         self.assertEqual(content, lines)
     59 
     60     def test_copy_file(self):
     61         src_dir = self.mkdtemp()
     62         foo = os.path.join(src_dir, 'foo')
     63         write_file(foo, 'content')
     64         dst_dir = self.mkdtemp()
     65         copy_file(foo, dst_dir)
     66         self.assertTrue(os.path.exists(os.path.join(dst_dir, 'foo')))
     67 
     68 
     69 def test_suite():
     70     return unittest.makeSuite(FileUtilTestCase)
     71 
     72 
     73 if __name__ == '__main__':
     74     unittest.main(defaultTest='test_suite')