Handle os.path.commonprefix exceptions (#124)

fix multidrive win chop

On Windows, when using different drives between source and report, `report.chop` raises an exception "ValueError("path is on mount %r, start on mount %r" % (path_drive, start_drive))"
(see https://github.com/python/cpython/blob/master/Lib/ntpath.py#L703)
This commit is contained in:
Thomas Pelletier
2020-03-03 23:29:25 +01:00
committed by GitHub
parent fddb84945c
commit d46af2ec93
2 changed files with 9 additions and 2 deletions

View File

@@ -492,8 +492,13 @@ def safe_readlines(filename):
def chop(prefix, filename):
# type: (str, str) -> str
""" Create 'filename' from '/prefix/filename' """
return filename if not prefix else os.path.relpath(filename, prefix)
result = filename
if prefix:
try:
result = os.path.relpath(filename, prefix)
except ValueError:
pass
return result
def escape(text):

View File

@@ -116,6 +116,8 @@ class ReportMethodTest(unittest.TestCase):
self.assertEqual('lib\\file',
sut.chop('c:\\prefix\\', 'c:\\prefix\\lib\\file'))
self.assertEqual('c:\\prefix\\file', sut.chop('', 'c:\\prefix\\file'))
self.assertEqual('c:\\prefix\\file',
sut.chop('e:\\prefix', 'c:\\prefix\\file'))
@unittest.skipIf(not IS_WINDOWS, 'windows has different path patterns')
def test_chop_when_cwd_on_windows(self):