17db96d56Sopenharmony_ciimport ntpath 27db96d56Sopenharmony_ciimport os 37db96d56Sopenharmony_ciimport string 47db96d56Sopenharmony_ciimport sys 57db96d56Sopenharmony_ciimport unittest 67db96d56Sopenharmony_ciimport warnings 77db96d56Sopenharmony_cifrom test.support import os_helper 87db96d56Sopenharmony_cifrom test.support import TestFailed, is_emscripten 97db96d56Sopenharmony_cifrom test.support.os_helper import FakePath 107db96d56Sopenharmony_cifrom test import test_genericpath 117db96d56Sopenharmony_cifrom tempfile import TemporaryFile 127db96d56Sopenharmony_ci 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_citry: 157db96d56Sopenharmony_ci import nt 167db96d56Sopenharmony_ciexcept ImportError: 177db96d56Sopenharmony_ci # Most tests can complete without the nt module, 187db96d56Sopenharmony_ci # but for those that require it we import here. 197db96d56Sopenharmony_ci nt = None 207db96d56Sopenharmony_ci 217db96d56Sopenharmony_citry: 227db96d56Sopenharmony_ci ntpath._getfinalpathname 237db96d56Sopenharmony_ciexcept AttributeError: 247db96d56Sopenharmony_ci HAVE_GETFINALPATHNAME = False 257db96d56Sopenharmony_cielse: 267db96d56Sopenharmony_ci HAVE_GETFINALPATHNAME = True 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_citry: 297db96d56Sopenharmony_ci import ctypes 307db96d56Sopenharmony_ciexcept ImportError: 317db96d56Sopenharmony_ci HAVE_GETSHORTPATHNAME = False 327db96d56Sopenharmony_cielse: 337db96d56Sopenharmony_ci HAVE_GETSHORTPATHNAME = True 347db96d56Sopenharmony_ci def _getshortpathname(path): 357db96d56Sopenharmony_ci GSPN = ctypes.WinDLL("kernel32", use_last_error=True).GetShortPathNameW 367db96d56Sopenharmony_ci GSPN.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32] 377db96d56Sopenharmony_ci GSPN.restype = ctypes.c_uint32 387db96d56Sopenharmony_ci result_len = GSPN(path, None, 0) 397db96d56Sopenharmony_ci if not result_len: 407db96d56Sopenharmony_ci raise OSError("failed to get short path name 0x{:08X}" 417db96d56Sopenharmony_ci .format(ctypes.get_last_error())) 427db96d56Sopenharmony_ci result = ctypes.create_unicode_buffer(result_len) 437db96d56Sopenharmony_ci result_len = GSPN(path, result, result_len) 447db96d56Sopenharmony_ci return result[:result_len] 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_cidef _norm(path): 477db96d56Sopenharmony_ci if isinstance(path, (bytes, str, os.PathLike)): 487db96d56Sopenharmony_ci return ntpath.normcase(os.fsdecode(path)) 497db96d56Sopenharmony_ci elif hasattr(path, "__iter__"): 507db96d56Sopenharmony_ci return tuple(ntpath.normcase(os.fsdecode(p)) for p in path) 517db96d56Sopenharmony_ci return path 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_cidef tester(fn, wantResult): 557db96d56Sopenharmony_ci fn = fn.replace("\\", "\\\\") 567db96d56Sopenharmony_ci gotResult = eval(fn) 577db96d56Sopenharmony_ci if wantResult != gotResult and _norm(wantResult) != _norm(gotResult): 587db96d56Sopenharmony_ci raise TestFailed("%s should return: %s but returned: %s" \ 597db96d56Sopenharmony_ci %(str(fn), str(wantResult), str(gotResult))) 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci # then with bytes 627db96d56Sopenharmony_ci fn = fn.replace("('", "(b'") 637db96d56Sopenharmony_ci fn = fn.replace('("', '(b"') 647db96d56Sopenharmony_ci fn = fn.replace("['", "[b'") 657db96d56Sopenharmony_ci fn = fn.replace('["', '[b"') 667db96d56Sopenharmony_ci fn = fn.replace(", '", ", b'") 677db96d56Sopenharmony_ci fn = fn.replace(', "', ', b"') 687db96d56Sopenharmony_ci fn = os.fsencode(fn).decode('latin1') 697db96d56Sopenharmony_ci fn = fn.encode('ascii', 'backslashreplace').decode('ascii') 707db96d56Sopenharmony_ci with warnings.catch_warnings(): 717db96d56Sopenharmony_ci warnings.simplefilter("ignore", DeprecationWarning) 727db96d56Sopenharmony_ci gotResult = eval(fn) 737db96d56Sopenharmony_ci if _norm(wantResult) != _norm(gotResult): 747db96d56Sopenharmony_ci raise TestFailed("%s should return: %s but returned: %s" \ 757db96d56Sopenharmony_ci %(str(fn), str(wantResult), repr(gotResult))) 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ciclass NtpathTestCase(unittest.TestCase): 797db96d56Sopenharmony_ci def assertPathEqual(self, path1, path2): 807db96d56Sopenharmony_ci if path1 == path2 or _norm(path1) == _norm(path2): 817db96d56Sopenharmony_ci return 827db96d56Sopenharmony_ci self.assertEqual(path1, path2) 837db96d56Sopenharmony_ci 847db96d56Sopenharmony_ci def assertPathIn(self, path, pathset): 857db96d56Sopenharmony_ci self.assertIn(_norm(path), _norm(pathset)) 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ciclass TestNtpath(NtpathTestCase): 897db96d56Sopenharmony_ci def test_splitext(self): 907db96d56Sopenharmony_ci tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) 917db96d56Sopenharmony_ci tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) 927db96d56Sopenharmony_ci tester('ntpath.splitext(".ext")', ('.ext', '')) 937db96d56Sopenharmony_ci tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) 947db96d56Sopenharmony_ci tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) 957db96d56Sopenharmony_ci tester('ntpath.splitext("")', ('', '')) 967db96d56Sopenharmony_ci tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) 977db96d56Sopenharmony_ci tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) 987db96d56Sopenharmony_ci tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) 997db96d56Sopenharmony_ci tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d')) 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ci def test_splitdrive(self): 1027db96d56Sopenharmony_ci tester('ntpath.splitdrive("c:\\foo\\bar")', 1037db96d56Sopenharmony_ci ('c:', '\\foo\\bar')) 1047db96d56Sopenharmony_ci tester('ntpath.splitdrive("c:/foo/bar")', 1057db96d56Sopenharmony_ci ('c:', '/foo/bar')) 1067db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")', 1077db96d56Sopenharmony_ci ('\\\\conky\\mountpoint', '\\foo\\bar')) 1087db96d56Sopenharmony_ci tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', 1097db96d56Sopenharmony_ci ('//conky/mountpoint', '/foo/bar')) 1107db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")', 1117db96d56Sopenharmony_ci ('\\\\\\conky', '\\mountpoint\\foo\\bar')) 1127db96d56Sopenharmony_ci tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")', 1137db96d56Sopenharmony_ci ('///conky', '/mountpoint/foo/bar')) 1147db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")', 1157db96d56Sopenharmony_ci ('\\\\conky\\', '\\mountpoint\\foo\\bar')) 1167db96d56Sopenharmony_ci tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")', 1177db96d56Sopenharmony_ci ('//conky/', '/mountpoint/foo/bar')) 1187db96d56Sopenharmony_ci # Issue #19911: UNC part containing U+0130 1197db96d56Sopenharmony_ci self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOİNT/foo/bar'), 1207db96d56Sopenharmony_ci ('//conky/MOUNTPOİNT', '/foo/bar')) 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci # gh-81790: support device namespace, including UNC drives. 1237db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/c:")', ("//?/c:", "")) 1247db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/c:/")', ("//?/c:", "/")) 1257db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/c:/dir")', ("//?/c:", "/dir")) 1267db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/UNC")', ("//?/UNC", "")) 1277db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/UNC/")', ("//?/UNC/", "")) 1287db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/UNC/server/")', ("//?/UNC/server/", "")) 1297db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/UNC/server/share")', ("//?/UNC/server/share", "")) 1307db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/UNC/server/share/dir")', ("//?/UNC/server/share", "/dir")) 1317db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/VOLUME{00000000-0000-0000-0000-000000000000}/spam")', 1327db96d56Sopenharmony_ci ('//?/VOLUME{00000000-0000-0000-0000-000000000000}', '/spam')) 1337db96d56Sopenharmony_ci tester('ntpath.splitdrive("//?/BootPartition/")', ("//?/BootPartition", "/")) 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\c:")', ("\\\\?\\c:", "")) 1367db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\c:\\")', ("\\\\?\\c:", "\\")) 1377db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\c:\\dir")', ("\\\\?\\c:", "\\dir")) 1387db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\UNC")', ("\\\\?\\UNC", "")) 1397db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("\\\\?\\UNC\\", "")) 1407db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\UNC\\server\\")', ("\\\\?\\UNC\\server\\", "")) 1417db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share")', ("\\\\?\\UNC\\server\\share", "")) 1427db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share\\dir")', 1437db96d56Sopenharmony_ci ("\\\\?\\UNC\\server\\share", "\\dir")) 1447db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}\\spam")', 1457db96d56Sopenharmony_ci ('\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}', '\\spam')) 1467db96d56Sopenharmony_ci tester('ntpath.splitdrive("\\\\?\\BootPartition\\")', ("\\\\?\\BootPartition", "\\")) 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ci # gh-96290: support partial/invalid UNC drives 1497db96d56Sopenharmony_ci tester('ntpath.splitdrive("//")', ("//", "")) # empty server & missing share 1507db96d56Sopenharmony_ci tester('ntpath.splitdrive("///")', ("///", "")) # empty server & empty share 1517db96d56Sopenharmony_ci tester('ntpath.splitdrive("///y")', ("///y", "")) # empty server & non-empty share 1527db96d56Sopenharmony_ci tester('ntpath.splitdrive("//x")', ("//x", "")) # non-empty server & missing share 1537db96d56Sopenharmony_ci tester('ntpath.splitdrive("//x/")', ("//x/", "")) # non-empty server & empty share 1547db96d56Sopenharmony_ci 1557db96d56Sopenharmony_ci def test_split(self): 1567db96d56Sopenharmony_ci tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) 1577db96d56Sopenharmony_ci tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', 1587db96d56Sopenharmony_ci ('\\\\conky\\mountpoint\\foo', 'bar')) 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ci tester('ntpath.split("c:\\")', ('c:\\', '')) 1617db96d56Sopenharmony_ci tester('ntpath.split("\\\\conky\\mountpoint\\")', 1627db96d56Sopenharmony_ci ('\\\\conky\\mountpoint\\', '')) 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci tester('ntpath.split("c:/")', ('c:/', '')) 1657db96d56Sopenharmony_ci tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', '')) 1667db96d56Sopenharmony_ci 1677db96d56Sopenharmony_ci def test_isabs(self): 1687db96d56Sopenharmony_ci tester('ntpath.isabs("c:\\")', 1) 1697db96d56Sopenharmony_ci tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) 1707db96d56Sopenharmony_ci tester('ntpath.isabs("\\foo")', 1) 1717db96d56Sopenharmony_ci tester('ntpath.isabs("\\foo\\bar")', 1) 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci # gh-96290: normal UNC paths and device paths without trailing backslashes 1747db96d56Sopenharmony_ci tester('ntpath.isabs("\\\\conky\\mountpoint")', 1) 1757db96d56Sopenharmony_ci tester('ntpath.isabs("\\\\.\\C:")', 1) 1767db96d56Sopenharmony_ci 1777db96d56Sopenharmony_ci def test_commonprefix(self): 1787db96d56Sopenharmony_ci tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', 1797db96d56Sopenharmony_ci "/home/swen") 1807db96d56Sopenharmony_ci tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', 1817db96d56Sopenharmony_ci "\\home\\swen\\") 1827db96d56Sopenharmony_ci tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', 1837db96d56Sopenharmony_ci "/home/swen/spam") 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci def test_join(self): 1867db96d56Sopenharmony_ci tester('ntpath.join("")', '') 1877db96d56Sopenharmony_ci tester('ntpath.join("", "", "")', '') 1887db96d56Sopenharmony_ci tester('ntpath.join("a")', 'a') 1897db96d56Sopenharmony_ci tester('ntpath.join("/a")', '/a') 1907db96d56Sopenharmony_ci tester('ntpath.join("\\a")', '\\a') 1917db96d56Sopenharmony_ci tester('ntpath.join("a:")', 'a:') 1927db96d56Sopenharmony_ci tester('ntpath.join("a:", "\\b")', 'a:\\b') 1937db96d56Sopenharmony_ci tester('ntpath.join("a", "\\b")', '\\b') 1947db96d56Sopenharmony_ci tester('ntpath.join("a", "b", "c")', 'a\\b\\c') 1957db96d56Sopenharmony_ci tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') 1967db96d56Sopenharmony_ci tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') 1977db96d56Sopenharmony_ci tester('ntpath.join("a", "b", "\\c")', '\\c') 1987db96d56Sopenharmony_ci tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') 1997db96d56Sopenharmony_ci tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') 2007db96d56Sopenharmony_ci 2017db96d56Sopenharmony_ci tester("ntpath.join('', 'a')", 'a') 2027db96d56Sopenharmony_ci tester("ntpath.join('', '', '', '', 'a')", 'a') 2037db96d56Sopenharmony_ci tester("ntpath.join('a', '')", 'a\\') 2047db96d56Sopenharmony_ci tester("ntpath.join('a', '', '', '', '')", 'a\\') 2057db96d56Sopenharmony_ci tester("ntpath.join('a\\', '')", 'a\\') 2067db96d56Sopenharmony_ci tester("ntpath.join('a\\', '', '', '', '')", 'a\\') 2077db96d56Sopenharmony_ci tester("ntpath.join('a/', '')", 'a/') 2087db96d56Sopenharmony_ci 2097db96d56Sopenharmony_ci tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y') 2107db96d56Sopenharmony_ci tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y') 2117db96d56Sopenharmony_ci tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y') 2127db96d56Sopenharmony_ci tester("ntpath.join('c:', 'x/y')", 'c:x/y') 2137db96d56Sopenharmony_ci tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y') 2147db96d56Sopenharmony_ci tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y') 2157db96d56Sopenharmony_ci tester("ntpath.join('c:/', 'x/y')", 'c:/x/y') 2167db96d56Sopenharmony_ci tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y') 2177db96d56Sopenharmony_ci tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y') 2187db96d56Sopenharmony_ci tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y') 2197db96d56Sopenharmony_ci tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y') 2207db96d56Sopenharmony_ci tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y') 2217db96d56Sopenharmony_ci 2227db96d56Sopenharmony_ci tester("ntpath.join('a/b', '/x/y')", '/x/y') 2237db96d56Sopenharmony_ci tester("ntpath.join('/a/b', '/x/y')", '/x/y') 2247db96d56Sopenharmony_ci tester("ntpath.join('c:', '/x/y')", 'c:/x/y') 2257db96d56Sopenharmony_ci tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y') 2267db96d56Sopenharmony_ci tester("ntpath.join('c:/', '/x/y')", 'c:/x/y') 2277db96d56Sopenharmony_ci tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y') 2287db96d56Sopenharmony_ci tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y') 2297db96d56Sopenharmony_ci tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y') 2307db96d56Sopenharmony_ci tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y') 2317db96d56Sopenharmony_ci 2327db96d56Sopenharmony_ci tester("ntpath.join('c:', 'C:x/y')", 'C:x/y') 2337db96d56Sopenharmony_ci tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y') 2347db96d56Sopenharmony_ci tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y') 2357db96d56Sopenharmony_ci tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y') 2367db96d56Sopenharmony_ci 2377db96d56Sopenharmony_ci for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b', 2387db96d56Sopenharmony_ci '//computer/share', '//computer/share/', '//computer/share/a/b'): 2397db96d56Sopenharmony_ci for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y', 2407db96d56Sopenharmony_ci '//machine/common', '//machine/common/', '//machine/common/x/y'): 2417db96d56Sopenharmony_ci tester("ntpath.join(%r, %r)" % (x, y), y) 2427db96d56Sopenharmony_ci 2437db96d56Sopenharmony_ci tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b') 2447db96d56Sopenharmony_ci tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b') 2457db96d56Sopenharmony_ci tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b') 2467db96d56Sopenharmony_ci tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b') 2477db96d56Sopenharmony_ci tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b') 2487db96d56Sopenharmony_ci tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b') 2497db96d56Sopenharmony_ci 2507db96d56Sopenharmony_ci def test_normpath(self): 2517db96d56Sopenharmony_ci tester("ntpath.normpath('A//////././//.//B')", r'A\B') 2527db96d56Sopenharmony_ci tester("ntpath.normpath('A/./B')", r'A\B') 2537db96d56Sopenharmony_ci tester("ntpath.normpath('A/foo/../B')", r'A\B') 2547db96d56Sopenharmony_ci tester("ntpath.normpath('C:A//B')", r'C:A\B') 2557db96d56Sopenharmony_ci tester("ntpath.normpath('D:A/./B')", r'D:A\B') 2567db96d56Sopenharmony_ci tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') 2577db96d56Sopenharmony_ci 2587db96d56Sopenharmony_ci tester("ntpath.normpath('C:///A//B')", r'C:\A\B') 2597db96d56Sopenharmony_ci tester("ntpath.normpath('D:///A/./B')", r'D:\A\B') 2607db96d56Sopenharmony_ci tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B') 2617db96d56Sopenharmony_ci 2627db96d56Sopenharmony_ci tester("ntpath.normpath('..')", r'..') 2637db96d56Sopenharmony_ci tester("ntpath.normpath('.')", r'.') 2647db96d56Sopenharmony_ci tester("ntpath.normpath('')", r'.') 2657db96d56Sopenharmony_ci tester("ntpath.normpath('/')", '\\') 2667db96d56Sopenharmony_ci tester("ntpath.normpath('c:/')", 'c:\\') 2677db96d56Sopenharmony_ci tester("ntpath.normpath('/../.././..')", '\\') 2687db96d56Sopenharmony_ci tester("ntpath.normpath('c:/../../..')", 'c:\\') 2697db96d56Sopenharmony_ci tester("ntpath.normpath('../.././..')", r'..\..\..') 2707db96d56Sopenharmony_ci tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') 2717db96d56Sopenharmony_ci tester("ntpath.normpath('C:////a/b')", r'C:\a\b') 2727db96d56Sopenharmony_ci tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') 2737db96d56Sopenharmony_ci 2747db96d56Sopenharmony_ci tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL') 2757db96d56Sopenharmony_ci tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z') 2767db96d56Sopenharmony_ci tester("ntpath.normpath('handbook/../../Tests/image.png')", r'..\Tests\image.png') 2777db96d56Sopenharmony_ci tester("ntpath.normpath('handbook/../../../Tests/image.png')", r'..\..\Tests\image.png') 2787db96d56Sopenharmony_ci tester("ntpath.normpath('handbook///../a/.././../b/c')", r'..\b\c') 2797db96d56Sopenharmony_ci tester("ntpath.normpath('handbook/a/../..///../../b/c')", r'..\..\b\c') 2807db96d56Sopenharmony_ci 2817db96d56Sopenharmony_ci tester("ntpath.normpath('//server/share/..')" , '\\\\server\\share\\') 2827db96d56Sopenharmony_ci tester("ntpath.normpath('//server/share/../')" , '\\\\server\\share\\') 2837db96d56Sopenharmony_ci tester("ntpath.normpath('//server/share/../..')", '\\\\server\\share\\') 2847db96d56Sopenharmony_ci tester("ntpath.normpath('//server/share/../../')", '\\\\server\\share\\') 2857db96d56Sopenharmony_ci 2867db96d56Sopenharmony_ci # gh-96290: don't normalize partial/invalid UNC drives as rooted paths. 2877db96d56Sopenharmony_ci tester("ntpath.normpath('\\\\foo\\\\')", '\\\\foo\\\\') 2887db96d56Sopenharmony_ci tester("ntpath.normpath('\\\\foo\\')", '\\\\foo\\') 2897db96d56Sopenharmony_ci tester("ntpath.normpath('\\\\foo')", '\\\\foo') 2907db96d56Sopenharmony_ci tester("ntpath.normpath('\\\\')", '\\\\') 2917db96d56Sopenharmony_ci 2927db96d56Sopenharmony_ci def test_realpath_curdir(self): 2937db96d56Sopenharmony_ci expected = ntpath.normpath(os.getcwd()) 2947db96d56Sopenharmony_ci tester("ntpath.realpath('.')", expected) 2957db96d56Sopenharmony_ci tester("ntpath.realpath('./.')", expected) 2967db96d56Sopenharmony_ci tester("ntpath.realpath('/'.join(['.'] * 100))", expected) 2977db96d56Sopenharmony_ci tester("ntpath.realpath('.\\.')", expected) 2987db96d56Sopenharmony_ci tester("ntpath.realpath('\\'.join(['.'] * 100))", expected) 2997db96d56Sopenharmony_ci 3007db96d56Sopenharmony_ci def test_realpath_pardir(self): 3017db96d56Sopenharmony_ci expected = ntpath.normpath(os.getcwd()) 3027db96d56Sopenharmony_ci tester("ntpath.realpath('..')", ntpath.dirname(expected)) 3037db96d56Sopenharmony_ci tester("ntpath.realpath('../..')", 3047db96d56Sopenharmony_ci ntpath.dirname(ntpath.dirname(expected))) 3057db96d56Sopenharmony_ci tester("ntpath.realpath('/'.join(['..'] * 50))", 3067db96d56Sopenharmony_ci ntpath.splitdrive(expected)[0] + '\\') 3077db96d56Sopenharmony_ci tester("ntpath.realpath('..\\..')", 3087db96d56Sopenharmony_ci ntpath.dirname(ntpath.dirname(expected))) 3097db96d56Sopenharmony_ci tester("ntpath.realpath('\\'.join(['..'] * 50))", 3107db96d56Sopenharmony_ci ntpath.splitdrive(expected)[0] + '\\') 3117db96d56Sopenharmony_ci 3127db96d56Sopenharmony_ci @os_helper.skip_unless_symlink 3137db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 3147db96d56Sopenharmony_ci def test_realpath_basic(self): 3157db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 3167db96d56Sopenharmony_ci open(ABSTFN, "wb").close() 3177db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN) 3187db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "1") 3197db96d56Sopenharmony_ci 3207db96d56Sopenharmony_ci os.symlink(ABSTFN, ABSTFN + "1") 3217db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN) 3227db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(os.fsencode(ABSTFN + "1")), 3237db96d56Sopenharmony_ci os.fsencode(ABSTFN)) 3247db96d56Sopenharmony_ci 3257db96d56Sopenharmony_ci # gh-88013: call ntpath.realpath with binary drive name may raise a 3267db96d56Sopenharmony_ci # TypeError. The drive should not exist to reproduce the bug. 3277db96d56Sopenharmony_ci for c in string.ascii_uppercase: 3287db96d56Sopenharmony_ci d = f"{c}:\\" 3297db96d56Sopenharmony_ci if not ntpath.exists(d): 3307db96d56Sopenharmony_ci break 3317db96d56Sopenharmony_ci else: 3327db96d56Sopenharmony_ci raise OSError("No free drive letters available") 3337db96d56Sopenharmony_ci self.assertEqual(ntpath.realpath(d), d) 3347db96d56Sopenharmony_ci 3357db96d56Sopenharmony_ci # gh-106242: Embedded nulls and non-strict fallback to abspath 3367db96d56Sopenharmony_ci self.assertEqual(ABSTFN + "\0spam", 3377db96d56Sopenharmony_ci ntpath.realpath(os_helper.TESTFN + "\0spam", strict=False)) 3387db96d56Sopenharmony_ci 3397db96d56Sopenharmony_ci @os_helper.skip_unless_symlink 3407db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 3417db96d56Sopenharmony_ci def test_realpath_strict(self): 3427db96d56Sopenharmony_ci # Bug #43757: raise FileNotFoundError in strict mode if we encounter 3437db96d56Sopenharmony_ci # a path that does not exist. 3447db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 3457db96d56Sopenharmony_ci os.symlink(ABSTFN + "1", ABSTFN) 3467db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN) 3477db96d56Sopenharmony_ci self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN, strict=True) 3487db96d56Sopenharmony_ci self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN + "2", strict=True) 3497db96d56Sopenharmony_ci # gh-106242: Embedded nulls should raise OSError (not ValueError) 3507db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "\0spam", strict=True) 3517db96d56Sopenharmony_ci 3527db96d56Sopenharmony_ci @os_helper.skip_unless_symlink 3537db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 3547db96d56Sopenharmony_ci def test_realpath_relative(self): 3557db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 3567db96d56Sopenharmony_ci open(ABSTFN, "wb").close() 3577db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN) 3587db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "1") 3597db96d56Sopenharmony_ci 3607db96d56Sopenharmony_ci os.symlink(ABSTFN, ntpath.relpath(ABSTFN + "1")) 3617db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "1"), ABSTFN) 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci @os_helper.skip_unless_symlink 3647db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 3657db96d56Sopenharmony_ci def test_realpath_broken_symlinks(self): 3667db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 3677db96d56Sopenharmony_ci os.mkdir(ABSTFN) 3687db96d56Sopenharmony_ci self.addCleanup(os_helper.rmtree, ABSTFN) 3697db96d56Sopenharmony_ci 3707db96d56Sopenharmony_ci with os_helper.change_cwd(ABSTFN): 3717db96d56Sopenharmony_ci os.mkdir("subdir") 3727db96d56Sopenharmony_ci os.chdir("subdir") 3737db96d56Sopenharmony_ci os.symlink(".", "recursive") 3747db96d56Sopenharmony_ci os.symlink("..", "parent") 3757db96d56Sopenharmony_ci os.chdir("..") 3767db96d56Sopenharmony_ci os.symlink(".", "self") 3777db96d56Sopenharmony_ci os.symlink("missing", "broken") 3787db96d56Sopenharmony_ci os.symlink(r"broken\bar", "broken1") 3797db96d56Sopenharmony_ci os.symlink(r"self\self\broken", "broken2") 3807db96d56Sopenharmony_ci os.symlink(r"subdir\parent\subdir\parent\broken", "broken3") 3817db96d56Sopenharmony_ci os.symlink(ABSTFN + r"\broken", "broken4") 3827db96d56Sopenharmony_ci os.symlink(r"recursive\..\broken", "broken5") 3837db96d56Sopenharmony_ci 3847db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath("broken"), 3857db96d56Sopenharmony_ci ABSTFN + r"\missing") 3867db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(r"broken\foo"), 3877db96d56Sopenharmony_ci ABSTFN + r"\missing\foo") 3887db96d56Sopenharmony_ci # bpo-38453: We no longer recursively resolve segments of relative 3897db96d56Sopenharmony_ci # symlinks that the OS cannot resolve. 3907db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(r"broken1"), 3917db96d56Sopenharmony_ci ABSTFN + r"\broken\bar") 3927db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(r"broken1\baz"), 3937db96d56Sopenharmony_ci ABSTFN + r"\broken\bar\baz") 3947db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath("broken2"), 3957db96d56Sopenharmony_ci ABSTFN + r"\self\self\missing") 3967db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath("broken3"), 3977db96d56Sopenharmony_ci ABSTFN + r"\subdir\parent\subdir\parent\missing") 3987db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath("broken4"), 3997db96d56Sopenharmony_ci ABSTFN + r"\missing") 4007db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath("broken5"), 4017db96d56Sopenharmony_ci ABSTFN + r"\missing") 4027db96d56Sopenharmony_ci 4037db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(b"broken"), 4047db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\missing")) 4057db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(rb"broken\foo"), 4067db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\missing\foo")) 4077db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(rb"broken1"), 4087db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\broken\bar")) 4097db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(rb"broken1\baz"), 4107db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\broken\bar\baz")) 4117db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(b"broken2"), 4127db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\self\self\missing")) 4137db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(rb"broken3"), 4147db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\subdir\parent\subdir\parent\missing")) 4157db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(b"broken4"), 4167db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\missing")) 4177db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(b"broken5"), 4187db96d56Sopenharmony_ci os.fsencode(ABSTFN + r"\missing")) 4197db96d56Sopenharmony_ci 4207db96d56Sopenharmony_ci @os_helper.skip_unless_symlink 4217db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 4227db96d56Sopenharmony_ci def test_realpath_symlink_loops(self): 4237db96d56Sopenharmony_ci # Symlink loops in non-strict mode are non-deterministic as to which 4247db96d56Sopenharmony_ci # path is returned, but it will always be the fully resolved path of 4257db96d56Sopenharmony_ci # one member of the cycle 4267db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 4277db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN) 4287db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "1") 4297db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "2") 4307db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "y") 4317db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "c") 4327db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "a") 4337db96d56Sopenharmony_ci 4347db96d56Sopenharmony_ci os.symlink(ABSTFN, ABSTFN) 4357db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN), ABSTFN) 4367db96d56Sopenharmony_ci 4377db96d56Sopenharmony_ci os.symlink(ABSTFN + "1", ABSTFN + "2") 4387db96d56Sopenharmony_ci os.symlink(ABSTFN + "2", ABSTFN + "1") 4397db96d56Sopenharmony_ci expected = (ABSTFN + "1", ABSTFN + "2") 4407db96d56Sopenharmony_ci self.assertPathIn(ntpath.realpath(ABSTFN + "1"), expected) 4417db96d56Sopenharmony_ci self.assertPathIn(ntpath.realpath(ABSTFN + "2"), expected) 4427db96d56Sopenharmony_ci 4437db96d56Sopenharmony_ci self.assertPathIn(ntpath.realpath(ABSTFN + "1\\x"), 4447db96d56Sopenharmony_ci (ntpath.join(r, "x") for r in expected)) 4457db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\.."), 4467db96d56Sopenharmony_ci ntpath.dirname(ABSTFN)) 4477db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\x"), 4487db96d56Sopenharmony_ci ntpath.dirname(ABSTFN) + "\\x") 4497db96d56Sopenharmony_ci os.symlink(ABSTFN + "x", ABSTFN + "y") 4507db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..\\" 4517db96d56Sopenharmony_ci + ntpath.basename(ABSTFN) + "y"), 4527db96d56Sopenharmony_ci ABSTFN + "x") 4537db96d56Sopenharmony_ci self.assertPathIn(ntpath.realpath(ABSTFN + "1\\..\\" 4547db96d56Sopenharmony_ci + ntpath.basename(ABSTFN) + "1"), 4557db96d56Sopenharmony_ci expected) 4567db96d56Sopenharmony_ci 4577db96d56Sopenharmony_ci os.symlink(ntpath.basename(ABSTFN) + "a\\b", ABSTFN + "a") 4587db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "a"), ABSTFN + "a") 4597db96d56Sopenharmony_ci 4607db96d56Sopenharmony_ci os.symlink("..\\" + ntpath.basename(ntpath.dirname(ABSTFN)) 4617db96d56Sopenharmony_ci + "\\" + ntpath.basename(ABSTFN) + "c", ABSTFN + "c") 4627db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "c"), ABSTFN + "c") 4637db96d56Sopenharmony_ci 4647db96d56Sopenharmony_ci # Test using relative path as well. 4657db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ntpath.basename(ABSTFN)), ABSTFN) 4667db96d56Sopenharmony_ci 4677db96d56Sopenharmony_ci @os_helper.skip_unless_symlink 4687db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 4697db96d56Sopenharmony_ci def test_realpath_symlink_loops_strict(self): 4707db96d56Sopenharmony_ci # Symlink loops raise OSError in strict mode 4717db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 4727db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN) 4737db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "1") 4747db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "2") 4757db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "y") 4767db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "c") 4777db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "a") 4787db96d56Sopenharmony_ci 4797db96d56Sopenharmony_ci os.symlink(ABSTFN, ABSTFN) 4807db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN, strict=True) 4817db96d56Sopenharmony_ci 4827db96d56Sopenharmony_ci os.symlink(ABSTFN + "1", ABSTFN + "2") 4837db96d56Sopenharmony_ci os.symlink(ABSTFN + "2", ABSTFN + "1") 4847db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1", strict=True) 4857db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "2", strict=True) 4867db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\x", strict=True) 4877db96d56Sopenharmony_ci # Windows eliminates '..' components before resolving links, so the 4887db96d56Sopenharmony_ci # following call is not expected to raise. 4897db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "1\\..", strict=True), 4907db96d56Sopenharmony_ci ntpath.dirname(ABSTFN)) 4917db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\..\\x", strict=True) 4927db96d56Sopenharmony_ci os.symlink(ABSTFN + "x", ABSTFN + "y") 4937db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "1\\..\\" 4947db96d56Sopenharmony_ci + ntpath.basename(ABSTFN) + "y", 4957db96d56Sopenharmony_ci strict=True) 4967db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, 4977db96d56Sopenharmony_ci ABSTFN + "1\\..\\" + ntpath.basename(ABSTFN) + "1", 4987db96d56Sopenharmony_ci strict=True) 4997db96d56Sopenharmony_ci 5007db96d56Sopenharmony_ci os.symlink(ntpath.basename(ABSTFN) + "a\\b", ABSTFN + "a") 5017db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "a", strict=True) 5027db96d56Sopenharmony_ci 5037db96d56Sopenharmony_ci os.symlink("..\\" + ntpath.basename(ntpath.dirname(ABSTFN)) 5047db96d56Sopenharmony_ci + "\\" + ntpath.basename(ABSTFN) + "c", ABSTFN + "c") 5057db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ABSTFN + "c", strict=True) 5067db96d56Sopenharmony_ci 5077db96d56Sopenharmony_ci # Test using relative path as well. 5087db96d56Sopenharmony_ci self.assertRaises(OSError, ntpath.realpath, ntpath.basename(ABSTFN), 5097db96d56Sopenharmony_ci strict=True) 5107db96d56Sopenharmony_ci 5117db96d56Sopenharmony_ci @os_helper.skip_unless_symlink 5127db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 5137db96d56Sopenharmony_ci def test_realpath_symlink_prefix(self): 5147db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 5157db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "3") 5167db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, "\\\\?\\" + ABSTFN + "3.") 5177db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "3link") 5187db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, ABSTFN + "3.link") 5197db96d56Sopenharmony_ci 5207db96d56Sopenharmony_ci with open(ABSTFN + "3", "wb") as f: 5217db96d56Sopenharmony_ci f.write(b'0') 5227db96d56Sopenharmony_ci os.symlink(ABSTFN + "3", ABSTFN + "3link") 5237db96d56Sopenharmony_ci 5247db96d56Sopenharmony_ci with open("\\\\?\\" + ABSTFN + "3.", "wb") as f: 5257db96d56Sopenharmony_ci f.write(b'1') 5267db96d56Sopenharmony_ci os.symlink("\\\\?\\" + ABSTFN + "3.", ABSTFN + "3.link") 5277db96d56Sopenharmony_ci 5287db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "3link"), 5297db96d56Sopenharmony_ci ABSTFN + "3") 5307db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath(ABSTFN + "3.link"), 5317db96d56Sopenharmony_ci "\\\\?\\" + ABSTFN + "3.") 5327db96d56Sopenharmony_ci 5337db96d56Sopenharmony_ci # Resolved paths should be usable to open target files 5347db96d56Sopenharmony_ci with open(ntpath.realpath(ABSTFN + "3link"), "rb") as f: 5357db96d56Sopenharmony_ci self.assertEqual(f.read(), b'0') 5367db96d56Sopenharmony_ci with open(ntpath.realpath(ABSTFN + "3.link"), "rb") as f: 5377db96d56Sopenharmony_ci self.assertEqual(f.read(), b'1') 5387db96d56Sopenharmony_ci 5397db96d56Sopenharmony_ci # When the prefix is included, it is not stripped 5407db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath("\\\\?\\" + ABSTFN + "3link"), 5417db96d56Sopenharmony_ci "\\\\?\\" + ABSTFN + "3") 5427db96d56Sopenharmony_ci self.assertPathEqual(ntpath.realpath("\\\\?\\" + ABSTFN + "3.link"), 5437db96d56Sopenharmony_ci "\\\\?\\" + ABSTFN + "3.") 5447db96d56Sopenharmony_ci 5457db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 5467db96d56Sopenharmony_ci def test_realpath_nul(self): 5477db96d56Sopenharmony_ci tester("ntpath.realpath('NUL')", r'\\.\NUL') 5487db96d56Sopenharmony_ci 5497db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname') 5507db96d56Sopenharmony_ci @unittest.skipUnless(HAVE_GETSHORTPATHNAME, 'need _getshortpathname') 5517db96d56Sopenharmony_ci def test_realpath_cwd(self): 5527db96d56Sopenharmony_ci ABSTFN = ntpath.abspath(os_helper.TESTFN) 5537db96d56Sopenharmony_ci 5547db96d56Sopenharmony_ci os_helper.unlink(ABSTFN) 5557db96d56Sopenharmony_ci os_helper.rmtree(ABSTFN) 5567db96d56Sopenharmony_ci os.mkdir(ABSTFN) 5577db96d56Sopenharmony_ci self.addCleanup(os_helper.rmtree, ABSTFN) 5587db96d56Sopenharmony_ci 5597db96d56Sopenharmony_ci test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName") 5607db96d56Sopenharmony_ci os.mkdir(test_dir_long) 5617db96d56Sopenharmony_ci 5627db96d56Sopenharmony_ci test_dir_short = _getshortpathname(test_dir_long) 5637db96d56Sopenharmony_ci test_file_long = ntpath.join(test_dir_long, "file.txt") 5647db96d56Sopenharmony_ci test_file_short = ntpath.join(test_dir_short, "file.txt") 5657db96d56Sopenharmony_ci 5667db96d56Sopenharmony_ci with open(test_file_long, "wb") as f: 5677db96d56Sopenharmony_ci f.write(b"content") 5687db96d56Sopenharmony_ci 5697db96d56Sopenharmony_ci self.assertPathEqual(test_file_long, ntpath.realpath(test_file_short)) 5707db96d56Sopenharmony_ci 5717db96d56Sopenharmony_ci with os_helper.change_cwd(test_dir_long): 5727db96d56Sopenharmony_ci self.assertPathEqual(test_file_long, ntpath.realpath("file.txt")) 5737db96d56Sopenharmony_ci with os_helper.change_cwd(test_dir_long.lower()): 5747db96d56Sopenharmony_ci self.assertPathEqual(test_file_long, ntpath.realpath("file.txt")) 5757db96d56Sopenharmony_ci with os_helper.change_cwd(test_dir_short): 5767db96d56Sopenharmony_ci self.assertPathEqual(test_file_long, ntpath.realpath("file.txt")) 5777db96d56Sopenharmony_ci 5787db96d56Sopenharmony_ci def test_expandvars(self): 5797db96d56Sopenharmony_ci with os_helper.EnvironmentVarGuard() as env: 5807db96d56Sopenharmony_ci env.clear() 5817db96d56Sopenharmony_ci env["foo"] = "bar" 5827db96d56Sopenharmony_ci env["{foo"] = "baz1" 5837db96d56Sopenharmony_ci env["{foo}"] = "baz2" 5847db96d56Sopenharmony_ci tester('ntpath.expandvars("foo")', "foo") 5857db96d56Sopenharmony_ci tester('ntpath.expandvars("$foo bar")', "bar bar") 5867db96d56Sopenharmony_ci tester('ntpath.expandvars("${foo}bar")', "barbar") 5877db96d56Sopenharmony_ci tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") 5887db96d56Sopenharmony_ci tester('ntpath.expandvars("$bar bar")', "$bar bar") 5897db96d56Sopenharmony_ci tester('ntpath.expandvars("$?bar")', "$?bar") 5907db96d56Sopenharmony_ci tester('ntpath.expandvars("$foo}bar")', "bar}bar") 5917db96d56Sopenharmony_ci tester('ntpath.expandvars("${foo")', "${foo") 5927db96d56Sopenharmony_ci tester('ntpath.expandvars("${{foo}}")', "baz1}") 5937db96d56Sopenharmony_ci tester('ntpath.expandvars("$foo$foo")', "barbar") 5947db96d56Sopenharmony_ci tester('ntpath.expandvars("$bar$bar")', "$bar$bar") 5957db96d56Sopenharmony_ci tester('ntpath.expandvars("%foo% bar")', "bar bar") 5967db96d56Sopenharmony_ci tester('ntpath.expandvars("%foo%bar")', "barbar") 5977db96d56Sopenharmony_ci tester('ntpath.expandvars("%foo%%foo%")', "barbar") 5987db96d56Sopenharmony_ci tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar") 5997db96d56Sopenharmony_ci tester('ntpath.expandvars("%?bar%")', "%?bar%") 6007db96d56Sopenharmony_ci tester('ntpath.expandvars("%foo%%bar")', "bar%bar") 6017db96d56Sopenharmony_ci tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") 6027db96d56Sopenharmony_ci tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%") 6037db96d56Sopenharmony_ci 6047db96d56Sopenharmony_ci @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII') 6057db96d56Sopenharmony_ci def test_expandvars_nonascii(self): 6067db96d56Sopenharmony_ci def check(value, expected): 6077db96d56Sopenharmony_ci tester('ntpath.expandvars(%r)' % value, expected) 6087db96d56Sopenharmony_ci with os_helper.EnvironmentVarGuard() as env: 6097db96d56Sopenharmony_ci env.clear() 6107db96d56Sopenharmony_ci nonascii = os_helper.FS_NONASCII 6117db96d56Sopenharmony_ci env['spam'] = nonascii 6127db96d56Sopenharmony_ci env[nonascii] = 'ham' + nonascii 6137db96d56Sopenharmony_ci check('$spam bar', '%s bar' % nonascii) 6147db96d56Sopenharmony_ci check('$%s bar' % nonascii, '$%s bar' % nonascii) 6157db96d56Sopenharmony_ci check('${spam}bar', '%sbar' % nonascii) 6167db96d56Sopenharmony_ci check('${%s}bar' % nonascii, 'ham%sbar' % nonascii) 6177db96d56Sopenharmony_ci check('$spam}bar', '%s}bar' % nonascii) 6187db96d56Sopenharmony_ci check('$%s}bar' % nonascii, '$%s}bar' % nonascii) 6197db96d56Sopenharmony_ci check('%spam% bar', '%s bar' % nonascii) 6207db96d56Sopenharmony_ci check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii) 6217db96d56Sopenharmony_ci check('%spam%bar', '%sbar' % nonascii) 6227db96d56Sopenharmony_ci check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii) 6237db96d56Sopenharmony_ci 6247db96d56Sopenharmony_ci def test_expanduser(self): 6257db96d56Sopenharmony_ci tester('ntpath.expanduser("test")', 'test') 6267db96d56Sopenharmony_ci 6277db96d56Sopenharmony_ci with os_helper.EnvironmentVarGuard() as env: 6287db96d56Sopenharmony_ci env.clear() 6297db96d56Sopenharmony_ci tester('ntpath.expanduser("~test")', '~test') 6307db96d56Sopenharmony_ci 6317db96d56Sopenharmony_ci env['HOMEDRIVE'] = 'C:\\' 6327db96d56Sopenharmony_ci env['HOMEPATH'] = 'Users\\eric' 6337db96d56Sopenharmony_ci env['USERNAME'] = 'eric' 6347db96d56Sopenharmony_ci tester('ntpath.expanduser("~test")', 'C:\\Users\\test') 6357db96d56Sopenharmony_ci tester('ntpath.expanduser("~")', 'C:\\Users\\eric') 6367db96d56Sopenharmony_ci 6377db96d56Sopenharmony_ci del env['HOMEDRIVE'] 6387db96d56Sopenharmony_ci tester('ntpath.expanduser("~test")', 'Users\\test') 6397db96d56Sopenharmony_ci tester('ntpath.expanduser("~")', 'Users\\eric') 6407db96d56Sopenharmony_ci 6417db96d56Sopenharmony_ci env.clear() 6427db96d56Sopenharmony_ci env['USERPROFILE'] = 'C:\\Users\\eric' 6437db96d56Sopenharmony_ci env['USERNAME'] = 'eric' 6447db96d56Sopenharmony_ci tester('ntpath.expanduser("~test")', 'C:\\Users\\test') 6457db96d56Sopenharmony_ci tester('ntpath.expanduser("~")', 'C:\\Users\\eric') 6467db96d56Sopenharmony_ci tester('ntpath.expanduser("~test\\foo\\bar")', 6477db96d56Sopenharmony_ci 'C:\\Users\\test\\foo\\bar') 6487db96d56Sopenharmony_ci tester('ntpath.expanduser("~test/foo/bar")', 6497db96d56Sopenharmony_ci 'C:\\Users\\test/foo/bar') 6507db96d56Sopenharmony_ci tester('ntpath.expanduser("~\\foo\\bar")', 6517db96d56Sopenharmony_ci 'C:\\Users\\eric\\foo\\bar') 6527db96d56Sopenharmony_ci tester('ntpath.expanduser("~/foo/bar")', 6537db96d56Sopenharmony_ci 'C:\\Users\\eric/foo/bar') 6547db96d56Sopenharmony_ci 6557db96d56Sopenharmony_ci # bpo-36264: ignore `HOME` when set on windows 6567db96d56Sopenharmony_ci env.clear() 6577db96d56Sopenharmony_ci env['HOME'] = 'F:\\' 6587db96d56Sopenharmony_ci env['USERPROFILE'] = 'C:\\Users\\eric' 6597db96d56Sopenharmony_ci env['USERNAME'] = 'eric' 6607db96d56Sopenharmony_ci tester('ntpath.expanduser("~test")', 'C:\\Users\\test') 6617db96d56Sopenharmony_ci tester('ntpath.expanduser("~")', 'C:\\Users\\eric') 6627db96d56Sopenharmony_ci 6637db96d56Sopenharmony_ci # bpo-39899: don't guess another user's home directory if 6647db96d56Sopenharmony_ci # `%USERNAME% != basename(%USERPROFILE%)` 6657db96d56Sopenharmony_ci env.clear() 6667db96d56Sopenharmony_ci env['USERPROFILE'] = 'C:\\Users\\eric' 6677db96d56Sopenharmony_ci env['USERNAME'] = 'idle' 6687db96d56Sopenharmony_ci tester('ntpath.expanduser("~test")', '~test') 6697db96d56Sopenharmony_ci tester('ntpath.expanduser("~")', 'C:\\Users\\eric') 6707db96d56Sopenharmony_ci 6717db96d56Sopenharmony_ci 6727db96d56Sopenharmony_ci 6737db96d56Sopenharmony_ci @unittest.skipUnless(nt, "abspath requires 'nt' module") 6747db96d56Sopenharmony_ci def test_abspath(self): 6757db96d56Sopenharmony_ci tester('ntpath.abspath("C:\\")', "C:\\") 6767db96d56Sopenharmony_ci tester('ntpath.abspath("\\\\?\\C:////spam////eggs. . .")', "\\\\?\\C:\\spam\\eggs") 6777db96d56Sopenharmony_ci tester('ntpath.abspath("\\\\.\\C:////spam////eggs. . .")', "\\\\.\\C:\\spam\\eggs") 6787db96d56Sopenharmony_ci tester('ntpath.abspath("//spam//eggs. . .")', "\\\\spam\\eggs") 6797db96d56Sopenharmony_ci tester('ntpath.abspath("\\\\spam\\\\eggs. . .")', "\\\\spam\\eggs") 6807db96d56Sopenharmony_ci tester('ntpath.abspath("C:/spam. . .")', "C:\\spam") 6817db96d56Sopenharmony_ci tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam") 6827db96d56Sopenharmony_ci tester('ntpath.abspath("C:/nul")', "\\\\.\\nul") 6837db96d56Sopenharmony_ci tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul") 6847db96d56Sopenharmony_ci tester('ntpath.abspath("//..")', "\\\\") 6857db96d56Sopenharmony_ci tester('ntpath.abspath("//../")', "\\\\..\\") 6867db96d56Sopenharmony_ci tester('ntpath.abspath("//../..")', "\\\\..\\") 6877db96d56Sopenharmony_ci tester('ntpath.abspath("//../../")', "\\\\..\\..\\") 6887db96d56Sopenharmony_ci tester('ntpath.abspath("//../../../")', "\\\\..\\..\\") 6897db96d56Sopenharmony_ci tester('ntpath.abspath("//../../../..")', "\\\\..\\..\\") 6907db96d56Sopenharmony_ci tester('ntpath.abspath("//../../../../")', "\\\\..\\..\\") 6917db96d56Sopenharmony_ci tester('ntpath.abspath("//server")', "\\\\server") 6927db96d56Sopenharmony_ci tester('ntpath.abspath("//server/")', "\\\\server\\") 6937db96d56Sopenharmony_ci tester('ntpath.abspath("//server/..")', "\\\\server\\") 6947db96d56Sopenharmony_ci tester('ntpath.abspath("//server/../")', "\\\\server\\..\\") 6957db96d56Sopenharmony_ci tester('ntpath.abspath("//server/../..")', "\\\\server\\..\\") 6967db96d56Sopenharmony_ci tester('ntpath.abspath("//server/../../")', "\\\\server\\..\\") 6977db96d56Sopenharmony_ci tester('ntpath.abspath("//server/../../..")', "\\\\server\\..\\") 6987db96d56Sopenharmony_ci tester('ntpath.abspath("//server/../../../")', "\\\\server\\..\\") 6997db96d56Sopenharmony_ci tester('ntpath.abspath("//server/share")', "\\\\server\\share") 7007db96d56Sopenharmony_ci tester('ntpath.abspath("//server/share/")', "\\\\server\\share\\") 7017db96d56Sopenharmony_ci tester('ntpath.abspath("//server/share/..")', "\\\\server\\share\\") 7027db96d56Sopenharmony_ci tester('ntpath.abspath("//server/share/../")', "\\\\server\\share\\") 7037db96d56Sopenharmony_ci tester('ntpath.abspath("//server/share/../..")', "\\\\server\\share\\") 7047db96d56Sopenharmony_ci tester('ntpath.abspath("//server/share/../../")', "\\\\server\\share\\") 7057db96d56Sopenharmony_ci tester('ntpath.abspath("C:\\nul. . .")', "\\\\.\\nul") 7067db96d56Sopenharmony_ci tester('ntpath.abspath("//... . .")', "\\\\") 7077db96d56Sopenharmony_ci tester('ntpath.abspath("//.. . . .")', "\\\\") 7087db96d56Sopenharmony_ci tester('ntpath.abspath("//../... . .")', "\\\\..\\") 7097db96d56Sopenharmony_ci tester('ntpath.abspath("//../.. . . .")', "\\\\..\\") 7107db96d56Sopenharmony_ci with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir: # bpo-31047 7117db96d56Sopenharmony_ci tester('ntpath.abspath("")', cwd_dir) 7127db96d56Sopenharmony_ci tester('ntpath.abspath(" ")', cwd_dir + "\\ ") 7137db96d56Sopenharmony_ci tester('ntpath.abspath("?")', cwd_dir + "\\?") 7147db96d56Sopenharmony_ci drive, _ = ntpath.splitdrive(cwd_dir) 7157db96d56Sopenharmony_ci tester('ntpath.abspath("/abc/")', drive + "\\abc") 7167db96d56Sopenharmony_ci 7177db96d56Sopenharmony_ci def test_relpath(self): 7187db96d56Sopenharmony_ci tester('ntpath.relpath("a")', 'a') 7197db96d56Sopenharmony_ci tester('ntpath.relpath(ntpath.abspath("a"))', 'a') 7207db96d56Sopenharmony_ci tester('ntpath.relpath("a/b")', 'a\\b') 7217db96d56Sopenharmony_ci tester('ntpath.relpath("../a/b")', '..\\a\\b') 7227db96d56Sopenharmony_ci with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir: 7237db96d56Sopenharmony_ci currentdir = ntpath.basename(cwd_dir) 7247db96d56Sopenharmony_ci tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') 7257db96d56Sopenharmony_ci tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') 7267db96d56Sopenharmony_ci tester('ntpath.relpath("a", "b/c")', '..\\..\\a') 7277db96d56Sopenharmony_ci tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat') 7287db96d56Sopenharmony_ci tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') 7297db96d56Sopenharmony_ci tester('ntpath.relpath("a", "a")', '.') 7307db96d56Sopenharmony_ci tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat') 7317db96d56Sopenharmony_ci tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') 7327db96d56Sopenharmony_ci tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') 7337db96d56Sopenharmony_ci tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') 7347db96d56Sopenharmony_ci tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') 7357db96d56Sopenharmony_ci tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') 7367db96d56Sopenharmony_ci tester('ntpath.relpath("/", "/")', '.') 7377db96d56Sopenharmony_ci tester('ntpath.relpath("/a", "/a")', '.') 7387db96d56Sopenharmony_ci tester('ntpath.relpath("/a/b", "/a/b")', '.') 7397db96d56Sopenharmony_ci tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') 7407db96d56Sopenharmony_ci 7417db96d56Sopenharmony_ci def test_commonpath(self): 7427db96d56Sopenharmony_ci def check(paths, expected): 7437db96d56Sopenharmony_ci tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'), 7447db96d56Sopenharmony_ci expected) 7457db96d56Sopenharmony_ci def check_error(exc, paths): 7467db96d56Sopenharmony_ci self.assertRaises(exc, ntpath.commonpath, paths) 7477db96d56Sopenharmony_ci self.assertRaises(exc, ntpath.commonpath, 7487db96d56Sopenharmony_ci [os.fsencode(p) for p in paths]) 7497db96d56Sopenharmony_ci 7507db96d56Sopenharmony_ci self.assertRaises(ValueError, ntpath.commonpath, []) 7517db96d56Sopenharmony_ci check_error(ValueError, ['C:\\Program Files', 'Program Files']) 7527db96d56Sopenharmony_ci check_error(ValueError, ['C:\\Program Files', 'C:Program Files']) 7537db96d56Sopenharmony_ci check_error(ValueError, ['\\Program Files', 'Program Files']) 7547db96d56Sopenharmony_ci check_error(ValueError, ['Program Files', 'C:\\Program Files']) 7557db96d56Sopenharmony_ci check(['C:\\Program Files'], 'C:\\Program Files') 7567db96d56Sopenharmony_ci check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files') 7577db96d56Sopenharmony_ci check(['C:\\Program Files\\', 'C:\\Program Files'], 7587db96d56Sopenharmony_ci 'C:\\Program Files') 7597db96d56Sopenharmony_ci check(['C:\\Program Files\\', 'C:\\Program Files\\'], 7607db96d56Sopenharmony_ci 'C:\\Program Files') 7617db96d56Sopenharmony_ci check(['C:\\\\Program Files', 'C:\\Program Files\\\\'], 7627db96d56Sopenharmony_ci 'C:\\Program Files') 7637db96d56Sopenharmony_ci check(['C:\\.\\Program Files', 'C:\\Program Files\\.'], 7647db96d56Sopenharmony_ci 'C:\\Program Files') 7657db96d56Sopenharmony_ci check(['C:\\', 'C:\\bin'], 'C:\\') 7667db96d56Sopenharmony_ci check(['C:\\Program Files', 'C:\\bin'], 'C:\\') 7677db96d56Sopenharmony_ci check(['C:\\Program Files', 'C:\\Program Files\\Bar'], 7687db96d56Sopenharmony_ci 'C:\\Program Files') 7697db96d56Sopenharmony_ci check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'], 7707db96d56Sopenharmony_ci 'C:\\Program Files') 7717db96d56Sopenharmony_ci check(['C:\\Program Files', 'C:\\Projects'], 'C:\\') 7727db96d56Sopenharmony_ci check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\') 7737db96d56Sopenharmony_ci 7747db96d56Sopenharmony_ci check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'], 7757db96d56Sopenharmony_ci 'C:\\Program Files') 7767db96d56Sopenharmony_ci check(['C:\\Program Files\\Foo', 'c:/program files/bar'], 7777db96d56Sopenharmony_ci 'C:\\Program Files') 7787db96d56Sopenharmony_ci check(['c:/program files/bar', 'C:\\Program Files\\Foo'], 7797db96d56Sopenharmony_ci 'c:\\program files') 7807db96d56Sopenharmony_ci 7817db96d56Sopenharmony_ci check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files']) 7827db96d56Sopenharmony_ci 7837db96d56Sopenharmony_ci check(['spam'], 'spam') 7847db96d56Sopenharmony_ci check(['spam', 'spam'], 'spam') 7857db96d56Sopenharmony_ci check(['spam', 'alot'], '') 7867db96d56Sopenharmony_ci check(['and\\jam', 'and\\spam'], 'and') 7877db96d56Sopenharmony_ci check(['and\\\\jam', 'and\\spam\\\\'], 'and') 7887db96d56Sopenharmony_ci check(['and\\.\\jam', '.\\and\\spam'], 'and') 7897db96d56Sopenharmony_ci check(['and\\jam', 'and\\spam', 'alot'], '') 7907db96d56Sopenharmony_ci check(['and\\jam', 'and\\spam', 'and'], 'and') 7917db96d56Sopenharmony_ci check(['C:and\\jam', 'C:and\\spam'], 'C:and') 7927db96d56Sopenharmony_ci 7937db96d56Sopenharmony_ci check([''], '') 7947db96d56Sopenharmony_ci check(['', 'spam\\alot'], '') 7957db96d56Sopenharmony_ci check_error(ValueError, ['', '\\spam\\alot']) 7967db96d56Sopenharmony_ci 7977db96d56Sopenharmony_ci self.assertRaises(TypeError, ntpath.commonpath, 7987db96d56Sopenharmony_ci [b'C:\\Program Files', 'C:\\Program Files\\Foo']) 7997db96d56Sopenharmony_ci self.assertRaises(TypeError, ntpath.commonpath, 8007db96d56Sopenharmony_ci [b'C:\\Program Files', 'Program Files\\Foo']) 8017db96d56Sopenharmony_ci self.assertRaises(TypeError, ntpath.commonpath, 8027db96d56Sopenharmony_ci [b'Program Files', 'C:\\Program Files\\Foo']) 8037db96d56Sopenharmony_ci self.assertRaises(TypeError, ntpath.commonpath, 8047db96d56Sopenharmony_ci ['C:\\Program Files', b'C:\\Program Files\\Foo']) 8057db96d56Sopenharmony_ci self.assertRaises(TypeError, ntpath.commonpath, 8067db96d56Sopenharmony_ci ['C:\\Program Files', b'Program Files\\Foo']) 8077db96d56Sopenharmony_ci self.assertRaises(TypeError, ntpath.commonpath, 8087db96d56Sopenharmony_ci ['Program Files', b'C:\\Program Files\\Foo']) 8097db96d56Sopenharmony_ci 8107db96d56Sopenharmony_ci @unittest.skipIf(is_emscripten, "Emscripten cannot fstat unnamed files.") 8117db96d56Sopenharmony_ci def test_sameopenfile(self): 8127db96d56Sopenharmony_ci with TemporaryFile() as tf1, TemporaryFile() as tf2: 8137db96d56Sopenharmony_ci # Make sure the same file is really the same 8147db96d56Sopenharmony_ci self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno())) 8157db96d56Sopenharmony_ci # Make sure different files are really different 8167db96d56Sopenharmony_ci self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno())) 8177db96d56Sopenharmony_ci # Make sure invalid values don't cause issues on win32 8187db96d56Sopenharmony_ci if sys.platform == "win32": 8197db96d56Sopenharmony_ci with self.assertRaises(OSError): 8207db96d56Sopenharmony_ci # Invalid file descriptors shouldn't display assert 8217db96d56Sopenharmony_ci # dialogs (#4804) 8227db96d56Sopenharmony_ci ntpath.sameopenfile(-1, -1) 8237db96d56Sopenharmony_ci 8247db96d56Sopenharmony_ci def test_ismount(self): 8257db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("c:\\")) 8267db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("C:\\")) 8277db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("c:/")) 8287db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("C:/")) 8297db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("\\\\.\\c:\\")) 8307db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("\\\\.\\C:\\")) 8317db96d56Sopenharmony_ci 8327db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"c:\\")) 8337db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"C:\\")) 8347db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"c:/")) 8357db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"C:/")) 8367db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\")) 8377db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\")) 8387db96d56Sopenharmony_ci 8397db96d56Sopenharmony_ci with os_helper.temp_dir() as d: 8407db96d56Sopenharmony_ci self.assertFalse(ntpath.ismount(d)) 8417db96d56Sopenharmony_ci 8427db96d56Sopenharmony_ci if sys.platform == "win32": 8437db96d56Sopenharmony_ci # 8447db96d56Sopenharmony_ci # Make sure the current folder isn't the root folder 8457db96d56Sopenharmony_ci # (or any other volume root). The drive-relative 8467db96d56Sopenharmony_ci # locations below cannot then refer to mount points 8477db96d56Sopenharmony_ci # 8487db96d56Sopenharmony_ci test_cwd = os.getenv("SystemRoot") 8497db96d56Sopenharmony_ci drive, path = ntpath.splitdrive(test_cwd) 8507db96d56Sopenharmony_ci with os_helper.change_cwd(test_cwd): 8517db96d56Sopenharmony_ci self.assertFalse(ntpath.ismount(drive.lower())) 8527db96d56Sopenharmony_ci self.assertFalse(ntpath.ismount(drive.upper())) 8537db96d56Sopenharmony_ci 8547db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("\\\\localhost\\c$")) 8557db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\")) 8567db96d56Sopenharmony_ci 8577db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$")) 8587db96d56Sopenharmony_ci self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\")) 8597db96d56Sopenharmony_ci 8607db96d56Sopenharmony_ci def assertEqualCI(self, s1, s2): 8617db96d56Sopenharmony_ci """Assert that two strings are equal ignoring case differences.""" 8627db96d56Sopenharmony_ci self.assertEqual(s1.lower(), s2.lower()) 8637db96d56Sopenharmony_ci 8647db96d56Sopenharmony_ci @unittest.skipUnless(nt, "OS helpers require 'nt' module") 8657db96d56Sopenharmony_ci def test_nt_helpers(self): 8667db96d56Sopenharmony_ci # Trivial validation that the helpers do not break, and support both 8677db96d56Sopenharmony_ci # unicode and bytes (UTF-8) paths 8687db96d56Sopenharmony_ci 8697db96d56Sopenharmony_ci executable = nt._getfinalpathname(sys.executable) 8707db96d56Sopenharmony_ci 8717db96d56Sopenharmony_ci for path in executable, os.fsencode(executable): 8727db96d56Sopenharmony_ci volume_path = nt._getvolumepathname(path) 8737db96d56Sopenharmony_ci path_drive = ntpath.splitdrive(path)[0] 8747db96d56Sopenharmony_ci volume_path_drive = ntpath.splitdrive(volume_path)[0] 8757db96d56Sopenharmony_ci self.assertEqualCI(path_drive, volume_path_drive) 8767db96d56Sopenharmony_ci 8777db96d56Sopenharmony_ci cap, free = nt._getdiskusage(sys.exec_prefix) 8787db96d56Sopenharmony_ci self.assertGreater(cap, 0) 8797db96d56Sopenharmony_ci self.assertGreater(free, 0) 8807db96d56Sopenharmony_ci b_cap, b_free = nt._getdiskusage(sys.exec_prefix.encode()) 8817db96d56Sopenharmony_ci # Free space may change, so only test the capacity is equal 8827db96d56Sopenharmony_ci self.assertEqual(b_cap, cap) 8837db96d56Sopenharmony_ci self.assertGreater(b_free, 0) 8847db96d56Sopenharmony_ci 8857db96d56Sopenharmony_ci for path in [sys.prefix, sys.executable]: 8867db96d56Sopenharmony_ci final_path = nt._getfinalpathname(path) 8877db96d56Sopenharmony_ci self.assertIsInstance(final_path, str) 8887db96d56Sopenharmony_ci self.assertGreater(len(final_path), 0) 8897db96d56Sopenharmony_ci 8907db96d56Sopenharmony_ci b_final_path = nt._getfinalpathname(path.encode()) 8917db96d56Sopenharmony_ci self.assertIsInstance(b_final_path, bytes) 8927db96d56Sopenharmony_ci self.assertGreater(len(b_final_path), 0) 8937db96d56Sopenharmony_ci 8947db96d56Sopenharmony_ciclass NtCommonTest(test_genericpath.CommonTest, unittest.TestCase): 8957db96d56Sopenharmony_ci pathmodule = ntpath 8967db96d56Sopenharmony_ci attributes = ['relpath'] 8977db96d56Sopenharmony_ci 8987db96d56Sopenharmony_ci 8997db96d56Sopenharmony_ciclass PathLikeTests(NtpathTestCase): 9007db96d56Sopenharmony_ci 9017db96d56Sopenharmony_ci path = ntpath 9027db96d56Sopenharmony_ci 9037db96d56Sopenharmony_ci def setUp(self): 9047db96d56Sopenharmony_ci self.file_name = os_helper.TESTFN 9057db96d56Sopenharmony_ci self.file_path = FakePath(os_helper.TESTFN) 9067db96d56Sopenharmony_ci self.addCleanup(os_helper.unlink, self.file_name) 9077db96d56Sopenharmony_ci with open(self.file_name, 'xb', 0) as file: 9087db96d56Sopenharmony_ci file.write(b"test_ntpath.PathLikeTests") 9097db96d56Sopenharmony_ci 9107db96d56Sopenharmony_ci def _check_function(self, func): 9117db96d56Sopenharmony_ci self.assertPathEqual(func(self.file_path), func(self.file_name)) 9127db96d56Sopenharmony_ci 9137db96d56Sopenharmony_ci def test_path_normcase(self): 9147db96d56Sopenharmony_ci self._check_function(self.path.normcase) 9157db96d56Sopenharmony_ci if sys.platform == 'win32': 9167db96d56Sopenharmony_ci self.assertEqual(ntpath.normcase('\u03a9\u2126'), 'ωΩ') 9177db96d56Sopenharmony_ci 9187db96d56Sopenharmony_ci def test_path_isabs(self): 9197db96d56Sopenharmony_ci self._check_function(self.path.isabs) 9207db96d56Sopenharmony_ci 9217db96d56Sopenharmony_ci def test_path_join(self): 9227db96d56Sopenharmony_ci self.assertEqual(self.path.join('a', FakePath('b'), 'c'), 9237db96d56Sopenharmony_ci self.path.join('a', 'b', 'c')) 9247db96d56Sopenharmony_ci 9257db96d56Sopenharmony_ci def test_path_split(self): 9267db96d56Sopenharmony_ci self._check_function(self.path.split) 9277db96d56Sopenharmony_ci 9287db96d56Sopenharmony_ci def test_path_splitext(self): 9297db96d56Sopenharmony_ci self._check_function(self.path.splitext) 9307db96d56Sopenharmony_ci 9317db96d56Sopenharmony_ci def test_path_splitdrive(self): 9327db96d56Sopenharmony_ci self._check_function(self.path.splitdrive) 9337db96d56Sopenharmony_ci 9347db96d56Sopenharmony_ci def test_path_basename(self): 9357db96d56Sopenharmony_ci self._check_function(self.path.basename) 9367db96d56Sopenharmony_ci 9377db96d56Sopenharmony_ci def test_path_dirname(self): 9387db96d56Sopenharmony_ci self._check_function(self.path.dirname) 9397db96d56Sopenharmony_ci 9407db96d56Sopenharmony_ci def test_path_islink(self): 9417db96d56Sopenharmony_ci self._check_function(self.path.islink) 9427db96d56Sopenharmony_ci 9437db96d56Sopenharmony_ci def test_path_lexists(self): 9447db96d56Sopenharmony_ci self._check_function(self.path.lexists) 9457db96d56Sopenharmony_ci 9467db96d56Sopenharmony_ci def test_path_ismount(self): 9477db96d56Sopenharmony_ci self._check_function(self.path.ismount) 9487db96d56Sopenharmony_ci 9497db96d56Sopenharmony_ci def test_path_expanduser(self): 9507db96d56Sopenharmony_ci self._check_function(self.path.expanduser) 9517db96d56Sopenharmony_ci 9527db96d56Sopenharmony_ci def test_path_expandvars(self): 9537db96d56Sopenharmony_ci self._check_function(self.path.expandvars) 9547db96d56Sopenharmony_ci 9557db96d56Sopenharmony_ci def test_path_normpath(self): 9567db96d56Sopenharmony_ci self._check_function(self.path.normpath) 9577db96d56Sopenharmony_ci 9587db96d56Sopenharmony_ci def test_path_abspath(self): 9597db96d56Sopenharmony_ci self._check_function(self.path.abspath) 9607db96d56Sopenharmony_ci 9617db96d56Sopenharmony_ci def test_path_realpath(self): 9627db96d56Sopenharmony_ci self._check_function(self.path.realpath) 9637db96d56Sopenharmony_ci 9647db96d56Sopenharmony_ci def test_path_relpath(self): 9657db96d56Sopenharmony_ci self._check_function(self.path.relpath) 9667db96d56Sopenharmony_ci 9677db96d56Sopenharmony_ci def test_path_commonpath(self): 9687db96d56Sopenharmony_ci common_path = self.path.commonpath([self.file_path, self.file_name]) 9697db96d56Sopenharmony_ci self.assertPathEqual(common_path, self.file_name) 9707db96d56Sopenharmony_ci 9717db96d56Sopenharmony_ci def test_path_isdir(self): 9727db96d56Sopenharmony_ci self._check_function(self.path.isdir) 9737db96d56Sopenharmony_ci 9747db96d56Sopenharmony_ci 9757db96d56Sopenharmony_ciif __name__ == "__main__": 9767db96d56Sopenharmony_ci unittest.main() 977