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'/', '/', ), ) from zope.app.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)