github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/test/test_genericpath.py (about)

     1  """
     2  Tests common to genericpath, macpath, ntpath and posixpath
     3  """
     4  
     5  import unittest
     6  from test import test_support
     7  import os
     8  import genericpath
     9  import sys
    10  
    11  
    12  def safe_rmdir(dirname):
    13      try:
    14          os.rmdir(dirname)
    15      except OSError:
    16          pass
    17  
    18  
    19  class 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))
    32  
    33      def test_commonprefix(self):
    34          commonprefix = self.pathmodule.commonprefix
    35          self.assertEqual(
    36              commonprefix([]),
    37              ""
    38          )
    39          self.assertEqual(
    40              commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
    41              "/home/swen"
    42          )
    43          self.assertEqual(
    44              commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
    45              "/home/swen/"
    46          )
    47          self.assertEqual(
    48              commonprefix(["/home/swen/spam", "/home/swen/spam"]),
    49              "/home/swen/spam"
    50          )
    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])
    74  
    75      def test_getsize(self):
    76          f = open(test_support.TESTFN, "wb")
    77          try:
    78              f.write("foo")
    79              f.close()
    80              self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3)
    81          finally:
    82              if not f.closed:
    83                  f.close()
    84              test_support.unlink(test_support.TESTFN)
    85  
    86      @unittest.skip('grumpy')
    87      def test_time(self):
    88          f = open(test_support.TESTFN, "wb")
    89          try:
    90              f.write("foo")
    91              f.close()
    92              f = open(test_support.TESTFN, "ab")
    93              f.write("bar")
    94              f.close()
    95              f = open(test_support.TESTFN, "rb")
    96              d = f.read()
    97              f.close()
    98              self.assertEqual(d, "foobar")
    99  
   100              self.assertLessEqual(
   101                  self.pathmodule.getctime(test_support.TESTFN),
   102                  self.pathmodule.getmtime(test_support.TESTFN)
   103              )
   104          finally:
   105              if not f.closed:
   106                  f.close()
   107              test_support.unlink(test_support.TESTFN)
   108  
   109      def test_exists(self):
   110          self.assertIs(self.pathmodule.exists(test_support.TESTFN), False)
   111          f = open(test_support.TESTFN, "wb")
   112          try:
   113              f.write("foo")
   114              f.close()
   115              self.assertIs(self.pathmodule.exists(test_support.TESTFN), True)
   116              if not self.pathmodule == genericpath:
   117                  self.assertIs(self.pathmodule.lexists(test_support.TESTFN),
   118                                True)
   119          finally:
   120              if not f.close():
   121                  f.close()
   122              test_support.unlink(test_support.TESTFN)
   123  
   124      def test_isdir(self):
   125          self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
   126          f = open(test_support.TESTFN, "wb")
   127          try:
   128              f.write("foo")
   129              f.close()
   130              self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
   131              os.remove(test_support.TESTFN)
   132              os.mkdir(test_support.TESTFN)
   133              self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True)
   134              os.rmdir(test_support.TESTFN)
   135          finally:
   136              if not f.close():
   137                  f.close()
   138              test_support.unlink(test_support.TESTFN)
   139              safe_rmdir(test_support.TESTFN)
   140  
   141      @unittest.skip('grumpy')
   142      def test_isfile(self):
   143          self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
   144          f = open(test_support.TESTFN, "wb")
   145          try:
   146              f.write("foo")
   147              f.close()
   148              self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True)
   149              os.remove(test_support.TESTFN)
   150              os.mkdir(test_support.TESTFN)
   151              self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
   152              os.rmdir(test_support.TESTFN)
   153          finally:
   154              if not f.close():
   155                  f.close()
   156              test_support.unlink(test_support.TESTFN)
   157              safe_rmdir(test_support.TESTFN)
   158  
   159  
   160  # Following TestCase is not supposed to be run from test_genericpath.
   161  # It is inherited by other test modules (macpath, ntpath, posixpath).
   162  
   163  class CommonTest(GenericTest):
   164      # The path module to be tested
   165      pathmodule = None
   166      common_attributes = GenericTest.common_attributes + [
   167          # Properties
   168          'curdir', 'pardir', 'extsep', 'sep',
   169          'pathsep', 'defpath', 'altsep', 'devnull',
   170          # Methods
   171          'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
   172          'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
   173          'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
   174      ]
   175  
   176      def test_normcase(self):
   177          # Check that normcase() is idempotent
   178          p = "FoO/./BaR"
   179          p = self.pathmodule.normcase(p)
   180          self.assertEqual(p, self.pathmodule.normcase(p))
   181  
   182      def test_splitdrive(self):
   183          # splitdrive for non-NT paths
   184          splitdrive = self.pathmodule.splitdrive
   185          self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
   186          self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
   187          self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
   188  
   189      def test_expandvars(self):
   190          if self.pathmodule.__name__ == 'macpath':
   191              self.skipTest('macpath.expandvars is a stub')
   192          expandvars = self.pathmodule.expandvars
   193          with test_support.EnvironmentVarGuard() as env:
   194              env.clear()
   195              env["foo"] = "bar"
   196              env["{foo"] = "baz1"
   197              env["{foo}"] = "baz2"
   198              self.assertEqual(expandvars("foo"), "foo")
   199              self.assertEqual(expandvars("$foo bar"), "bar bar")
   200              self.assertEqual(expandvars("${foo}bar"), "barbar")
   201              self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
   202              self.assertEqual(expandvars("$bar bar"), "$bar bar")
   203              self.assertEqual(expandvars("$?bar"), "$?bar")
   204              self.assertEqual(expandvars("$foo}bar"), "bar}bar")
   205              self.assertEqual(expandvars("${foo"), "${foo")
   206              self.assertEqual(expandvars("${{foo}}"), "baz1}")
   207              self.assertEqual(expandvars("$foo$foo"), "barbar")
   208              self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
   209  
   210      @unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII')
   211      def test_expandvars_nonascii(self):
   212          if self.pathmodule.__name__ == 'macpath':
   213              self.skipTest('macpath.expandvars is a stub')
   214          expandvars = self.pathmodule.expandvars
   215          def check(value, expected):
   216              self.assertEqual(expandvars(value), expected)
   217          encoding = sys.getfilesystemencoding()
   218          with test_support.EnvironmentVarGuard() as env:
   219              env.clear()
   220              unonascii = test_support.FS_NONASCII
   221              snonascii = unonascii.encode(encoding)
   222              env['spam'] = snonascii
   223              env[snonascii] = 'ham' + snonascii
   224              check(snonascii, snonascii)
   225              check('$spam bar', '%s bar' % snonascii)
   226              check('${spam}bar', '%sbar' % snonascii)
   227              check('${%s}bar' % snonascii, 'ham%sbar' % snonascii)
   228              check('$bar%s bar' % snonascii, '$bar%s bar' % snonascii)
   229              check('$spam}bar', '%s}bar' % snonascii)
   230  
   231              check(unonascii, unonascii)
   232              check(u'$spam bar', u'%s bar' % unonascii)
   233              check(u'${spam}bar', u'%sbar' % unonascii)
   234              check(u'${%s}bar' % unonascii, u'ham%sbar' % unonascii)
   235              check(u'$bar%s bar' % unonascii, u'$bar%s bar' % unonascii)
   236              check(u'$spam}bar', u'%s}bar' % unonascii)
   237  
   238      def test_abspath(self):
   239          self.assertIn("foo", self.pathmodule.abspath("foo"))
   240  
   241          # Abspath returns bytes when the arg is bytes
   242          for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
   243              self.assertIsInstance(self.pathmodule.abspath(path), str)
   244  
   245      def test_realpath(self):
   246          self.assertIn("foo", self.pathmodule.realpath("foo"))
   247  
   248      @test_support.requires_unicode
   249      def test_normpath_issue5827(self):
   250          # Make sure normpath preserves unicode
   251          for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
   252              self.assertIsInstance(self.pathmodule.normpath(path), unicode)
   253  
   254      @test_support.requires_unicode
   255      def test_abspath_issue3426(self):
   256          # Check that abspath returns unicode when the arg is unicode
   257          # with both ASCII and non-ASCII cwds.
   258          abspath = self.pathmodule.abspath
   259          for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
   260              self.assertIsInstance(abspath(path), unicode)
   261  
   262          unicwd = u'\xe7w\xf0'
   263          try:
   264              fsencoding = test_support.TESTFN_ENCODING or "ascii"
   265              unicwd.encode(fsencoding)
   266          except (AttributeError, UnicodeEncodeError):
   267              # FS encoding is probably ASCII
   268              pass
   269          else:
   270              with test_support.temp_cwd(unicwd):
   271                  for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
   272                      self.assertIsInstance(abspath(path), unicode)
   273  
   274      @unittest.skipIf(sys.platform == 'darwin',
   275          "Mac OS X denies the creation of a directory with an invalid utf8 name")
   276      def test_nonascii_abspath(self):
   277          # Test non-ASCII, non-UTF8 bytes in the path.
   278          with test_support.temp_cwd('\xe7w\xf0'):
   279              self.test_abspath()
   280  
   281  
   282  def test_main():
   283      test_support.run_unittest(GenericTest)
   284  
   285  
   286  if __name__=="__main__":
   287      test_main()