1 """Tests for distutils.file_util."""
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
):
14 self
._logs
.append(msg
% args
)
16 self
._logs
.append(msg
)
19 super(FileUtilTestCase
, self
).setUp()
21 self
.old_log
= log
.info
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')
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')
37 move_file(self
.source
, self
.target
, verbose
=0)
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)
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']
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')))
75 return unittest
.makeSuite(FileUtilTestCase
)
77 if __name__
== "__main__":
78 unittest
.main(defaultTest
="test_suite")