Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: af48e11e06b0
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ee07ad67c99e
Choose a head ref
  • 2 commits
  • 8 files changed
  • 1 contributor

Commits on Jul 3, 2015

  1. added passing test

    mkristian committed Jul 3, 2015
    Copy the full SHA
    5616abe View commit details
  2. fix File.basename and Pathname#root? and Pathname#absolute? for uri-l…

    …ike paths
    
    on the root of the uri-like paths basename will return the path as is. with this
    and some extra pattern for detecting the root Pathname does calulate root? and
    absolute? correctly
    
    Sponsored by Lookout Inc.
    mkristian committed Jul 3, 2015
    Copy the full SHA
    ee07ad6 View commit details
Showing with 75 additions and 8 deletions.
  1. +9 −3 core/src/main/java/org/jruby/RubyFile.java
  2. +2 −2 lib/ruby/1.8/pathname.rb
  3. +2 −2 lib/ruby/1.9/pathname.rb
  4. +2 −0 test/jruby.1.8.index
  5. +2 −0 test/jruby.1.9.index
  6. +18 −0 test/test_file.rb
  7. +38 −0 test/test_pathname.rb
  8. +2 −1 test/test_uri_classloader.rb
12 changes: 9 additions & 3 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
@@ -505,9 +505,10 @@ public IRubyObject inspect() {
val.append(">");
return getRuntime().newString(val.toString());
}


private static Pattern ROOT_PATTERN = Pattern.compile("^(uri|jar|file|classpath):([^:]*:)?//?$");

/* File class methods */

@JRubyMethod(required = 1, optional = 1, meta = true)
public static IRubyObject basename(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
@@ -516,6 +517,11 @@ public static IRubyObject basename(ThreadContext context, IRubyObject recv, IRub
Encoding origEncoding = origString.getEncoding();
String name = origString.toString();

// uri-like paths without parent directory
if (name.endsWith("!/") || ROOT_PATTERN.matcher(name).matches()) {
return args[0];
}

// MRI-compatible basename handling for windows drive letter paths
if (Platform.IS_WINDOWS) {
if (name.length() > 1 && name.charAt(1) == ':' && Character.isLetter(name.charAt(0))) {
@@ -661,7 +667,7 @@ public static IRubyObject dirname(ThreadContext context, IRubyObject recv, IRuby
return runtime.newString(dirname(context, jfilename)).infectBy(filename);
}

private static Pattern PROTOCOL_PATTERN = Pattern.compile("^([a-z]+:)?[a-z]+:/.*");
private static Pattern PROTOCOL_PATTERN = Pattern.compile("^(uri|jar|file|classpath):([^:]*:)?//?.*");
public static String dirname(ThreadContext context, String jfilename) {
String name = jfilename.replace('\\', '/');
int minPathLength = 1;
4 changes: 2 additions & 2 deletions lib/ruby/1.8/pathname.rb
Original file line number Diff line number Diff line change
@@ -276,9 +276,9 @@ def sub(pattern, *rest, &block)
end

if File::ALT_SEPARATOR
SEPARATOR_PAT = /[#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}]/
SEPARATOR_PAT = /[#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}]|.+!\/|[a-z:]+:\/\/?/
else
SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}|.+!\/|[a-z:]+:\/\/?/
end

# chop_basename(path) -> [pre-basename, basename] or nil
4 changes: 2 additions & 2 deletions lib/ruby/1.9/pathname.rb
Original file line number Diff line number Diff line change
@@ -33,10 +33,10 @@ class Pathname

if File::ALT_SEPARATOR
SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
SEPARATOR_PAT = /[#{SEPARATOR_LIST}]|.+!\/|[a-z:]+:\/\/?/
else
SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}"
SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}|.+!\/|[a-z:]+:\/\/?/
end

# chop_basename(path) -> [pre-basename, basename] or nil
2 changes: 2 additions & 0 deletions test/jruby.1.8.index
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ test_ivar_table_integrity
test_io
test_line_endings
test_load
test_load_gem_extensions
test_method
test_method_cache
test_method_override_and_caching
@@ -103,6 +104,7 @@ test_jruby_ext
test_jruby_core_ext
test_thread_context_frame_dereferences_unreachable_variables
test_context_classloader
test_uri_classloader
test_rexml_document
test_load_compiled_ruby
test_openssl_stub
2 changes: 2 additions & 0 deletions test/jruby.1.9.index
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ test_integer_overflows
test_ivar_table_integrity
test_io
test_load
test_load_gem_extensions
test_method
test_method_cache
test_method_override_and_caching
@@ -90,6 +91,7 @@ test_jruby_ext
test_jruby_core_ext
test_thread_context_frame_dereferences_unreachable_variables
test_context_classloader
test_uri_classloader
test_rexml_document
test_load_compiled_ruby
test_openssl_stub
18 changes: 18 additions & 0 deletions test/test_file.rb
Original file line number Diff line number Diff line change
@@ -48,6 +48,24 @@ def test_basename
assert_equal("/", File.basename("/"))
end

def test_basename_uri_like_paths
assert_equal('uri:classloader:/', File.basename('uri:classloader:/'))
assert_equal('uri:classloader://', File.basename('uri:classloader://'))
assert_equal('uri:file:/', File.basename('uri:file:/'))
assert_equal('uri:file://', File.basename('uri:file://'))
assert_equal('classpath:/', File.basename('classpath:/'))
assert_equal('classpath://', File.basename('classpath://'))
assert_equal('file:/', File.basename('file:/'))
assert_equal('file://', File.basename('file://'))
assert_equal('jar:file:/my.jar!/', File.basename('jar:file:/my.jar!/'))
assert_equal('jar:file://my.jar!/', File.basename('jar:file://my.jar!/'))
assert_equal('jar:/my.jar!/', File.basename('jar:/my.jar!/'))
assert_equal('jar://my.jar!/', File.basename('jar://my.jar!/'))
assert_equal('file:/my.jar!/', File.basename('file:/my.jar!/'))
assert_equal('file://my.jar!/', File.basename('file://my.jar!/'))
assert_equal('my.jar!/', File.basename('my.jar!/'))
end

# Added to add more flexibility on windows. Depending on subsystem (msys,
# cygwin, cmd) environment sometimes we have mixed case drive letters. Since
# windows is still case insensitive, downcasing seems a simple solution.
38 changes: 38 additions & 0 deletions test/test_pathname.rb
Original file line number Diff line number Diff line change
@@ -21,4 +21,42 @@ def test_realpath_symlink
File.delete(link) if link && File.exists?(link)
end
end

def test_root_and_absolute
[:root?, :absolute?].each do |method|
assert Pathname.new('uri:classloader:/').send method
assert Pathname.new('uri:classloader://').send method
assert Pathname.new('uri:file:/').send method
assert Pathname.new('uri:file://').send method
assert Pathname.new('classpath:/').send method
assert Pathname.new('classpath://').send method
assert Pathname.new('file:/').send method
assert Pathname.new('file://').send method
assert Pathname.new('jar:file:/my.jar!/').send method
assert Pathname.new('jar:file://my.jar!/').send method
assert Pathname.new('jar:/my.jar!/').send method
assert Pathname.new('jar://my.jar!/').send method
assert Pathname.new('file:/my.jar!/').send method
assert Pathname.new('file://my.jar!/').send method
assert Pathname.new('my.jar!/').send method
end
end

def test_absolute
assert Pathname.new('uri:classloader:/asd').absolute?
assert Pathname.new('uri:classloader://asd').absolute?
assert Pathname.new('uri:file:/asd').absolute?
assert Pathname.new('uri:file://asd').absolute?
assert Pathname.new('classpath:/asd').absolute?
assert Pathname.new('classpath://asd').absolute?
assert Pathname.new('file:/asd').absolute?
assert Pathname.new('file://asd').absolute?
assert Pathname.new('jar:file:/my.jar!/asd').absolute?
assert Pathname.new('jar:file://my.jar!/asd').absolute?
assert Pathname.new('jar:/my.jar!/asd').absolute?
assert Pathname.new('jar://my.jar!/asd').absolute?
assert Pathname.new('file:/my.jar!/asd').absolute?
assert Pathname.new('file://my.jar!/asd').absolute?
assert Pathname.new('my.jar!/asd').absolute?
end
end
3 changes: 2 additions & 1 deletion test/test_uri_classloader.rb
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@
require 'test/unit'
require 'test/test_helper'

class TestFile < Test::Unit::TestCase
# TODO fails with enableassertion :(
class TestURIClassloader < Test::Unit::TestCase
include TestHelper

def setup