Skip to content

Commit fb417d3

Browse files
committed
fix #222
1 parent 75d61fc commit fb417d3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

tests/testsuite.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import xmlrunner
1414
from xmlrunner.result import _DuplicateWriter
1515
from xmlrunner.result import _XMLTestResult
16+
from xmlrunner.result import resolve_filename
1617
import doctest
1718
import tests.doctest_example
1819
from io import StringIO, BytesIO
@@ -964,3 +965,23 @@ def test_xmlrunner_output_file(self, exiter, testrunner, opener):
964965

965966
testrunner.assert_called_once_with(**kwargs)
966967
exiter.assert_called_once_with(False)
968+
969+
970+
class ResolveFilenameTestCase(unittest.TestCase):
971+
@mock.patch('os.path.relpath')
972+
def test_resolve_filename_relative(self, relpath):
973+
relpath.return_value = 'somefile.py'
974+
filename = resolve_filename('/path/to/somefile.py')
975+
self.assertEqual(filename, 'somefile.py')
976+
977+
@mock.patch('os.path.relpath')
978+
def test_resolve_filename_outside(self, relpath):
979+
relpath.return_value = '../../../tmp/somefile.py'
980+
filename = resolve_filename('/tmp/somefile.py')
981+
self.assertEqual(filename, '/tmp/somefile.py')
982+
983+
@mock.patch('os.path.relpath')
984+
def test_resolve_filename_error(self, relpath):
985+
relpath.side_effect = ValueError("ValueError: path is on mount 'C:', start on mount 'D:'")
986+
filename = resolve_filename('C:\\path\\to\\somefile.py')
987+
self.assertEqual(filename, 'C:\\path\\to\\somefile.py')

xmlrunner/result.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ def testcase_name(test_method):
6969
return result
7070

7171

72+
def resolve_filename(filename):
73+
# Try to make filename relative to current directory.
74+
try:
75+
rel_filename = os.path.relpath(filename)
76+
except ValueError:
77+
return filename
78+
# if not inside folder, keep as-is
79+
return filename if rel_filename.startswith('../') else rel_filename
80+
81+
7282
class _DuplicateWriter(io.TextIOBase):
7383
"""
7484
Duplicate output from the first handle to the second handle
@@ -559,8 +569,7 @@ def _report_testcase(test_result, xml_testsuite, xml_document):
559569

560570
if test_result.filename is not None:
561571
# Try to make filename relative to current directory.
562-
filename = os.path.relpath(test_result.filename)
563-
filename = test_result.filename if filename.startswith('../') else filename
572+
filename = resolve_filename(test_result.filename)
564573
testcase.setAttribute('file', filename)
565574

566575
if test_result.lineno is not None:

0 commit comments

Comments
 (0)