2009-02-05 20:31:59 -04:00
|
|
|
"""Tests for distutils.file_util."""
|
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2009-07-03 16:01:12 -03:00
|
|
|
from distutils.file_util import move_file, write_file, copy_file
|
2009-02-05 20:31:59 -04:00
|
|
|
from distutils import log
|
2009-02-14 10:10:23 -04:00
|
|
|
from distutils.tests import support
|
Merged revisions 86236,86240,86332,86340,87271,87273,87447 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
To comply with the 2.x doc style, the methods in trace.rst use brackets around
optional arguments. The rest is a mostly straight merge, modulo support changed
to test_support and use of the old super call style in test_tuple.
........
r86236 | eric.araujo | 2010-11-06 03:44:43 +0100 (sam., 06 nov. 2010) | 2 lines
Make sure each test can be run standalone (./python Lib/distutils/tests/x.py)
........
r86240 | eric.araujo | 2010-11-06 05:11:59 +0100 (sam., 06 nov. 2010) | 2 lines
Prevent ResourceWarnings in test_gettext
........
r86332 | eric.araujo | 2010-11-08 19:15:17 +0100 (lun., 08 nov. 2010) | 4 lines
Add missing NEWS entry for a fix committed by Senthil.
All recent modifications to distutils should now be covered in NEWS.
........
r86340 | eric.araujo | 2010-11-08 22:48:23 +0100 (lun., 08 nov. 2010) | 2 lines
This was actually fixed for the previous alpha.
........
r87271 | eric.araujo | 2010-12-15 20:09:58 +0100 (mer., 15 déc. 2010) | 2 lines
Improve trace documentation (#9264). Patch by Eli Bendersky.
........
r87273 | eric.araujo | 2010-12-15 20:30:15 +0100 (mer., 15 déc. 2010) | 2 lines
Use nested method directives, rewrap long lines, fix whitespace.
........
r87447 | eric.araujo | 2010-12-23 20:13:05 +0100 (jeu., 23 déc. 2010) | 2 lines
Fix typo in superclass method name
........
2011-02-02 20:12:18 -04:00
|
|
|
from test.test_support import run_unittest
|
2009-02-05 20:31:59 -04:00
|
|
|
|
2009-02-14 10:10:23 -04:00
|
|
|
class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
|
2009-02-05 20:31:59 -04:00
|
|
|
|
|
|
|
def _log(self, msg, *args):
|
|
|
|
if len(args) > 0:
|
|
|
|
self._logs.append(msg % args)
|
|
|
|
else:
|
|
|
|
self._logs.append(msg)
|
|
|
|
|
|
|
|
def setUp(self):
|
2009-05-29 05:08:07 -03:00
|
|
|
super(FileUtilTestCase, self).setUp()
|
2009-02-05 20:31:59 -04:00
|
|
|
self._logs = []
|
|
|
|
self.old_log = log.info
|
|
|
|
log.info = self._log
|
2009-02-14 10:10:23 -04:00
|
|
|
tmp_dir = self.mkdtemp()
|
|
|
|
self.source = os.path.join(tmp_dir, 'f1')
|
|
|
|
self.target = os.path.join(tmp_dir, 'f2')
|
|
|
|
self.target_dir = os.path.join(tmp_dir, 'd1')
|
2009-02-05 20:31:59 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
log.info = self.old_log
|
2009-05-29 05:08:07 -03:00
|
|
|
super(FileUtilTestCase, self).tearDown()
|
2009-02-05 20:31:59 -04:00
|
|
|
|
|
|
|
def test_move_file_verbosity(self):
|
|
|
|
f = open(self.source, 'w')
|
2010-11-06 01:06:18 -03:00
|
|
|
try:
|
|
|
|
f.write('some content')
|
|
|
|
finally:
|
|
|
|
f.close()
|
2009-02-05 20:31:59 -04:00
|
|
|
|
|
|
|
move_file(self.source, self.target, verbose=0)
|
|
|
|
wanted = []
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:31:59 -04:00
|
|
|
|
|
|
|
# back to original state
|
|
|
|
move_file(self.target, self.source, verbose=0)
|
|
|
|
|
|
|
|
move_file(self.source, self.target, verbose=1)
|
|
|
|
wanted = ['moving %s -> %s' % (self.source, self.target)]
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:31:59 -04:00
|
|
|
|
|
|
|
# back to original state
|
|
|
|
move_file(self.target, self.source, verbose=0)
|
|
|
|
|
|
|
|
self._logs = []
|
|
|
|
# now the target is a dir
|
|
|
|
os.mkdir(self.target_dir)
|
|
|
|
move_file(self.source, self.target_dir, verbose=1)
|
|
|
|
wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self._logs, wanted)
|
2009-02-05 20:31:59 -04:00
|
|
|
|
2009-07-03 16:01:12 -03:00
|
|
|
def test_write_file(self):
|
|
|
|
lines = ['a', 'b', 'c']
|
|
|
|
dir = self.mkdtemp()
|
|
|
|
foo = os.path.join(dir, 'foo')
|
|
|
|
write_file(foo, lines)
|
|
|
|
content = [line.strip() for line in open(foo).readlines()]
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(content, lines)
|
2009-07-03 16:01:12 -03:00
|
|
|
|
|
|
|
def test_copy_file(self):
|
|
|
|
src_dir = self.mkdtemp()
|
|
|
|
foo = os.path.join(src_dir, 'foo')
|
|
|
|
write_file(foo, 'content')
|
|
|
|
dst_dir = self.mkdtemp()
|
|
|
|
copy_file(foo, dst_dir)
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(dst_dir, 'foo')))
|
2009-02-05 20:31:59 -04:00
|
|
|
|
|
|
|
def test_suite():
|
|
|
|
return unittest.makeSuite(FileUtilTestCase)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
Merged revisions 86236,86240,86332,86340,87271,87273,87447 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
To comply with the 2.x doc style, the methods in trace.rst use brackets around
optional arguments. The rest is a mostly straight merge, modulo support changed
to test_support and use of the old super call style in test_tuple.
........
r86236 | eric.araujo | 2010-11-06 03:44:43 +0100 (sam., 06 nov. 2010) | 2 lines
Make sure each test can be run standalone (./python Lib/distutils/tests/x.py)
........
r86240 | eric.araujo | 2010-11-06 05:11:59 +0100 (sam., 06 nov. 2010) | 2 lines
Prevent ResourceWarnings in test_gettext
........
r86332 | eric.araujo | 2010-11-08 19:15:17 +0100 (lun., 08 nov. 2010) | 4 lines
Add missing NEWS entry for a fix committed by Senthil.
All recent modifications to distutils should now be covered in NEWS.
........
r86340 | eric.araujo | 2010-11-08 22:48:23 +0100 (lun., 08 nov. 2010) | 2 lines
This was actually fixed for the previous alpha.
........
r87271 | eric.araujo | 2010-12-15 20:09:58 +0100 (mer., 15 déc. 2010) | 2 lines
Improve trace documentation (#9264). Patch by Eli Bendersky.
........
r87273 | eric.araujo | 2010-12-15 20:30:15 +0100 (mer., 15 déc. 2010) | 2 lines
Use nested method directives, rewrap long lines, fix whitespace.
........
r87447 | eric.araujo | 2010-12-23 20:13:05 +0100 (jeu., 23 déc. 2010) | 2 lines
Fix typo in superclass method name
........
2011-02-02 20:12:18 -04:00
|
|
|
run_unittest(test_suite())
|