diff options
Diffstat (limited to 'Lib/test/test_ntpath.py')
| -rw-r--r-- | Lib/test/test_ntpath.py | 98 |
1 files changed, 81 insertions, 17 deletions
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index e1852c0..6464950 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -1,7 +1,9 @@ import ntpath import os -from test.test_support import TestFailed -from test import test_support, test_genericpath +import sys +from test.support import TestFailed +from test import support, test_genericpath +from tempfile import TemporaryFile import unittest @@ -9,8 +11,26 @@ def tester(fn, wantResult): fn = fn.replace("\\", "\\\\") gotResult = eval(fn) if wantResult != gotResult: - raise TestFailed, "%s should return: %s but returned: %s" \ - %(str(fn), str(wantResult), str(gotResult)) + raise TestFailed("%s should return: %s but returned: %s" \ + %(str(fn), str(wantResult), str(gotResult))) + + # then with bytes + fn = fn.replace("('", "(b'") + fn = fn.replace('("', '(b"') + fn = fn.replace("['", "[b'") + fn = fn.replace('["', '[b"') + fn = fn.replace(", '", ", b'") + fn = fn.replace(', "', ', b"') + gotResult = eval(fn) + if isinstance(wantResult, str): + wantResult = wantResult.encode('ascii') + elif isinstance(wantResult, tuple): + wantResult = tuple(r.encode('ascii') for r in wantResult) + + gotResult = eval(fn) + if wantResult != gotResult: + raise TestFailed("%s should return: %s but returned: %s" \ + %(str(fn), str(wantResult), repr(gotResult))) class TestNtpath(unittest.TestCase): @@ -31,12 +51,18 @@ class TestNtpath(unittest.TestCase): ('c:', '\\foo\\bar')) tester('ntpath.splitdrive("c:/foo/bar")', ('c:', '/foo/bar')) - - def test_splitunc(self): - tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', + tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")', ('\\\\conky\\mountpoint', '\\foo\\bar')) - tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', + tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")', ('//conky/mountpoint', '/foo/bar')) + tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")', + ('', '\\\\\\conky\\mountpoint\\foo\\bar')) + tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")', + ('', '///conky/mountpoint/foo/bar')) + tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")', + ('', '\\\\conky\\\\mountpoint\\foo\\bar')) + tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")', + ('', '//conky//mountpoint/foo/bar')) def test_split(self): tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) @@ -45,10 +71,10 @@ class TestNtpath(unittest.TestCase): tester('ntpath.split("c:\\")', ('c:\\', '')) tester('ntpath.split("\\\\conky\\mountpoint\\")', - ('\\\\conky\\mountpoint', '')) + ('\\\\conky\\mountpoint\\', '')) tester('ntpath.split("c:/")', ('c:/', '')) - tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', '')) + tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', '')) def test_isabs(self): tester('ntpath.isabs("c:\\")', 1) @@ -99,6 +125,33 @@ class TestNtpath(unittest.TestCase): tester("ntpath.join('a\\', '')", 'a\\') tester("ntpath.join('a\\', '', '', '', '')", 'a\\') + # from comment in ntpath.join + tester("ntpath.join('c:', '/a')", 'c:/a') + tester("ntpath.join('//computer/share', '/a')", '//computer/share/a') + tester("ntpath.join('c:/', '/a')", 'c:/a') + tester("ntpath.join('//computer/share/', '/a')", '//computer/share/a') + tester("ntpath.join('c:/a', '/b')", '/b') + tester("ntpath.join('//computer/share/a', '/b')", '/b') + tester("ntpath.join('c:', 'd:/')", 'd:/') + tester("ntpath.join('c:', '//computer/share/')", '//computer/share/') + tester("ntpath.join('//computer/share', 'd:/')", 'd:/') + tester("ntpath.join('//computer/share', '//computer/share/')", '//computer/share/') + tester("ntpath.join('c:/', 'd:/')", 'd:/') + tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/') + tester("ntpath.join('//computer/share/', 'd:/')", 'd:/') + tester("ntpath.join('//computer/share/', '//computer/share/')", '//computer/share/') + + tester("ntpath.join('c:', '//computer/share/')", '//computer/share/') + tester("ntpath.join('c:/', '//computer/share/')", '//computer/share/') + tester("ntpath.join('c:/', '//computer/share/a/b')", '//computer/share/a/b') + + tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b') + tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b') + tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b') + tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b') + tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b') + tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b') + def test_normpath(self): tester("ntpath.normpath('A//////././//.//B')", r'A\B') tester("ntpath.normpath('A/./B')", r'A\B') @@ -127,7 +180,7 @@ class TestNtpath(unittest.TestCase): tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z') def test_expandvars(self): - with test_support.EnvironmentVarGuard() as env: + with support.EnvironmentVarGuard() as env: env.clear() env["foo"] = "bar" env["{foo"] = "baz1" @@ -158,14 +211,11 @@ class TestNtpath(unittest.TestCase): # the rest of the tests for the ntpath module to be run to completion # on any platform, since most of the module is intended to be usable # from any platform. - # XXX this needs more tests try: import nt - except ImportError: - # check that the function is there even if we are not on Windows - ntpath.abspath - else: tester('ntpath.abspath("C:\\")', "C:\\") + except ImportError: + pass def test_relpath(self): currentdir = os.path.split(os.getcwd())[-1] @@ -176,6 +226,7 @@ class TestNtpath(unittest.TestCase): tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a') tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b') tester('ntpath.relpath("a", "b/c")', '..\\..\\a') + tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat') tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') tester('ntpath.relpath("a", "a")', '.') tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat') @@ -189,6 +240,19 @@ class TestNtpath(unittest.TestCase): tester('ntpath.relpath("/a/b", "/a/b")', '.') tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') + def test_sameopenfile(self): + with TemporaryFile() as tf1, TemporaryFile() as tf2: + # Make sure the same file is really the same + self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno())) + # Make sure different files are really different + self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno())) + # Make sure invalid values don't cause issues on win32 + if sys.platform == "win32": + with self.assertRaises(OSError): + # Invalid file descriptors shouldn't display assert + # dialogs (#4804) + ntpath.sameopenfile(-1, -1) + class NtCommonTest(test_genericpath.CommonTest): pathmodule = ntpath @@ -196,7 +260,7 @@ class NtCommonTest(test_genericpath.CommonTest): def test_main(): - test_support.run_unittest(TestNtpath, NtCommonTest) + support.run_unittest(TestNtpath, NtCommonTest) if __name__ == "__main__": |
