Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_genericpath.py

    r2 r391  
     1"""
     2Tests common to genericpath, macpath, ntpath and posixpath
     3"""
     4
    15import unittest
    26from test import test_support
    37import os
    48import genericpath
    5 
    6 class AllCommonTest(unittest.TestCase):
    7 
    8     def assertIs(self, a, b):
    9         self.assert_(a is b)
     9import sys
     10
     11
     12def safe_rmdir(dirname):
     13    try:
     14        os.rmdir(dirname)
     15    except OSError:
     16        pass
     17
     18
     19class GenericTest(unittest.TestCase):
     20    # The path module to be tested
     21    pathmodule = genericpath
     22    common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
     23                         'getmtime', 'exists', 'isdir', 'isfile']
     24    attributes = []
     25
     26    def test_no_argument(self):
     27        for attr in self.common_attributes + self.attributes:
     28            with self.assertRaises(TypeError):
     29                getattr(self.pathmodule, attr)()
     30                raise self.fail("{}.{}() did not raise a TypeError"
     31                                .format(self.pathmodule.__name__, attr))
    1032
    1133    def test_commonprefix(self):
    12         self.assertEqual(
    13             genericpath.commonprefix([]),
     34        commonprefix = self.pathmodule.commonprefix
     35        self.assertEqual(
     36            commonprefix([]),
    1437            ""
    1538        )
    1639        self.assertEqual(
    17             genericpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
     40            commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
    1841            "/home/swen"
    1942        )
    2043        self.assertEqual(
    21             genericpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
     44            commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
    2245            "/home/swen/"
    2346        )
    2447        self.assertEqual(
    25             genericpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
     48            commonprefix(["/home/swen/spam", "/home/swen/spam"]),
    2649            "/home/swen/spam"
    2750        )
     51        self.assertEqual(
     52            commonprefix(["home:swenson:spam", "home:swen:spam"]),
     53            "home:swen"
     54        )
     55        self.assertEqual(
     56            commonprefix([":home:swen:spam", ":home:swen:eggs"]),
     57            ":home:swen:"
     58        )
     59        self.assertEqual(
     60            commonprefix([":home:swen:spam", ":home:swen:spam"]),
     61            ":home:swen:spam"
     62        )
     63
     64        testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
     65                    'aXc', 'abd', 'ab', 'aX', 'abcX']
     66        for s1 in testlist:
     67            for s2 in testlist:
     68                p = commonprefix([s1, s2])
     69                self.assertTrue(s1.startswith(p))
     70                self.assertTrue(s2.startswith(p))
     71                if s1 != s2:
     72                    n = len(p)
     73                    self.assertNotEqual(s1[n:n+1], s2[n:n+1])
    2874
    2975    def test_getsize(self):
     
    3278            f.write("foo")
    3379            f.close()
    34             self.assertEqual(genericpath.getsize(test_support.TESTFN), 3)
     80            self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3)
    3581        finally:
    3682            if not f.closed:
    3783                f.close()
    38             os.remove(test_support.TESTFN)
     84            test_support.unlink(test_support.TESTFN)
    3985
    4086    def test_time(self):
     
    5197            self.assertEqual(d, "foobar")
    5298
    53             self.assert_(
    54                 genericpath.getctime(test_support.TESTFN) <=
    55                 genericpath.getmtime(test_support.TESTFN)
     99            self.assertLessEqual(
     100                self.pathmodule.getctime(test_support.TESTFN),
     101                self.pathmodule.getmtime(test_support.TESTFN)
    56102            )
    57103        finally:
    58104            if not f.closed:
    59105                f.close()
    60             os.remove(test_support.TESTFN)
     106            test_support.unlink(test_support.TESTFN)
    61107
    62108    def test_exists(self):
    63         self.assertIs(genericpath.exists(test_support.TESTFN), False)
    64         f = open(test_support.TESTFN, "wb")
    65         try:
    66             f.write("foo")
    67             f.close()
    68             self.assertIs(genericpath.exists(test_support.TESTFN), True)
     109        self.assertIs(self.pathmodule.exists(test_support.TESTFN), False)
     110        f = open(test_support.TESTFN, "wb")
     111        try:
     112            f.write("foo")
     113            f.close()
     114            self.assertIs(self.pathmodule.exists(test_support.TESTFN), True)
     115            if not self.pathmodule == genericpath:
     116                self.assertIs(self.pathmodule.lexists(test_support.TESTFN),
     117                              True)
    69118        finally:
    70119            if not f.close():
    71120                f.close()
    72             try:
    73                 os.remove(test_support.TESTFN)
    74             except os.error:
    75                 pass
    76 
    77         self.assertRaises(TypeError, genericpath.exists)
     121            test_support.unlink(test_support.TESTFN)
    78122
    79123    def test_isdir(self):
    80         self.assertIs(genericpath.isdir(test_support.TESTFN), False)
    81         f = open(test_support.TESTFN, "wb")
    82         try:
    83             f.write("foo")
    84             f.close()
    85             self.assertIs(genericpath.isdir(test_support.TESTFN), False)
     124        self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
     125        f = open(test_support.TESTFN, "wb")
     126        try:
     127            f.write("foo")
     128            f.close()
     129            self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
    86130            os.remove(test_support.TESTFN)
    87131            os.mkdir(test_support.TESTFN)
    88             self.assertIs(genericpath.isdir(test_support.TESTFN), True)
     132            self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True)
    89133            os.rmdir(test_support.TESTFN)
    90134        finally:
    91135            if not f.close():
    92136                f.close()
    93             try:
    94                 os.remove(test_support.TESTFN)
    95             except os.error:
    96                 pass
    97             try:
    98                 os.rmdir(test_support.TESTFN)
    99             except os.error:
    100                 pass
    101 
    102         self.assertRaises(TypeError, genericpath.isdir)
     137            test_support.unlink(test_support.TESTFN)
     138            safe_rmdir(test_support.TESTFN)
    103139
    104140    def test_isfile(self):
    105         self.assertIs(genericpath.isfile(test_support.TESTFN), False)
    106         f = open(test_support.TESTFN, "wb")
    107         try:
    108             f.write("foo")
    109             f.close()
    110             self.assertIs(genericpath.isfile(test_support.TESTFN), True)
     141        self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
     142        f = open(test_support.TESTFN, "wb")
     143        try:
     144            f.write("foo")
     145            f.close()
     146            self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True)
    111147            os.remove(test_support.TESTFN)
    112148            os.mkdir(test_support.TESTFN)
    113             self.assertIs(genericpath.isfile(test_support.TESTFN), False)
     149            self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
    114150            os.rmdir(test_support.TESTFN)
    115151        finally:
    116152            if not f.close():
    117153                f.close()
    118             try:
    119                 os.remove(test_support.TESTFN)
    120             except os.error:
    121                 pass
    122             try:
    123                 os.rmdir(test_support.TESTFN)
    124             except os.error:
    125                 pass
    126 
    127         self.assertRaises(TypeError, genericpath.isdir)
    128 
    129         def test_samefile(self):
    130             f = open(test_support.TESTFN + "1", "wb")
    131             try:
    132                 f.write("foo")
    133                 f.close()
    134                 self.assertIs(
    135                     genericpath.samefile(
    136                         test_support.TESTFN + "1",
    137                         test_support.TESTFN + "1"
    138                     ),
    139                     True
    140                 )
    141                 # If we don't have links, assume that os.stat doesn't return resonable
    142                 # inode information and thus, that samefile() doesn't work
    143                 if hasattr(os, "symlink"):
    144                     os.symlink(
    145                         test_support.TESTFN + "1",
    146                         test_support.TESTFN + "2"
    147                     )
    148                     self.assertIs(
    149                         genericpath.samefile(
    150                             test_support.TESTFN + "1",
    151                             test_support.TESTFN + "2"
    152                         ),
    153                         True
    154                     )
    155                     os.remove(test_support.TESTFN + "2")
    156                     f = open(test_support.TESTFN + "2", "wb")
    157                     f.write("bar")
    158                     f.close()
    159                     self.assertIs(
    160                         genericpath.samefile(
    161                             test_support.TESTFN + "1",
    162                             test_support.TESTFN + "2"
    163                         ),
    164                         False
    165                     )
    166             finally:
    167                 if not f.close():
    168                     f.close()
    169                 try:
    170                     os.remove(test_support.TESTFN + "1")
    171                 except os.error:
    172                     pass
    173                 try:
    174                     os.remove(test_support.TESTFN + "2")
    175                 except os.error:
    176                     pass
    177 
    178             self.assertRaises(TypeError, genericpath.samefile)
     154            test_support.unlink(test_support.TESTFN)
     155            safe_rmdir(test_support.TESTFN)
     156
     157
     158# Following TestCase is not supposed to be run from test_genericpath.
     159# It is inherited by other test modules (macpath, ntpath, posixpath).
     160
     161class CommonTest(GenericTest):
     162    # The path module to be tested
     163    pathmodule = None
     164    common_attributes = GenericTest.common_attributes + [
     165        # Properties
     166        'curdir', 'pardir', 'extsep', 'sep',
     167        'pathsep', 'defpath', 'altsep', 'devnull',
     168        # Methods
     169        'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
     170        'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
     171        'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
     172    ]
     173
     174    def test_normcase(self):
     175        # Check that normcase() is idempotent
     176        p = "FoO/./BaR"
     177        p = self.pathmodule.normcase(p)
     178        self.assertEqual(p, self.pathmodule.normcase(p))
     179
     180    def test_splitdrive(self):
     181        # splitdrive for non-NT paths
     182        splitdrive = self.pathmodule.splitdrive
     183        self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
     184        self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
     185        self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
     186
     187    def test_expandvars(self):
     188        if self.pathmodule.__name__ == 'macpath':
     189            self.skipTest('macpath.expandvars is a stub')
     190        expandvars = self.pathmodule.expandvars
     191        with test_support.EnvironmentVarGuard() as env:
     192            env.clear()
     193            env["foo"] = "bar"
     194            env["{foo"] = "baz1"
     195            env["{foo}"] = "baz2"
     196            self.assertEqual(expandvars("foo"), "foo")
     197            self.assertEqual(expandvars("$foo bar"), "bar bar")
     198            self.assertEqual(expandvars("${foo}bar"), "barbar")
     199            self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
     200            self.assertEqual(expandvars("$bar bar"), "$bar bar")
     201            self.assertEqual(expandvars("$?bar"), "$?bar")
     202            self.assertEqual(expandvars("${foo}bar"), "barbar")
     203            self.assertEqual(expandvars("$foo}bar"), "bar}bar")
     204            self.assertEqual(expandvars("${foo"), "${foo")
     205            self.assertEqual(expandvars("${{foo}}"), "baz1}")
     206            self.assertEqual(expandvars("$foo$foo"), "barbar")
     207            self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
     208
     209    def test_abspath(self):
     210        self.assertIn("foo", self.pathmodule.abspath("foo"))
     211
     212        # Abspath returns bytes when the arg is bytes
     213        for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
     214            self.assertIsInstance(self.pathmodule.abspath(path), str)
     215
     216    def test_realpath(self):
     217        self.assertIn("foo", self.pathmodule.realpath("foo"))
     218
     219    def test_normpath_issue5827(self):
     220        # Make sure normpath preserves unicode
     221        for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
     222            self.assertIsInstance(self.pathmodule.normpath(path), unicode)
     223
     224    def test_abspath_issue3426(self):
     225        # Check that abspath returns unicode when the arg is unicode
     226        # with both ASCII and non-ASCII cwds.
     227        abspath = self.pathmodule.abspath
     228        for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
     229            self.assertIsInstance(abspath(path), unicode)
     230
     231        unicwd = u'\xe7w\xf0'
     232        try:
     233            fsencoding = test_support.TESTFN_ENCODING or "ascii"
     234            unicwd.encode(fsencoding)
     235        except (AttributeError, UnicodeEncodeError):
     236            # FS encoding is probably ASCII
     237            pass
     238        else:
     239            with test_support.temp_cwd(unicwd):
     240                for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
     241                    self.assertIsInstance(abspath(path), unicode)
     242
     243    @unittest.skipIf(sys.platform == 'darwin',
     244        "Mac OS X denies the creation of a directory with an invalid utf8 name")
     245    def test_nonascii_abspath(self):
     246        # Test non-ASCII, non-UTF8 bytes in the path.
     247        with test_support.temp_cwd('\xe7w\xf0'):
     248            self.test_abspath()
     249
    179250
    180251def test_main():
    181     test_support.run_unittest(AllCommonTest)
     252    test_support.run_unittest(GenericTest)
     253
    182254
    183255if __name__=="__main__":
Note: See TracChangeset for help on using the changeset viewer.