Ejemplo n.º 1
0
def test_convert_utilities(tmpdir):
    import pydevd_file_utils
    import sys

    test_dir = str(tmpdir.mkdir("Test_Convert_Utilities"))
    if sys.platform == 'win32':
        normalized = pydevd_file_utils.normcase(test_dir)
        assert isinstance(normalized, str)  # bytes on py2, unicode on py3
        assert normalized.lower() == normalized

        assert '~' not in normalized
        assert '~' in pydevd_file_utils.convert_to_short_pathname(normalized)

        real_case = pydevd_file_utils.get_path_with_real_case(normalized)
        assert isinstance(real_case, str)  # bytes on py2, unicode on py3
        # Note test_dir itself cannot be compared with because pytest may
        # have passed the case normalized.
        assert real_case.endswith("Test_Convert_Utilities")

    else:
        # On other platforms, nothing should change
        assert pydevd_file_utils.normcase(test_dir) == test_dir
        assert pydevd_file_utils.convert_to_short_pathname(
            test_dir) == test_dir
        assert pydevd_file_utils.get_path_with_real_case(test_dir) == test_dir
Ejemplo n.º 2
0
def test_convert_utilities(tmpdir):
    import pydevd_file_utils

    test_dir = str(tmpdir.mkdir("Test_Convert_Utilities"))

    if IS_WINDOWS:
        normalized = pydevd_file_utils.normcase(test_dir)
        assert isinstance(normalized, str)  # bytes on py2, unicode on py3
        assert normalized.lower() == normalized

        upper_version = os.path.join(test_dir, 'ÁÉÍÓÚ')
        with open(upper_version, 'w') as stream:
            stream.write('test')

        with open(upper_version, 'r') as stream:
            assert stream.read() == 'test'

        with open(pydevd_file_utils.normcase(upper_version), 'r') as stream:
            assert stream.read() == 'test'

        assert '~' not in normalized

        for i in range(3):  # Check if cache is ok.

            if i == 2:
                pydevd_file_utils._listdir_cache.clear()

            assert pydevd_file_utils.get_path_with_real_case('<does not EXIST>') == '<does not EXIST>'
            real_case = pydevd_file_utils.get_path_with_real_case(normalized)
            assert isinstance(real_case, str)  # bytes on py2, unicode on py3
            # Note test_dir itself cannot be compared with because pytest may
            # have passed the case normalized.
            assert real_case.endswith("Test_Convert_Utilities")

            if i == 2:
                # Check that we have the expected paths in the cache.
                assert pydevd_file_utils._listdir_cache[os.path.dirname(normalized).lower()] == ['Test_Convert_Utilities']
                assert pydevd_file_utils._listdir_cache[(os.path.dirname(normalized).lower(), 'Test_Convert_Utilities'.lower())] == real_case

            if IS_PY2:
                # Test with unicode in python 2 too.
                real_case = pydevd_file_utils.get_path_with_real_case(normalized.decode(
                    getfilesystemencoding()))
                assert isinstance(real_case, str)  # bytes on py2, unicode on py3
                # Note test_dir itself cannot be compared with because pytest may
                # have passed the case normalized.
                assert real_case.endswith("Test_Convert_Utilities")

        # Check that it works with a shortened path.
        shortened = pydevd_file_utils.convert_to_short_pathname(normalized)
        assert '~' in shortened
        with_real_case = pydevd_file_utils.get_path_with_real_case(shortened)
        assert with_real_case.endswith('Test_Convert_Utilities')
        assert '~' not in with_real_case

    else:
        # On other platforms, nothing should change
        assert pydevd_file_utils.normcase(test_dir) == test_dir
        assert pydevd_file_utils.get_path_with_real_case(test_dir) == test_dir
Ejemplo n.º 3
0
    def matches(self, other):
        if isinstance(other, py.path.local):
            other = other.strpath

        if isinstance(other, unicode):
            pass
        elif isinstance(other, bytes):
            other = other.encode(sys.getfilesystemencoding())
        else:
            return NotImplemented

        left = pydevd_file_utils.get_path_with_real_case(self.path)
        right = pydevd_file_utils.get_path_with_real_case(other)
        return left == right
Ejemplo n.º 4
0
def compare_path(left, right, show=True):
    # If there's a unicode/bytes mismatch, make both unicode.
    if isinstance(left, unicode):
        if not isinstance(right, unicode):
            right = right.decode(sys.getfilesystemencoding())
    elif isinstance(right, unicode):
        right = right.encode(sys.getfilesystemencoding())

    n_left = get_path_with_real_case(left)
    n_right = get_path_with_real_case(right)
    if show:
        print('LEFT : ' + n_left)
        print('RIGHT: ' + n_right)
    return n_left == n_right
Ejemplo n.º 5
0
def test_convert_utilities(tmpdir):
    import pydevd_file_utils

    test_dir = str(tmpdir.mkdir("Test_Convert_Utilities"))

    if IS_WINDOWS:
        normalized = pydevd_file_utils.normcase(test_dir)
        assert isinstance(normalized, str)  # bytes on py2, unicode on py3
        assert normalized.lower() == normalized

        upper_version = os.path.join(test_dir, 'ÁÉÍÓÚ')
        with open(upper_version, 'w') as stream:
            stream.write('test')

        with open(upper_version, 'r') as stream:
            assert stream.read() == 'test'

        with open(pydevd_file_utils.normcase(upper_version), 'r') as stream:
            assert stream.read() == 'test'

        assert '~' not in normalized
        if not IS_JYTHON:
            assert '~' in pydevd_file_utils.convert_to_short_pathname(
                normalized)

        real_case = pydevd_file_utils.get_path_with_real_case(normalized)
        assert isinstance(real_case, str)  # bytes on py2, unicode on py3
        # Note test_dir itself cannot be compared with because pytest may
        # have passed the case normalized.
        assert real_case.endswith("Test_Convert_Utilities")

    else:
        # On other platforms, nothing should change
        assert pydevd_file_utils.normcase(test_dir) == test_dir
        assert pydevd_file_utils.convert_to_short_pathname(
            test_dir) == test_dir
        assert pydevd_file_utils.get_path_with_real_case(test_dir) == test_dir
def test_to_server_and_to_client(tmpdir):
    try:

        def check(obtained, expected):
            assert obtained == expected, '%s (%s) != %s (%s)' % (
                obtained, type(obtained), expected, type(expected))
            if isinstance(obtained, tuple):
                assert isinstance(obtained[0],
                                  str)  # bytes on py2, unicode on py3
            else:
                assert isinstance(obtained,
                                  str)  # bytes on py2, unicode on py3

            if isinstance(expected, tuple):
                assert isinstance(expected[0],
                                  str)  # bytes on py2, unicode on py3
            else:
                assert isinstance(expected,
                                  str)  # bytes on py2, unicode on py3

        import pydevd_file_utils
        if IS_WINDOWS:
            # Check with made-up files

            pydevd_file_utils.setup_client_server_paths([
                ('c:\\foo', 'c:\\bar'), ('c:\\foo2', 'c:\\bar2')
            ])

            stream = io.StringIO()
            with log_context(0, stream=stream):
                pydevd_file_utils.map_file_to_server(
                    'y:\\only_exists_in_client_not_in_server')
            assert r'pydev debugger: unable to find translation for: "y:\only_exists_in_client_not_in_server" in ["c:\foo\", "c:\foo2\", "c:\foo", "c:\foo2"] (please revise your path mappings).' in stream.getvalue(
            )

            # Client and server are on windows.
            pydevd_file_utils.set_ide_os('WINDOWS')
            for in_eclipse, in_python in ([
                ('c:\\foo', 'c:\\bar'),
                ('c:/foo', 'c:\\bar'),
                ('c:\\foo', 'c:/bar'),
                ('c:\\foo', 'c:\\bar\\'),
                ('c:/foo', 'c:\\bar\\'),
                ('c:\\foo', 'c:/bar/'),
                ('c:\\foo\\', 'c:\\bar'),
                ('c:/foo/', 'c:\\bar'),
                ('c:\\foo\\', 'c:/bar'),
            ]):
                PATHS_FROM_ECLIPSE_TO_PYTHON = [(in_eclipse, in_python)]
                pydevd_file_utils.setup_client_server_paths(
                    PATHS_FROM_ECLIPSE_TO_PYTHON)
                check(pydevd_file_utils.map_file_to_server('c:\\foo\\my'),
                      'c:\\bar\\my')
                check(pydevd_file_utils.map_file_to_server('c:/foo/my'),
                      'c:\\bar\\my')
                check(pydevd_file_utils.map_file_to_server('c:/foo/my/'),
                      'c:\\bar\\my')
                check(
                    pydevd_file_utils.map_file_to_server(
                        'c:\\foo\\áéíóú'.upper()),
                    'c:\\bar' + '\\áéíóú'.upper())
                check(pydevd_file_utils.map_file_to_client('c:\\bar\\my'),
                      ('c:\\foo\\my', True))

            # Client on unix and server on windows
            pydevd_file_utils.set_ide_os('UNIX')
            for in_eclipse, in_python in ([
                ('/foo', 'c:\\bar'),
                ('/foo', 'c:/bar'),
                ('/foo', 'c:\\bar\\'),
                ('/foo', 'c:/bar/'),
                ('/foo/', 'c:\\bar'),
                ('/foo/', 'c:\\bar\\'),
            ]):

                PATHS_FROM_ECLIPSE_TO_PYTHON = [(in_eclipse, in_python)]
                pydevd_file_utils.setup_client_server_paths(
                    PATHS_FROM_ECLIPSE_TO_PYTHON)
                check(pydevd_file_utils.map_file_to_server('/foo/my'),
                      'c:\\bar\\my')
                check(pydevd_file_utils.map_file_to_client('c:\\bar\\my'),
                      ('/foo/my', True))
                check(pydevd_file_utils.map_file_to_client('c:\\bar\\my\\'),
                      ('/foo/my', True))
                check(pydevd_file_utils.map_file_to_client('c:/bar/my'),
                      ('/foo/my', True))
                check(pydevd_file_utils.map_file_to_client('c:/bar/my/'),
                      ('/foo/my', True))

            # Test with 'real' files
            # Client and server are on windows.
            pydevd_file_utils.set_ide_os('WINDOWS')

            test_dir = pydevd_file_utils.get_path_with_real_case(
                str(tmpdir.mkdir("Foo")))
            os.makedirs(os.path.join(test_dir, "Another"))

            in_eclipse = os.path.join(os.path.dirname(test_dir), 'Bar')
            in_python = test_dir
            PATHS_FROM_ECLIPSE_TO_PYTHON = [(in_eclipse, in_python)]
            pydevd_file_utils.setup_client_server_paths(
                PATHS_FROM_ECLIPSE_TO_PYTHON)

            if pydevd_file_utils.map_file_to_server(
                    in_eclipse) != in_python.lower():
                raise AssertionError(
                    '%s != %s\ntmpdir:%s\nin_eclipse: %s\nin_python: %s\ntest_dir: %s'
                    % (pydevd_file_utils.map_file_to_server(in_eclipse),
                       in_python.lower(), tmpdir, in_eclipse, in_python,
                       test_dir))

            found_in_eclipse = pydevd_file_utils.map_file_to_client(
                in_python)[0]
            assert found_in_eclipse.endswith('Bar')

            assert pydevd_file_utils.map_file_to_server(
                os.path.join(in_eclipse,
                             'another')) == os.path.join(in_python,
                                                         'another').lower()
            found_in_eclipse = pydevd_file_utils.map_file_to_client(
                os.path.join(in_python, 'another'))[0]
            assert found_in_eclipse.endswith('Bar\\Another')

            # Client on unix and server on windows
            pydevd_file_utils.set_ide_os('UNIX')
            in_eclipse = '/foo'
            in_python = test_dir
            PATHS_FROM_ECLIPSE_TO_PYTHON = [(in_eclipse, in_python)]
            pydevd_file_utils.setup_client_server_paths(
                PATHS_FROM_ECLIPSE_TO_PYTHON)
            assert pydevd_file_utils.map_file_to_server(
                '/foo').lower() == in_python.lower()
            assert pydevd_file_utils.map_file_to_client(in_python) == (
                in_eclipse, True)

            # Test without translation in place (still needs to fix case and separators)
            pydevd_file_utils.set_ide_os('WINDOWS')
            PATHS_FROM_ECLIPSE_TO_PYTHON = []
            pydevd_file_utils.setup_client_server_paths(
                PATHS_FROM_ECLIPSE_TO_PYTHON)
            assert pydevd_file_utils.map_file_to_server(test_dir) == test_dir
            assert pydevd_file_utils.map_file_to_client(
                test_dir.lower())[0].endswith('\\Foo')
        else:
            # Client on windows and server on unix
            pydevd_file_utils.set_ide_os('WINDOWS')

            PATHS_FROM_ECLIPSE_TO_PYTHON = [('c:\\BAR', '/bar')]

            pydevd_file_utils.setup_client_server_paths(
                PATHS_FROM_ECLIPSE_TO_PYTHON)
            assert pydevd_file_utils.map_file_to_server(
                'c:\\bar\\my') == '/bar/my'
            assert pydevd_file_utils.map_file_to_client('/bar/my') == (
                'c:\\BAR\\my', True)

            for in_eclipse, in_python in ([
                ('c:\\foo', '/báéíóúr'),
                ('c:/foo', '/báéíóúr'),
                ('c:/foo/', '/báéíóúr'),
                ('c:/foo/', '/báéíóúr/'),
                ('c:\\foo\\', '/báéíóúr/'),
            ]):

                PATHS_FROM_ECLIPSE_TO_PYTHON = [(in_eclipse, in_python)]

                pydevd_file_utils.setup_client_server_paths(
                    PATHS_FROM_ECLIPSE_TO_PYTHON)
                assert pydevd_file_utils.map_file_to_server(
                    'c:\\foo\\my') == '/báéíóúr/my'
                assert pydevd_file_utils.map_file_to_server(
                    'C:\\foo\\my') == '/báéíóúr/my'
                assert pydevd_file_utils.map_file_to_server(
                    'C:\\foo\\MY') == '/báéíóúr/MY'
                assert pydevd_file_utils.map_file_to_server(
                    'C:\\foo\\MY\\') == '/báéíóúr/MY'
                assert pydevd_file_utils.map_file_to_server(
                    'c:\\foo\\my\\file.py') == '/báéíóúr/my/file.py'
                assert pydevd_file_utils.map_file_to_server(
                    'c:\\foo\\my\\other\\file.py'
                ) == '/báéíóúr/my/other/file.py'
                assert pydevd_file_utils.map_file_to_server(
                    'c:/foo/my') == '/báéíóúr/my'
                assert pydevd_file_utils.map_file_to_server(
                    'c:\\foo\\my\\') == '/báéíóúr/my'
                assert pydevd_file_utils.map_file_to_server(
                    'c:/foo/my/') == '/báéíóúr/my'

                assert pydevd_file_utils.map_file_to_client('/báéíóúr/my') == (
                    'c:\\foo\\my', True)
                assert pydevd_file_utils.map_file_to_client(
                    '/báéíóúr/my/') == ('c:\\foo\\my', True)

                # Files for which there's no translation have only their separators updated.
                assert pydevd_file_utils.map_file_to_client(
                    '/usr/bin/x.py') == ('\\usr\\bin\\x.py', False)
                assert pydevd_file_utils.map_file_to_client('/usr/bin') == (
                    '\\usr\\bin', False)
                assert pydevd_file_utils.map_file_to_client('/usr/bin/') == (
                    '\\usr\\bin', False)
                assert pydevd_file_utils.map_file_to_server(
                    '\\usr\\bin') == '/usr/bin'
                assert pydevd_file_utils.map_file_to_server(
                    '\\usr\\bin\\') == '/usr/bin'

                # When we have a client file and there'd be no translation, and making it absolute would
                # do something as '$cwd/$file_received' (i.e.: $cwd/c:/another in the case below),
                # warn the user that it's not correct and the path that should be translated instead
                # and don't make it absolute.
                assert pydevd_file_utils.map_file_to_server(
                    'c:\\Another') == 'c:/Another'

                assert pydevd_file_utils.map_file_to_server(
                    'c:/FoO/my/BAR') == '/báéíóúr/my/BAR'
                assert pydevd_file_utils.map_file_to_client(
                    '/báéíóúr/my/BAR') == ('c:\\foo\\my\\BAR', True)

            # Client and server on unix
            pydevd_file_utils.set_ide_os('UNIX')
            in_eclipse = '/foo'
            in_python = '/báéíóúr'
            PATHS_FROM_ECLIPSE_TO_PYTHON = [(in_eclipse, in_python)]
            pydevd_file_utils.setup_client_server_paths(
                PATHS_FROM_ECLIPSE_TO_PYTHON)
            assert pydevd_file_utils.map_file_to_server(
                '/foo/my') == '/báéíóúr/my'
            assert pydevd_file_utils.map_file_to_client('/báéíóúr/my') == (
                '/foo/my', True)
    finally:
        pydevd_file_utils.setup_client_server_paths([])