Skip to content

Commit

Permalink
Merge pull request #885 from gifvex/gifvex/uri_from_path
Browse files Browse the repository at this point in the history
Account for lowercase drive letter on Windows
  • Loading branch information
vinistock authored Aug 14, 2023
2 parents 7a6dcf3 + dbd8be4 commit b23dcbe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/core_ext/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class << self
sig { params(path: String, scheme: String).returns(URI::Generic) }
def from_path(path:, scheme: "file")
# On Windows, if the path begins with the disk name, we need to add a leading slash to make it a valid URI
escaped_path = if /^[A-Z]:/.match?(path)
escaped_path = if /^[A-Z]:/i.match?(path)
DEFAULT_PARSER.escape("/#{path}")
else
DEFAULT_PARSER.escape(path)
Expand All @@ -26,15 +26,15 @@ def to_standardized_path
parsed_path = path
return unless parsed_path

unescaped_path = CGI.unescape(parsed_path)

# On Windows, when we're getting the file system path back from the URI, we need to remove the leading forward
# slash
actual_path = if %r{^/[A-Z]:}.match?(parsed_path)
parsed_path.delete_prefix("/")
if %r{^/[A-Z]:}i.match?(unescaped_path)
unescaped_path.delete_prefix("/")
else
parsed_path
unescaped_path
end

CGI.unescape(actual_path)
end

sig { returns(String) }
Expand Down
19 changes: 17 additions & 2 deletions test/requests/support/uri_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ def test_from_path_on_windows
assert_equal("/C:/some/windows/path/to/file.rb", uri.path)
end

def test_to_path_on_unix
def test_from_path_on_windows_with_lowercase_drive
uri = URI::Generic.from_path(path: "c:/some/windows/path/to/file.rb")
assert_equal("/c:/some/windows/path/to/file.rb", uri.path)
end

def test_to_standardized_path_on_unix
uri = URI::Generic.from_path(path: "/some/unix/path/to/file.rb")
assert_equal(uri.path, uri.to_standardized_path)
end

def test_to_path_on_windows
def test_to_standardized_path_on_windows
uri = URI::Generic.from_path(path: "C:/some/windows/path/to/file.rb")
assert_equal("C:/some/windows/path/to/file.rb", uri.to_standardized_path)
end

def test_to_standardized_path_on_windows_with_lowercase_drive
uri = URI::Generic.from_path(path: "c:/some/windows/path/to/file.rb")
assert_equal("c:/some/windows/path/to/file.rb", uri.to_standardized_path)
end

def test_to_standardized_path_on_windows_with_received_uri
uri = URI("file:///c%3A/some/windows/path/to/file.rb")
assert_equal("c:/some/windows/path/to/file.rb", uri.to_standardized_path)
end
end
end

0 comments on commit b23dcbe

Please sign in to comment.