Skip to content

Commit

Permalink
Fix res URIs for Windows (#1785)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwols authored Jul 7, 2021
1 parent d7e9f50 commit f33fd06
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
3 changes: 1 addition & 2 deletions plugin/core/url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .typing import Any, Tuple
from urllib.parse import quote
from urllib.parse import urljoin
from urllib.parse import urlparse
from urllib.request import pathname2url
Expand Down Expand Up @@ -68,7 +67,7 @@ def _to_resource_uri(path: str, prefix: str) -> str:
See: https://github.com/sublimehq/sublime_text/issues/3742
"""
return "res://Packages{}".format(quote(path[len(prefix):]))
return "res://Packages{}".format(pathname2url(path[len(prefix):]))


def _uppercase_driveletter(match: Any) -> str:
Expand Down
45 changes: 39 additions & 6 deletions tests/test_url.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
from LSP.plugin.core.url import (filename_to_uri, uri_to_filename)
from LSP.plugin.core.url import filename_to_uri
from LSP.plugin.core.url import uri_to_filename
from LSP.plugin.core.url import view_to_uri
import sys
import unittest
import unittest.mock
import sublime
import os


@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
class WindowsTests(unittest.TestCase):

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_converts_path_to_uri(self):
self.assertEqual("file:///C:/dir%20ectory/file.txt", filename_to_uri("c:\\dir ectory\\file.txt"))

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_converts_uri_to_path(self):
self.assertEqual("C:\\dir ectory\\file.txt", uri_to_filename("file:///c:/dir ectory/file.txt"))

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_converts_encoded_bad_drive_uri_to_path(self):
# url2pathname does not understand %3A
self.assertEqual("C:\\dir ectory\\file.txt", uri_to_filename("file:///c%3A/dir%20ectory/file.txt"))

def test_view_to_uri_with_valid_filename(self):
view = sublime.active_window().active_view()
assert view
view.file_name = unittest.mock.MagicMock(
return_value="C:\\Users\\A b\\popups.css"
)
uri = view_to_uri(view)
self.assertEqual(uri, "file:///C:/Users/A%20b/popups.css")


@unittest.skipIf(sys.platform.startswith("win"), "requires non-Windows")
class NixTests(unittest.TestCase):

@unittest.skipIf(sys.platform.startswith("win"), "requires non-Windows")
def test_converts_path_to_uri(self):
self.assertEqual("file:///dir%20ectory/file.txt", filename_to_uri("/dir ectory/file.txt"))

@unittest.skipIf(sys.platform.startswith("win"), "requires non-Windows")
def test_converts_uri_to_path(self):
self.assertEqual("/dir ectory/file.txt", uri_to_filename("file:///dir ectory/file.txt"))

def test_view_to_uri_with_valid_filename(self):
view = sublime.active_window().active_view()
assert view
view.file_name = unittest.mock.MagicMock(return_value="/foo/bar/baz.txt")
uri = view_to_uri(view)
self.assertEqual(uri, "file:///foo/bar/baz.txt")


class MultiplatformTests(unittest.TestCase):

def test_resource_path(self):
uri = filename_to_uri(os.path.join(sublime.installed_packages_path(), "Package Control", "dir", "file.py"))
self.assertEqual(uri, "res://Packages/Package%20Control/dir/file.py")

def test_buffer_uri(self):
view = sublime.active_window().active_view()
assert view
view.file_name = unittest.mock.MagicMock(return_value=None)
view.buffer_id = unittest.mock.MagicMock(return_value=42)
uri = view_to_uri(view)
self.assertEqual(uri, "buffer://sublime/42")

0 comments on commit f33fd06

Please sign in to comment.