Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for lowercase drive letter on Windows #885

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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