Skip to content

Commit aed5642

Browse files
authored
Stubgen: normalize input paths when finding common parent directory for out files (#8067)
Signed-off-by: Oleg Höfling <[email protected]>
1 parent e815e48 commit aed5642

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

mypy/stubutil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ def remove_misplaced_type_comments(source: Union[str, bytes]) -> Union[str, byte
247247
def common_dir_prefix(paths: List[str]) -> str:
248248
if not paths:
249249
return '.'
250-
cur = os.path.dirname(paths[0])
250+
cur = os.path.dirname(os.path.normpath(paths[0]))
251251
for path in paths[1:]:
252252
while True:
253-
path = os.path.dirname(path)
254-
if (cur + '/').startswith(path + '/'):
253+
path = os.path.dirname(os.path.normpath(path))
254+
if (cur + os.sep).startswith(path + os.sep):
255255
cur = path
256256
break
257257
return cur or '.'

mypy/test/teststubgen.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,9 @@ def h():
445445

446446
assert_equal(remove_misplaced_type_comments(original), dest)
447447

448-
def test_common_dir_prefix(self) -> None:
448+
@unittest.skipIf(sys.platform == 'win32',
449+
'Tests building the paths common ancestor on *nix')
450+
def test_common_dir_prefix_unix(self) -> None:
449451
assert common_dir_prefix([]) == '.'
450452
assert common_dir_prefix(['x.pyi']) == '.'
451453
assert common_dir_prefix(['./x.pyi']) == '.'
@@ -458,6 +460,26 @@ def test_common_dir_prefix(self) -> None:
458460
assert common_dir_prefix(['foo/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo'
459461
assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == 'foo/bar'
460462
assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo/bar'
463+
assert common_dir_prefix([r'foo/bar\x.pyi']) == 'foo'
464+
assert common_dir_prefix([r'foo\bar/x.pyi']) == r'foo\bar'
465+
466+
@unittest.skipIf(sys.platform != 'win32',
467+
'Tests building the paths common ancestor on Windows')
468+
def test_common_dir_prefix_win(self) -> None:
469+
assert common_dir_prefix(['x.pyi']) == '.'
470+
assert common_dir_prefix([r'.\x.pyi']) == '.'
471+
assert common_dir_prefix([r'foo\bar\x.pyi']) == r'foo\bar'
472+
assert common_dir_prefix([r'foo\bar\x.pyi',
473+
r'foo\bar\y.pyi']) == r'foo\bar'
474+
assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\y.pyi']) == 'foo'
475+
assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\y.pyi']) == 'foo'
476+
assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\y.pyi']) == 'foo'
477+
assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\zar\y.pyi']) == 'foo'
478+
assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\bar\y.pyi']) == r'foo\bar'
479+
assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\bar\zar\y.pyi']) == r'foo\bar'
480+
assert common_dir_prefix([r'foo/bar\x.pyi']) == r'foo\bar'
481+
assert common_dir_prefix([r'foo\bar/x.pyi']) == r'foo\bar'
482+
assert common_dir_prefix([r'foo/bar/x.pyi']) == r'foo\bar'
461483

462484

463485
class StubgenHelpersSuite(unittest.TestCase):

0 commit comments

Comments
 (0)