Issue #7575: An overflow test for math.expm1 was failing on OS X 10.4/Intel,
[python.git] / Lib / distutils / tests / test_file_util.py
blob99b421f73f20c2acccb8c850cea87c96c0f52358
1 """Tests for distutils.file_util."""
2 import unittest
3 import os
4 import shutil
6 from distutils.file_util import move_file, write_file, copy_file
7 from distutils import log
8 from distutils.tests import support
10 class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
12 def _log(self, msg, *args):
13 if len(args) > 0:
14 self._logs.append(msg % args)
15 else:
16 self._logs.append(msg)
18 def setUp(self):
19 super(FileUtilTestCase, self).setUp()
20 self._logs = []
21 self.old_log = log.info
22 log.info = self._log
23 tmp_dir = self.mkdtemp()
24 self.source = os.path.join(tmp_dir, 'f1')
25 self.target = os.path.join(tmp_dir, 'f2')
26 self.target_dir = os.path.join(tmp_dir, 'd1')
28 def tearDown(self):
29 log.info = self.old_log
30 super(FileUtilTestCase, self).tearDown()
32 def test_move_file_verbosity(self):
33 f = open(self.source, 'w')
34 f.write('some content')
35 f.close()
37 move_file(self.source, self.target, verbose=0)
38 wanted = []
39 self.assertEquals(self._logs, wanted)
41 # back to original state
42 move_file(self.target, self.source, verbose=0)
44 move_file(self.source, self.target, verbose=1)
45 wanted = ['moving %s -> %s' % (self.source, self.target)]
46 self.assertEquals(self._logs, wanted)
48 # back to original state
49 move_file(self.target, self.source, verbose=0)
51 self._logs = []
52 # now the target is a dir
53 os.mkdir(self.target_dir)
54 move_file(self.source, self.target_dir, verbose=1)
55 wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
56 self.assertEquals(self._logs, wanted)
58 def test_write_file(self):
59 lines = ['a', 'b', 'c']
60 dir = self.mkdtemp()
61 foo = os.path.join(dir, 'foo')
62 write_file(foo, lines)
63 content = [line.strip() for line in open(foo).readlines()]
64 self.assertEquals(content, lines)
66 def test_copy_file(self):
67 src_dir = self.mkdtemp()
68 foo = os.path.join(src_dir, 'foo')
69 write_file(foo, 'content')
70 dst_dir = self.mkdtemp()
71 copy_file(foo, dst_dir)
72 self.assertTrue(os.path.exists(os.path.join(dst_dir, 'foo')))
74 def test_suite():
75 return unittest.makeSuite(FileUtilTestCase)
77 if __name__ == "__main__":
78 unittest.main(defaultTest="test_suite")