Skip to content

Commit

Permalink
Fix separators in local/remote FilePath
Browse files Browse the repository at this point in the history
Add tests for FilePath in Unix/Win and Win/Unix constellations
  • Loading branch information
Bernd Rothert committed Jan 25, 2014
1 parent 32672ab commit fdcb1a9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
33 changes: 22 additions & 11 deletions plugin/python/vdebug/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self,filename):
self.is_win = True
if filename[0] == "/":
filename = filename[1:]
filename = filename.replace('/', '\\')

self.local = self._create_local(filename)
self.remote = self._create_remote(filename)
Expand All @@ -101,17 +102,22 @@ def _create_local(self,f):
Uses the "local_path" and "remote_path" options.
"""
ret = f
if ret[2] == "/":
ret = ret.replace("/","\\")


if vdebug.opts.Options.isset('path_maps'):
for remote, local in vdebug.opts.Options.get('path_maps', dict).items():
if remote in ret:
vdebug.log.Log("Replacing remote path (%s) " % remote +\
"with local path (%s)" % local ,\
vdebug.log.Logger.DEBUG)
ret = ret.replace(remote,local)
ret = ret.replace(remote,local,1)

# determine remote path separator and replace by local
local_sep = self._findSeparator(local)
remote_sep = self._findSeparator(remote)
if local_sep and remote_sep and remote_sep != local_sep:
ret = ret.replace(remote_sep, local_sep)
break

return ret

def _create_remote(self,f):
Expand All @@ -127,16 +133,15 @@ def _create_remote(self,f):
vdebug.log.Log("Replacing local path (%s) " % local +\
"with remote path (%s)" % remote ,\
vdebug.log.Logger.DEBUG)
ret = ret.replace(local,remote)
ret = ret.replace(local,remote,1)
# replace remaining local separators with URL '/' separators
ret = ret.replace('\\', '/')
break

if ret[2] == "\\":
ret = ret.replace("\\","/")

if self.is_win:
return "file:///"+ret
else:
if ret.startswith('/'):
return "file://"+ret
else:
return "file:///"+ret

def as_local(self,quote = False):
if quote:
Expand All @@ -147,6 +152,12 @@ def as_local(self,quote = False):
def as_remote(self):
return self.remote

def _findSeparator(self, path):
for sep in '\\/':
if sep in path:
return sep
return None

def __eq__(self,other):
if isinstance(other,FilePath):
if other.as_local() == self.as_local():
Expand Down
54 changes: 54 additions & 0 deletions tests/test_util_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,57 @@ def test_as_remote_with_backslashed_win_paths(self):
filename = "C:/local2/path/to/file"
file = FilePath(filename)
self.assertEqual("C:\\local2\\path\\to\\file",file.as_local())

class RemoteWinLocalUnixPathTest(unittest.TestCase):
def setUp(self):
vdebug.opts.Options.set({'path_maps':{'G:\\remote\\path':'/local/path', 'G:\\remote2\\path':'/local2/path'}})

def test_as_local(self):
filename = "G:\\remote\\path\\to\\file"
file = FilePath(filename)
self.assertEqual("/local/path/to/file",file.as_local())

filename = "file:///G:/remote2/path/to/file"
file = FilePath(filename)
self.assertEqual("/local2/path/to/file",file.as_local())

def test_as_local_does_nothing(self):
filename = "/the/path/to/file"
file = FilePath(filename)
self.assertEqual("/the/path/to/file",file.as_local())

def test_as_remote(self):
filename = "/local/path/to/file"
file = FilePath(filename)
self.assertEqual("file:///G:/remote/path/to/file",file.as_remote())

filename = "file:///local2/path/to/file"
file = FilePath(filename)
self.assertEqual("file:///G:/remote2/path/to/file",file.as_remote())

class RemoteUnixLocalWinPathTest(unittest.TestCase):
def setUp(self):
vdebug.opts.Options.set({'path_maps':{'/remote/path':'G:\\local\\path', '/remote2/path':'G:\\local2\\path'}})

def test_as_local(self):
filename = "/remote/path/to/file"
file = FilePath(filename)
self.assertEqual("G:\\local\\path\\to\\file",file.as_local())

filename = "file:///remote2/path/to/file"
file = FilePath(filename)
self.assertEqual("G:\\local2\\path\\to\\file",file.as_local())

def test_as_local_does_nothing(self):
filename = "G:\\the\\path\\to\\file"
file = FilePath(filename)
self.assertEqual("G:\\the\\path\\to\\file",file.as_local())

def test_as_remote(self):
filename = "G:\\local\\path\\to\\file"
file = FilePath(filename)
self.assertEqual("file:///remote/path/to/file",file.as_remote())

filename = "file:///G:/local2/path/to/file"
file = FilePath(filename)
self.assertEqual("file:///remote2/path/to/file",file.as_remote())

0 comments on commit fdcb1a9

Please sign in to comment.