mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 19:34:08 +00:00 
			
		
		
		
	svn+ssh://pythondev@svn.python.org/python/trunk ........ r60131 | georg.brandl | 2008-01-20 12:13:29 +0100 (Sun, 20 Jan 2008) | 3 lines #1351692: in pprint, always call format() for dict and list items to enable custom formatting of contents via subclassing PrettyPrinter. ........ r60133 | georg.brandl | 2008-01-20 12:43:03 +0100 (Sun, 20 Jan 2008) | 2 lines #1178141: add addinfourl.code to get http status code from urllib. ........ r60134 | georg.brandl | 2008-01-20 13:05:43 +0100 (Sun, 20 Jan 2008) | 4 lines #856047: respect the ``no_proxy`` env var when checking for proxies in urllib and using the other ``_proxy`` env vars. Original patch by Donovan Baarda. ........ r60135 | georg.brandl | 2008-01-20 13:18:17 +0100 (Sun, 20 Jan 2008) | 4 lines #1664522: in urllib, don't read non-existing directories in ftp mode, returning a 0-byte file -- raise an IOError instead. Original patch from Phil Knirsch. ........ r60136 | georg.brandl | 2008-01-20 13:57:47 +0100 (Sun, 20 Jan 2008) | 2 lines #799369: document possible sys.platform values. ........ r60137 | georg.brandl | 2008-01-20 14:08:37 +0100 (Sun, 20 Jan 2008) | 2 lines #652749: document the constants added to the builtins by site.py. ........ r60138 | georg.brandl | 2008-01-20 14:59:46 +0100 (Sun, 20 Jan 2008) | 2 lines #1648: add sys.gettrace() and sys.getprofile(). ........ r60139 | georg.brandl | 2008-01-20 15:17:42 +0100 (Sun, 20 Jan 2008) | 2 lines #1669: don't allow shutil.rmtree() to be called on a symlink. ........ r60140 | georg.brandl | 2008-01-20 15:20:02 +0100 (Sun, 20 Jan 2008) | 2 lines Fix test_pyclbr after urllib change. ........ r60141 | christian.heimes | 2008-01-20 15:28:28 +0100 (Sun, 20 Jan 2008) | 1 line Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :) ........ r60142 | georg.brandl | 2008-01-20 15:31:27 +0100 (Sun, 20 Jan 2008) | 2 lines #1876: fix typos in test_operator. ........
		
			
				
	
	
		
			170 lines
		
	
	
	
		
			5.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			170 lines
		
	
	
	
		
			5.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# Copyright (C) 2003 Python Software Foundation
 | 
						|
 | 
						|
import unittest
 | 
						|
import shutil
 | 
						|
import tempfile
 | 
						|
import sys
 | 
						|
import stat
 | 
						|
import os
 | 
						|
import os.path
 | 
						|
from test import test_support
 | 
						|
from test.test_support import TESTFN
 | 
						|
 | 
						|
class TestShutil(unittest.TestCase):
 | 
						|
    def test_rmtree_errors(self):
 | 
						|
        # filename is guaranteed not to exist
 | 
						|
        filename = tempfile.mktemp()
 | 
						|
        self.assertRaises(OSError, shutil.rmtree, filename)
 | 
						|
 | 
						|
    # See bug #1071513 for why we don't run this on cygwin
 | 
						|
    # and bug #1076467 for why we don't run this as root.
 | 
						|
    if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
 | 
						|
        and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
 | 
						|
        def test_on_error(self):
 | 
						|
            self.errorState = 0
 | 
						|
            os.mkdir(TESTFN)
 | 
						|
            self.childpath = os.path.join(TESTFN, 'a')
 | 
						|
            f = open(self.childpath, 'w')
 | 
						|
            f.close()
 | 
						|
            old_dir_mode = os.stat(TESTFN).st_mode
 | 
						|
            old_child_mode = os.stat(self.childpath).st_mode
 | 
						|
            # Make unwritable.
 | 
						|
            os.chmod(self.childpath, stat.S_IREAD)
 | 
						|
            os.chmod(TESTFN, stat.S_IREAD)
 | 
						|
 | 
						|
            shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
 | 
						|
            # Test whether onerror has actually been called.
 | 
						|
            self.assertEqual(self.errorState, 2,
 | 
						|
                             "Expected call to onerror function did not happen.")
 | 
						|
 | 
						|
            # Make writable again.
 | 
						|
            os.chmod(TESTFN, old_dir_mode)
 | 
						|
            os.chmod(self.childpath, old_child_mode)
 | 
						|
 | 
						|
            # Clean up.
 | 
						|
            shutil.rmtree(TESTFN)
 | 
						|
 | 
						|
    def check_args_to_onerror(self, func, arg, exc):
 | 
						|
        if self.errorState == 0:
 | 
						|
            self.assertEqual(func, os.remove)
 | 
						|
            self.assertEqual(arg, self.childpath)
 | 
						|
            self.failUnless(issubclass(exc[0], OSError))
 | 
						|
            self.errorState = 1
 | 
						|
        else:
 | 
						|
            self.assertEqual(func, os.rmdir)
 | 
						|
            self.assertEqual(arg, TESTFN)
 | 
						|
            self.failUnless(issubclass(exc[0], OSError))
 | 
						|
            self.errorState = 2
 | 
						|
 | 
						|
    def test_rmtree_dont_delete_file(self):
 | 
						|
        # When called on a file instead of a directory, don't delete it.
 | 
						|
        handle, path = tempfile.mkstemp()
 | 
						|
        os.fdopen(handle).close()
 | 
						|
        self.assertRaises(OSError, shutil.rmtree, path)
 | 
						|
        os.remove(path)
 | 
						|
 | 
						|
    def test_dont_move_dir_in_itself(self):
 | 
						|
        src_dir = tempfile.mkdtemp()
 | 
						|
        try:
 | 
						|
            dst = os.path.join(src_dir, 'foo')
 | 
						|
            self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
 | 
						|
        finally:
 | 
						|
            try:
 | 
						|
                os.rmdir(src_dir)
 | 
						|
            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')
 | 
						|
 | 
						|
        write_data(os.path.join(src_dir, 'test.txt'), '123')
 | 
						|
 | 
						|
        os.mkdir(os.path.join(src_dir, 'test_dir'))
 | 
						|
        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')))
 | 
						|
            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:
 | 
						|
            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 (src_dir,
 | 
						|
                    os.path.abspath(os.path.join(dst_dir, os.path.pardir))
 | 
						|
                ):
 | 
						|
                if os.path.exists(path):
 | 
						|
                    shutil.rmtree(path)
 | 
						|
 | 
						|
 | 
						|
    if hasattr(os, "symlink"):
 | 
						|
        def test_dont_copy_file_onto_link_to_itself(self):
 | 
						|
            # bug 851123.
 | 
						|
            os.mkdir(TESTFN)
 | 
						|
            src = os.path.join(TESTFN, 'cheese')
 | 
						|
            dst = os.path.join(TESTFN, 'shop')
 | 
						|
            try:
 | 
						|
                f = open(src, 'w')
 | 
						|
                f.write('cheddar')
 | 
						|
                f.close()
 | 
						|
 | 
						|
                os.link(src, dst)
 | 
						|
                self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
 | 
						|
                self.assertEqual(open(src,'r').read(), 'cheddar')
 | 
						|
                os.remove(dst)
 | 
						|
 | 
						|
                # Using `src` here would mean we end up with a symlink pointing
 | 
						|
                # to TESTFN/TESTFN/cheese, while it should point at
 | 
						|
                # TESTFN/cheese.
 | 
						|
                os.symlink('cheese', dst)
 | 
						|
                self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
 | 
						|
                self.assertEqual(open(src,'r').read(), 'cheddar')
 | 
						|
                os.remove(dst)
 | 
						|
            finally:
 | 
						|
                try:
 | 
						|
                    shutil.rmtree(TESTFN)
 | 
						|
                except OSError:
 | 
						|
                    pass
 | 
						|
 | 
						|
        def test_rmtree_on_symlink(self):
 | 
						|
            # bug 1669.
 | 
						|
            os.mkdir(TESTFN)
 | 
						|
            try:
 | 
						|
                src = os.path.join(TESTFN, 'cheese')
 | 
						|
                dst = os.path.join(TESTFN, 'shop')
 | 
						|
                os.mkdir(src)
 | 
						|
                os.symlink(src, dst)
 | 
						|
                self.assertRaises(OSError, shutil.rmtree, dst)
 | 
						|
            finally:
 | 
						|
                shutil.rmtree(TESTFN, ignore_errors=True)
 | 
						|
 | 
						|
 | 
						|
 | 
						|
def test_main():
 | 
						|
    test_support.run_unittest(TestShutil)
 | 
						|
 | 
						|
if __name__ == '__main__':
 | 
						|
    test_main()
 |