Skip to content

Commit

Permalink
fixes expand_path on relative reference and inside uri:classloader: a…
Browse files Browse the repository at this point in the history
…s CWD

using JRubyFile instead of regular File helps to keep the uri-like
paths intact.

fixes #3176

Sponsored by Lookout Inc.
  • Loading branch information
mkristian committed Jul 24, 2015
1 parent 0832a7c commit 21a9cbe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/org/jruby/RubyFile.java
Expand Up @@ -1664,7 +1664,8 @@ private static IRubyObject expandPathInternal(ThreadContext context, IRubyObject
if (expandUser) {
cwd = expandUserPath(context, cwd, true);
}


// TODO try to treat all uri-like paths alike
String[] cwdURIParts = splitURI(cwd);
if (uriParts == null && cwdURIParts != null) {
uriParts = cwdURIParts;
Expand All @@ -1681,7 +1682,8 @@ private static IRubyObject expandPathInternal(ThreadContext context, IRubyObject
// If the path isn't absolute, then prepend the current working
// directory to the path.
if (!startsWithSlashNotOnWindows && !startsWithDriveLetterOnWindows(cwd)) {
cwd = new File(runtime.getCurrentDirectory(), cwd).getAbsolutePath();
if ("".equals(cwd)) cwd = ".";
cwd = JRubyFile.create(runtime.getCurrentDirectory(), cwd).getAbsolutePath();
}
}
} else {
Expand Down
13 changes: 1 addition & 12 deletions core/src/main/ruby/jruby/kernel19/kernel.rb
Expand Up @@ -8,18 +8,7 @@ def require_relative(relative_arg)
file = $` # just the filename
raise LoadError, "cannot infer basepath" if /\A\((.*)\)/ =~ file # eval etc.

# FIXME: Classpath-path can be removed if we make expand_path+dirname
# know about classpath: paths.
if file =~ /^classpath:(.*)/
dir = File.dirname($1)
dir = dir == '.' ? "" : dir + "/"
absolute_feature = "classpath:#{dir}#{relative_arg}"
elsif file =~ /^uri:(.*)/
dir = File.dirname($1)
absolute_feature = "uri:#{dir}/#{relative_arg}"
else
absolute_feature = File.expand_path(relative_arg, File.dirname(file))
end
absolute_feature = File.expand_path(relative_arg, File.dirname(file))

require absolute_feature
end
Expand Down
9 changes: 9 additions & 0 deletions test/test_file.rb
Expand Up @@ -331,6 +331,15 @@ def test_expand_path_with_pathname_and_uri_path
assert_equal 'uri:classloader://foo', File.expand_path('foo', Pathname.new('uri:classloader:/'))
end

# GH-3176
def test_expand_path_with_relative_reference_and_inside_uri_classloader
jruby_specific_test
Dir.chdir( 'uri:classloader:/') do
assert_equal 'uri:classloader://something/foo', File.expand_path('foo', 'something')
assert_equal 'uri:classloader://foo', File.expand_path('foo', '.')
end
end

# JRUBY-5219
def test_expand_path_looks_like_url
jruby_specific_test
Expand Down

0 comments on commit 21a9cbe

Please sign in to comment.