fix in_base for base='/'
If the base directory already ends with '/' the test failed.
It failed because we added an extra '/' to make sure that '/foobar' is
not under '/foo', so ask '/foobar/'.startswith('/foo/').
Whoever when we have the base already start with '/' we might test:
'/foo/bar/'.startwith('/foo//'), and give a false negative. We
shouldn't have this case, because we normalized the path, but in the
case of the root directory ('/') even a normalized path ends with '/',
and thus when base='/' this function failed.
Some re-factoring was needed to make this base testable.
This commit is contained in:
@@ -1081,12 +1081,38 @@ class TestToolsFunctions(unittest.TestCase):
|
|||||||
self.assertEqual(prettydate(d='invalid_date'), '[invalid date]')
|
self.assertEqual(prettydate(d='invalid_date'), '[invalid date]')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pjoin = os.path.join
|
pjoin = os.path.join
|
||||||
|
|
||||||
def have_symlinks():
|
def have_symlinks():
|
||||||
return os.name == 'posix'
|
return os.name == 'posix'
|
||||||
|
|
||||||
|
|
||||||
|
class Test_Expose__in_base(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_in_base(self):
|
||||||
|
are_under = [ # (sub, base)
|
||||||
|
('/foo/bar', '/foo'),
|
||||||
|
('/foo', '/foo'),
|
||||||
|
('/foo', '/'),
|
||||||
|
('/', '/'),
|
||||||
|
]
|
||||||
|
for sub, base in are_under:
|
||||||
|
self.assertTrue( Expose._Expose__in_base(subdir=sub, basedir=base, sep='/'),
|
||||||
|
'%s is not under %s' % (sub, base) )
|
||||||
|
|
||||||
|
def test_not_in_base(self):
|
||||||
|
are_not_under = [ # (sub, base)
|
||||||
|
('/foobar', '/foo'),
|
||||||
|
('/foo', '/foo/bar'),
|
||||||
|
('/bar', '/foo'),
|
||||||
|
('/foo/bar', '/bar'),
|
||||||
|
('/', '/x'),
|
||||||
|
]
|
||||||
|
for sub, base in are_not_under:
|
||||||
|
self.assertFalse( Expose._Expose__in_base(subdir=sub, basedir=base, sep='/'),
|
||||||
|
'%s should not be under %s' % (sub, base) )
|
||||||
|
|
||||||
|
|
||||||
class TestExpose(unittest.TestCase):
|
class TestExpose(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|||||||
+10
-5
@@ -6294,15 +6294,20 @@ class Expose(object):
|
|||||||
for folder in self.folders], **dict(_class="table")))
|
for folder in self.folders], **dict(_class="table")))
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __in_base(subdir, basedir, sep=os.path.sep):
|
||||||
|
"""True if subdir/ is under basedir/"""
|
||||||
|
s = lambda f: '%s%s' % (f.rstrip(sep), sep) # f -> f/
|
||||||
|
# The trailing '/' is for the case of '/foobar' in_base of '/foo':
|
||||||
|
# - becase '/foobar' starts with '/foo'
|
||||||
|
# - but '/foobar/' doesn't start with '/foo/'
|
||||||
|
return s(subdir).startswith(s(basedir))
|
||||||
|
|
||||||
def in_base(self, f):
|
def in_base(self, f):
|
||||||
"""True if f/ is under self.base/
|
"""True if f/ is under self.base/
|
||||||
Where f ans slef.base are normalized paths
|
Where f ans slef.base are normalized paths
|
||||||
"""
|
"""
|
||||||
s = lambda f: '%s%s' % (f, os.path.sep) # f -> f/
|
return self.__in_base(self.normalize_path(f), self.base)
|
||||||
# The trailing '/' is for the case of '/foobar' in_base of '/foo':
|
|
||||||
# - becase '/foobar' starts with '/foo'
|
|
||||||
# - but '/foobar/' doesn't start with '/foo/'
|
|
||||||
return s(self.normalize_path(f)).startswith(s(self.base))
|
|
||||||
|
|
||||||
def normalize_path(self, f):
|
def normalize_path(self, f):
|
||||||
if self.follow_symlink_out:
|
if self.follow_symlink_out:
|
||||||
|
|||||||
Reference in New Issue
Block a user