diff --git a/gluon/tests/test_tools.py b/gluon/tests/test_tools.py index 79f2db03..c8009bcc 100644 --- a/gluon/tests/test_tools.py +++ b/gluon/tests/test_tools.py @@ -1081,12 +1081,38 @@ class TestToolsFunctions(unittest.TestCase): self.assertEqual(prettydate(d='invalid_date'), '[invalid date]') - pjoin = os.path.join def have_symlinks(): 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): def setUp(self): diff --git a/gluon/tools.py b/gluon/tools.py index 5138d076..2734118c 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -6294,15 +6294,20 @@ class Expose(object): for folder in self.folders], **dict(_class="table"))) 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): """True if f/ is under self.base/ Where f ans slef.base are normalized paths """ - s = lambda f: '%s%s' % (f, os.path.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(self.normalize_path(f)).startswith(s(self.base)) + return self.__in_base(self.normalize_path(f), self.base) def normalize_path(self, f): if self.follow_symlink_out: