diff --git a/mypy/stubutil.py b/mypy/stubutil.py index d21ba4059913..5772d3fc9981 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -247,11 +247,11 @@ def remove_misplaced_type_comments(source: Union[str, bytes]) -> Union[str, byte def common_dir_prefix(paths: List[str]) -> str: if not paths: return '.' - cur = os.path.dirname(paths[0]) + cur = os.path.dirname(os.path.normpath(paths[0])) for path in paths[1:]: while True: - path = os.path.dirname(path) - if (cur + '/').startswith(path + '/'): + path = os.path.dirname(os.path.normpath(path)) + if (cur + os.sep).startswith(path + os.sep): cur = path break return cur or '.' diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 3566f03fb9a1..d5fb7bdf14cc 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -445,7 +445,9 @@ def h(): assert_equal(remove_misplaced_type_comments(original), dest) - def test_common_dir_prefix(self) -> None: + @unittest.skipIf(sys.platform == 'win32', + 'Tests building the paths common ancestor on *nix') + def test_common_dir_prefix_unix(self) -> None: assert common_dir_prefix([]) == '.' assert common_dir_prefix(['x.pyi']) == '.' assert common_dir_prefix(['./x.pyi']) == '.' @@ -458,6 +460,26 @@ def test_common_dir_prefix(self) -> None: assert common_dir_prefix(['foo/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo' assert common_dir_prefix(['foo/bar/zar/x.pyi', 'foo/bar/y.pyi']) == 'foo/bar' assert common_dir_prefix(['foo/bar/x.pyi', 'foo/bar/zar/y.pyi']) == 'foo/bar' + assert common_dir_prefix([r'foo/bar\x.pyi']) == 'foo' + assert common_dir_prefix([r'foo\bar/x.pyi']) == r'foo\bar' + + @unittest.skipIf(sys.platform != 'win32', + 'Tests building the paths common ancestor on Windows') + def test_common_dir_prefix_win(self) -> None: + assert common_dir_prefix(['x.pyi']) == '.' + assert common_dir_prefix([r'.\x.pyi']) == '.' + assert common_dir_prefix([r'foo\bar\x.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar\x.pyi', + r'foo\bar\y.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\x.pyi', r'foo\bar\zar\y.pyi']) == 'foo' + assert common_dir_prefix([r'foo\bar\zar\x.pyi', r'foo\bar\y.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar\x.pyi', r'foo\bar\zar\y.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo/bar\x.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo\bar/x.pyi']) == r'foo\bar' + assert common_dir_prefix([r'foo/bar/x.pyi']) == r'foo\bar' class StubgenHelpersSuite(unittest.TestCase):
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: