[Python-checkins] r51187 - python/trunk/Lib/test/test_shutil.py

tim.peters python-checkins at python.org
Thu Aug 10 05:01:31 CEST 2006


Author: tim.peters
Date: Thu Aug 10 05:01:26 2006
New Revision: 51187

Modified:
   python/trunk/Lib/test/test_shutil.py
Log:
test_copytree_simple():  This was leaving behind two new temp
directories each time it ran, at least on Windows.

Several changes:  explicitly closed all files; wrapped long
lines; stopped suppressing errors when removing a file or
directory fails (removing /shouldn't/ fail!); and changed
what appeared to be incorrect usage of os.removedirs() (that
doesn't remove empty directories at and /under/ the given
path, instead it must be given an empty leaf directory and
then deletes empty directories moving /up/ the path -- could
be that the conceptually simpler shutil.rmtree() was really
actually intended here).


Modified: python/trunk/Lib/test/test_shutil.py
==============================================================================
--- python/trunk/Lib/test/test_shutil.py	(original)
+++ python/trunk/Lib/test/test_shutil.py	Thu Aug 10 05:01:26 2006
@@ -74,31 +74,51 @@
             except:
                 pass
 
-
     def test_copytree_simple(self):
+        def write_data(path, data):
+            f = open(path, "w")
+            f.write(data)
+            f.close()
+
+        def read_data(path):
+            f = open(path)
+            data = f.read()
+            f.close()
+            return data
+
         src_dir = tempfile.mkdtemp()
         dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
-        open(os.path.join(src_dir, 'test.txt'), 'w').write('123')
+
+        write_data(os.path.join(src_dir, 'test.txt'), '123')
+
         os.mkdir(os.path.join(src_dir, 'test_dir'))
-        open(os.path.join(src_dir, 'test_dir', 'test.txt'), 'w').write('456')
-        #
+        write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
+
         try:
             shutil.copytree(src_dir, dst_dir)
             self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
             self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
-            self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir', 'test.txt')))
-            self.assertEqual(open(os.path.join(dst_dir, 'test.txt')).read(), '123')
-            self.assertEqual(open(os.path.join(dst_dir, 'test_dir', 'test.txt')).read(), '456')
+            self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
+                                                        'test.txt')))
+            actual = read_data(os.path.join(dst_dir, 'test.txt'))
+            self.assertEqual(actual, '123')
+            actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
+            self.assertEqual(actual, '456')
         finally:
-            try:
-                os.remove(os.path.join(src_dir, 'test.txt'))
-                os.remove(os.path.join(dst_dir, 'test.txt'))
-                os.remove(os.path.join(src_dir, 'test_dir', 'test.txt'))
-                os.remove(os.path.join(dst_dir, 'test_dir', 'test.txt'))
-                os.removedirs(src_dir)
-                os.removedirs(dst_dir)
-            except:
-                pass
+            for path in (
+                    os.path.join(src_dir, 'test.txt'),
+                    os.path.join(dst_dir, 'test.txt'),
+                    os.path.join(src_dir, 'test_dir', 'test.txt'),
+                    os.path.join(dst_dir, 'test_dir', 'test.txt'),
+                ):
+                if os.path.exists(path):
+                    os.remove(path)
+            for path in (
+                    os.path.join(src_dir, 'test_dir'),
+                    os.path.join(dst_dir, 'test_dir'),
+                ):
+                if os.path.exists(path):
+                    os.removedirs(path)
 
 
     if hasattr(os, "symlink"):


More information about the Python-checkins mailing list