Skip to content

Commit

Permalink
Properly handle Windows style file:/// uris
Browse files Browse the repository at this point in the history
  • Loading branch information
trotter committed Feb 3, 2011
1 parent a175e1d commit 406bda1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/rubygems/remote_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def download(spec, source_uri, install_dir = Gem.dir)
path = source_uri.path
path = File.dirname(path) if File.extname(path) == '.gem'

remote_gem_path = File.join(path, 'gems', gem_file_name)
remote_gem_path = correct_for_windows_path(File.join(path, 'gems', gem_file_name))

FileUtils.cp(remote_gem_path, local_gem_path)
rescue Errno::EACCES
Expand Down Expand Up @@ -270,6 +270,14 @@ def connection_for(uri)
raise FetchError.new(e.message, uri)
end

def correct_for_windows_path(path)
if path[0].chr == '/' && path[1].chr =~ /[a-zA-Z]/ && path[2].chr == ':'
path = path[1..-1]
else
path
end
end

##
# Read the data from the (source based) URI, but if it is a file:// URI,
# read from the filesystem instead.
Expand All @@ -287,13 +295,7 @@ def open_uri_or_path(uri, last_modified = nil, head = false, depth = 0)
end

if uri.scheme == 'file'
path = uri.path

# Deal with leading slash on Windows paths
if path[0].chr == '/' && path[1].chr =~ /[a-zA-Z]/ && path[2].chr == ':'
path = path[1..-1]
end

path = correct_for_windows_path(uri.path)
return Gem.read_binary(path)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/rubygems/spec_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def initialize
# Returns the local directory to write +uri+ to.

def cache_dir(uri)
File.join @dir, "#{uri.host}%#{uri.port}", File.dirname(uri.path)
escaped_path = uri.path.sub(%r[^/([a-zA-z]):/], '/\\1-/') # Correct for windows paths
File.join @dir, "#{uri.host}%#{uri.port}", File.dirname(escaped_path)
end

##
Expand Down
8 changes: 8 additions & 0 deletions test/rubygems/test_gem_remote_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -738,5 +738,13 @@ def start_server(port, data)
end
end

def test_correct_for_windows_path
path = "/C:/WINDOWS/Temp/gems"
assert_equal "C:/WINDOWS/Temp/gems", @fetcher.correct_for_windows_path(path)

path = "/home/skillet"
assert_equal "/home/skillet", @fetcher.correct_for_windows_path(path)
end

end

5 changes: 5 additions & 0 deletions test/rubygems/test_gem_spec_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,10 @@ def test_load_specs_cached_empty
assert_equal @latest_specs, latest_specs
end

def test_cache_dir_escapes_windows_paths
uri = URI.parse("file:///C:/WINDOWS/Temp/gem_repo")
cache_dir = @sf.cache_dir(uri)
assert cache_dir !~ /:/, "#{cache_dir} should not contain a :"
end
end

0 comments on commit 406bda1

Please sign in to comment.