mirror of
https://github.com/python/cpython.git
synced 2025-08-17 15:21:26 +00:00

svn+ssh://pythondev@svn.python.org/python/branches/release27-maint ................ r83429 | georg.brandl | 2010-08-01 21:14:56 +0200 (So, 01 Aug 2010) | 37 lines Merged revisions 83352,83356-83358,83362,83366,83368-83369 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83352 | georg.brandl | 2010-07-31 20:11:07 +0200 (Sa, 31 Jul 2010) | 1 line #9440: Remove borderline test case that fails based on unpredictable conditions such as compiler flags. ........ r83356 | georg.brandl | 2010-07-31 21:29:15 +0200 (Sa, 31 Jul 2010) | 1 line Remove trailing whitespace. ........ r83357 | georg.brandl | 2010-07-31 21:59:55 +0200 (Sa, 31 Jul 2010) | 1 line #5778: document that sys.version can contain a newline. ........ r83358 | georg.brandl | 2010-07-31 22:05:31 +0200 (Sa, 31 Jul 2010) | 1 line #9442: do not document a specific format for sys.version; rather refer to version_info and the platform module. ........ r83362 | georg.brandl | 2010-07-31 23:12:15 +0200 (Sa, 31 Jul 2010) | 1 line #8910: add a file explaining why Lib/test/data is there. ........ r83366 | georg.brandl | 2010-07-31 23:26:40 +0200 (Sa, 31 Jul 2010) | 1 line There always is a False and True now. ........ r83368 | georg.brandl | 2010-07-31 23:40:15 +0200 (Sa, 31 Jul 2010) | 1 line #7909: the prefixes \\.\ and \\?\ indicate special Windows paths, do not try to manipulate them. See http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx for details. ........ r83369 | georg.brandl | 2010-07-31 23:41:42 +0200 (Sa, 31 Jul 2010) | 1 line Fix "Berkeley" name. ........ ................ r83436 | georg.brandl | 2010-08-01 21:33:15 +0200 (So, 01 Aug 2010) | 42 lines Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83259 | georg.brandl | 2010-07-30 09:03:39 +0200 (Fr, 30 Jul 2010) | 1 line Clarification. ........ r83261 | georg.brandl | 2010-07-30 09:21:26 +0200 (Fr, 30 Jul 2010) | 1 line #9230: allow Pdb.checkline() to be called without a current frame, for setting breakpoints before starting debugging. ........ r83264 | georg.brandl | 2010-07-30 10:45:26 +0200 (Fr, 30 Jul 2010) | 1 line Document the "jump" command in pdb.__doc__, and add a version tag for "until X". ........ r83265 | georg.brandl | 2010-07-30 10:54:49 +0200 (Fr, 30 Jul 2010) | 1 line #8015: fix crash when entering an empty line for breakpoint commands. Also restore environment properly when an exception occurs during the definition of commands. ........ r83268 | georg.brandl | 2010-07-30 11:23:23 +0200 (Fr, 30 Jul 2010) | 2 lines Issue #8048: Prevent doctests from failing when sys.displayhook has been reassigned. ........ r83269 | georg.brandl | 2010-07-30 11:43:00 +0200 (Fr, 30 Jul 2010) | 1 line #6719: In pdb, do not stop somewhere in the encodings machinery if the source file to be debugged is in a non-builtin encoding. ........ r83271 | georg.brandl | 2010-07-30 11:59:28 +0200 (Fr, 30 Jul 2010) | 1 line #5727: Restore the ability to use readline when calling into pdb in doctests. ........ r83272 | georg.brandl | 2010-07-30 12:29:19 +0200 (Fr, 30 Jul 2010) | 1 line #5294: Fix the behavior of pdb "continue" command when called in the top-level debugged frame. ........ r83281 | georg.brandl | 2010-07-30 15:36:43 +0200 (Fr, 30 Jul 2010) | 1 line Add myself for pdb. ........ ................
214 lines
9.4 KiB
Python
214 lines
9.4 KiB
Python
import ntpath
|
|
import os
|
|
from test.test_support import verbose, TestFailed
|
|
import test.test_support as test_support
|
|
import unittest
|
|
|
|
|
|
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))
|
|
|
|
|
|
class TestNtpath(unittest.TestCase):
|
|
def test_splitext(self):
|
|
tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
|
|
tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
|
|
tester('ntpath.splitext(".ext")', ('.ext', ''))
|
|
tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
|
|
tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
|
|
tester('ntpath.splitext("")', ('', ''))
|
|
tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
|
|
tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
|
|
tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
|
|
tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
|
|
|
|
def test_splitdrive(self):
|
|
tester('ntpath.splitdrive("c:\\foo\\bar")',
|
|
('c:', '\\foo\\bar'))
|
|
tester('ntpath.splitdrive("c:/foo/bar")',
|
|
('c:', '/foo/bar'))
|
|
|
|
def test_splitunc(self):
|
|
tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
|
|
('\\\\conky\\mountpoint', '\\foo\\bar'))
|
|
tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
|
|
('//conky/mountpoint', '/foo/bar'))
|
|
|
|
def test_split(self):
|
|
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
|
|
tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
|
|
('\\\\conky\\mountpoint\\foo', 'bar'))
|
|
|
|
tester('ntpath.split("c:\\")', ('c:\\', ''))
|
|
tester('ntpath.split("\\\\conky\\mountpoint\\")',
|
|
('\\\\conky\\mountpoint', ''))
|
|
|
|
tester('ntpath.split("c:/")', ('c:/', ''))
|
|
tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
|
|
|
|
def test_isabs(self):
|
|
tester('ntpath.isabs("c:\\")', 1)
|
|
tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
|
|
tester('ntpath.isabs("\\foo")', 1)
|
|
tester('ntpath.isabs("\\foo\\bar")', 1)
|
|
|
|
def test_commonprefix(self):
|
|
tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
|
|
"/home/swen")
|
|
tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
|
|
"\\home\\swen\\")
|
|
tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
|
|
"/home/swen/spam")
|
|
|
|
def test_join(self):
|
|
tester('ntpath.join("")', '')
|
|
tester('ntpath.join("", "", "")', '')
|
|
tester('ntpath.join("a")', 'a')
|
|
tester('ntpath.join("/a")', '/a')
|
|
tester('ntpath.join("\\a")', '\\a')
|
|
tester('ntpath.join("a:")', 'a:')
|
|
tester('ntpath.join("a:", "b")', 'a:b')
|
|
tester('ntpath.join("a:", "/b")', 'a:/b')
|
|
tester('ntpath.join("a:", "\\b")', 'a:\\b')
|
|
tester('ntpath.join("a", "/b")', '/b')
|
|
tester('ntpath.join("a", "\\b")', '\\b')
|
|
tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
|
|
tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
|
|
tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
|
|
tester('ntpath.join("a", "b", "\\c")', '\\c')
|
|
tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
|
|
tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
|
|
tester("ntpath.join('c:', '/a')", 'c:/a')
|
|
tester("ntpath.join('c:/', '/a')", 'c:/a')
|
|
tester("ntpath.join('c:/a', '/b')", '/b')
|
|
tester("ntpath.join('c:', 'd:/')", 'd:/')
|
|
tester("ntpath.join('c:/', 'd:/')", 'd:/')
|
|
tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
|
|
|
|
tester("ntpath.join('')", '')
|
|
tester("ntpath.join('', '', '', '', '')", '')
|
|
tester("ntpath.join('a')", 'a')
|
|
tester("ntpath.join('', 'a')", 'a')
|
|
tester("ntpath.join('', '', '', '', 'a')", 'a')
|
|
tester("ntpath.join('a', '')", 'a\\')
|
|
tester("ntpath.join('a', '', '', '', '')", 'a\\')
|
|
tester("ntpath.join('a\\', '')", 'a\\')
|
|
tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
|
|
|
|
def test_normpath(self):
|
|
tester("ntpath.normpath('A//////././//.//B')", r'A\B')
|
|
tester("ntpath.normpath('A/./B')", r'A\B')
|
|
tester("ntpath.normpath('A/foo/../B')", r'A\B')
|
|
tester("ntpath.normpath('C:A//B')", r'C:A\B')
|
|
tester("ntpath.normpath('D:A/./B')", r'D:A\B')
|
|
tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
|
|
|
|
tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
|
|
tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
|
|
tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
|
|
|
|
tester("ntpath.normpath('..')", r'..')
|
|
tester("ntpath.normpath('.')", r'.')
|
|
tester("ntpath.normpath('')", r'.')
|
|
tester("ntpath.normpath('/')", '\\')
|
|
tester("ntpath.normpath('c:/')", 'c:\\')
|
|
tester("ntpath.normpath('/../.././..')", '\\')
|
|
tester("ntpath.normpath('c:/../../..')", 'c:\\')
|
|
tester("ntpath.normpath('../.././..')", r'..\..\..')
|
|
tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
|
|
tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
|
|
tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
|
|
|
|
# Issue 5827: Make sure normpath preserves unicode
|
|
for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
|
|
self.assertTrue(isinstance(ntpath.normpath(path), unicode),
|
|
'normpath() returned str instead of unicode')
|
|
|
|
tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
|
|
tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
|
|
|
|
def test_expandvars(self):
|
|
oldenv = os.environ.copy()
|
|
try:
|
|
os.environ.clear()
|
|
os.environ["foo"] = "bar"
|
|
os.environ["{foo"] = "baz1"
|
|
os.environ["{foo}"] = "baz2"
|
|
tester('ntpath.expandvars("foo")', "foo")
|
|
tester('ntpath.expandvars("$foo bar")', "bar bar")
|
|
tester('ntpath.expandvars("${foo}bar")', "barbar")
|
|
tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
|
|
tester('ntpath.expandvars("$bar bar")', "$bar bar")
|
|
tester('ntpath.expandvars("$?bar")', "$?bar")
|
|
tester('ntpath.expandvars("${foo}bar")', "barbar")
|
|
tester('ntpath.expandvars("$foo}bar")', "bar}bar")
|
|
tester('ntpath.expandvars("${foo")', "${foo")
|
|
tester('ntpath.expandvars("${{foo}}")', "baz1}")
|
|
tester('ntpath.expandvars("$foo$foo")', "barbar")
|
|
tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
|
|
tester('ntpath.expandvars("%foo% bar")', "bar bar")
|
|
tester('ntpath.expandvars("%foo%bar")', "barbar")
|
|
tester('ntpath.expandvars("%foo%%foo%")', "barbar")
|
|
tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
|
|
tester('ntpath.expandvars("%?bar%")', "%?bar%")
|
|
tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
|
|
tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
|
|
finally:
|
|
os.environ.clear()
|
|
os.environ.update(oldenv)
|
|
|
|
def test_abspath(self):
|
|
# ntpath.abspath() can only be used on a system with the "nt" module
|
|
# (reasonably), so we protect this test with "import nt". This allows
|
|
# 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:\\")
|
|
|
|
# Issue 3426: check that abspath retuns unicode when the arg is
|
|
# unicode and str when it's str, with both ASCII and non-ASCII cwds
|
|
saved_cwd = os.getcwd()
|
|
for cwd in (u'cwd', u'\xe7w\xf0'):
|
|
try:
|
|
os.mkdir(cwd)
|
|
os.chdir(cwd)
|
|
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
|
|
self.assertTrue(isinstance(ntpath.abspath(path), str))
|
|
for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
|
|
self.assertTrue(isinstance(ntpath.abspath(upath),
|
|
unicode))
|
|
finally:
|
|
os.chdir(saved_cwd)
|
|
os.rmdir(cwd)
|
|
|
|
|
|
def test_relpath(self):
|
|
currentdir = os.path.split(os.getcwd())[-1]
|
|
tester('ntpath.relpath("a")', 'a')
|
|
tester('ntpath.relpath(os.path.abspath("a"))', 'a')
|
|
tester('ntpath.relpath("a/b")', 'a\\b')
|
|
tester('ntpath.relpath("../a/b")', '..\\a\\b')
|
|
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("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
|
|
tester('ntpath.relpath("a", "a")', '.')
|
|
|
|
|
|
def test_main():
|
|
test_support.run_unittest(TestNtpath)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|