Made normpath('/.') and normpath('/..') behave more consistently

parent 43854af5
...@@ -46,7 +46,8 @@ def normpath(path): ...@@ -46,7 +46,8 @@ def normpath(path):
if not _requires_normalization(path): if not _requires_normalization(path):
return path.rstrip('/') return path.rstrip('/')
components = [''] if path.startswith('/') else [] prefix = u'/' if path.startswith('/') else u''
components = []
append = components.append append = components.append
special = ('..', '.', '').__contains__ special = ('..', '.', '').__contains__
try: try:
...@@ -61,7 +62,7 @@ def normpath(path): ...@@ -61,7 +62,7 @@ def normpath(path):
# causing a circular import. # causing a circular import.
from fs.errors import BackReferenceError from fs.errors import BackReferenceError
raise BackReferenceError('Too many backrefs in \'%s\'' % path) raise BackReferenceError('Too many backrefs in \'%s\'' % path)
return u'/'.join(components) return prefix + u'/'.join(components)
if os.sep != '/': if os.sep != '/':
......
...@@ -18,6 +18,7 @@ class TestPathFunctions(unittest.TestCase): ...@@ -18,6 +18,7 @@ class TestPathFunctions(unittest.TestCase):
(".", ""), (".", ""),
("./", ""), ("./", ""),
("", ""), ("", ""),
("/.", "/"),
("/a/b/c", "/a/b/c"), ("/a/b/c", "/a/b/c"),
("a/b/c", "a/b/c"), ("a/b/c", "a/b/c"),
("a/b/../c/", "a/c"), ("a/b/../c/", "a/c"),
...@@ -50,7 +51,9 @@ class TestPathFunctions(unittest.TestCase): ...@@ -50,7 +51,9 @@ class TestPathFunctions(unittest.TestCase):
result = testpaths[-1] result = testpaths[-1]
self.assertEqual(pathjoin(*paths), result) self.assertEqual(pathjoin(*paths), result)
self.assertRaises(ValueError, pathjoin, "..")
self.assertRaises(ValueError, pathjoin, "../") self.assertRaises(ValueError, pathjoin, "../")
self.assertRaises(ValueError, pathjoin, "/..")
self.assertRaises(ValueError, pathjoin, "./../") self.assertRaises(ValueError, pathjoin, "./../")
self.assertRaises(ValueError, pathjoin, "a/b", "../../..") self.assertRaises(ValueError, pathjoin, "a/b", "../../..")
self.assertRaises(ValueError, pathjoin, "a/b/../../../d") self.assertRaises(ValueError, pathjoin, "a/b/../../../d")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment