def _make_relative(self, path): if path.startswith("/") and self.pp: path = canonicalPath(path) if path.startswith(self.pp): path = path[self.pplen:] while path.startswith("/"): path = path[1:] return path
def testCanonicalPath(self): _bad_locations = ( (ValueError, '\xa323'), (ValueError, ''), (ValueError, '//'), (ValueError, '/foo//bar'), # regarding the next two errors: # having a trailing slash on a location is undefined. # we might want to give it a particular meaning for zope3 later # for now, it is an invalid location identifier (ValueError, '/foo/bar/'), (ValueError, 'foo/bar/'), (IndexError, '/a/../..'), (ValueError, '/a//v'), ) # sequence of N-tuples: # (loc_returned_as_string, input, input, ...) # The string and tuple are tested as input as well as being the # specification for output. _good_locations = ( # location returned as string ( u'/xx/yy/zz', # arguments to try in addition to the above '/xx/yy/zz', '/xx/./yy/ww/../zz', ), ( u'/xx/yy/zz', '/xx/yy/zz', ), ( u'/xx', '/xx', ), ( u'/', '/', self.root, ), ) from zope.traversing.api import canonicalPath for error_type, value in _bad_locations: self.assertRaises(error_type, canonicalPath, value) for spec in _good_locations: correct_answer = spec[0] for argument in spec: self.assertEqual(canonicalPath(argument), correct_answer, "failure on %s" % argument)
def testCanonicalPath(self): _bad_locations = ( (ValueError, '\xa323'), (ValueError, ''), (ValueError, '//'), (ValueError, '/foo//bar'), # regarding the next two errors: # having a trailing slash on a location is undefined. # we might want to give it a particular meaning for zope3 later # for now, it is an invalid location identifier (ValueError, '/foo/bar/'), (ValueError, 'foo/bar/'), (IndexError, '/a/../..'), (ValueError, '/a//v'), ) # sequence of N-tuples: # (loc_returned_as_string, input, input, ...) # The string and tuple are tested as input as well as being the # specification for output. _good_locations = ( # location returned as string (u'/xx/yy/zz', # arguments to try in addition to the above '/xx/yy/zz', '/xx/./yy/ww/../zz', ), (u'/xx/yy/zz', '/xx/yy/zz', ), (u'/xx', '/xx', ), (u'/', '/', self.root, ), ) from zope.traversing.api import canonicalPath for error_type, value in _bad_locations: self.assertRaises(error_type, canonicalPath, value) for spec in _good_locations: correct_answer = spec[0] for argument in spec: self.assertEqual(canonicalPath(argument), correct_answer, "failure on %s" % argument)
def _make_relative(self, path): if path.startswith("/") and self.pp: path = canonicalPath(path) if path.startswith(self.pp): path = path[self.pplen:] # Now, the path should not actually begin with a /. # canonicalPath doesn't allow trailing / in a path # segment, and we already cut off the whole length of # the parent, which we guaranteed to begin and end # with a /. But it's possible that older dependencies # than we test with could produce this scenario, so we # leave it for BWC. path = path.lstrip("/") return path