Skip to content

Commit

Permalink
consider escaped comas in glob pattern (resolves #1842)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jan 20, 2017
1 parent bfa5cad commit e927035
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/org/jruby/util/Dir.java
Expand Up @@ -385,7 +385,7 @@ public byte next() {

}

public static interface GlobFunc<T> {
public interface GlobFunc<T> {
int call(byte[] ptr, int p, int len, T ary);
}

Expand Down Expand Up @@ -447,7 +447,11 @@ private static int push_braces(POSIX posix, String cwd, List<ByteList> result, G
int i = lbrace;
while (pattern.bytes[i] != '}') {
middleRegionIndex = i + 1;
for(i = middleRegionIndex; i < pattern.end && pattern.bytes[i] != '}' && pattern.bytes[i] != ','; i++) {
for (i = middleRegionIndex; i < pattern.end && pattern.bytes[i] != '}'; i++) {
if (pattern.bytes[i] == ',') {
if (i > pattern.begin && pattern.bytes[i-1] == '\\') continue;
break;
}
if (pattern.bytes[i] == '{') i = pattern.findClosingIndexOf(i); // skip inner braces
}

Expand Down
25 changes: 16 additions & 9 deletions test/test_dir.rb
Expand Up @@ -7,7 +7,7 @@ class TestDir < Test::Unit::TestCase
include TestHelper
WINDOWS = RbConfig::CONFIG['host_os'] =~ /Windows|mswin/

def setup
def setup; require 'fileutils' ; require 'tmpdir'
@save_dir = Dir.pwd
1.upto(5) do |i|
Dir["testDir_#{i}/*"].each do |f|
Expand All @@ -24,10 +24,6 @@ def teardown

# JRUBY-2519
def test_dir_instance_should_not_cache_dir_contents

require 'fileutils'
require 'tmpdir'

testdir = File.join(Dir.tmpdir, Process.pid.to_s)
FileUtils.mkdir_p testdir

Expand Down Expand Up @@ -82,22 +78,33 @@ def test_glob_empty_string
assert_equal([], Dir[''])
end

def test_glob_escaped_comma
result = Dir.glob('{dont\,exist\,./**/*.rb}')
assert_equal 0, result.size
end

def test_glob_double_star
# Test that glob expansion of ** works ok with non-patterns as path
# elements. This used to throw NPE.
Dir.mkdir("testDir_2")
open("testDir_2/testDir_tmp1", "w").close
Dir.glob('./testDir_2/**/testDir_tmp1').each {|f| assert File.exist?(f) }
FileUtils.touch "testDir_2/testDir_tmp1"
result = Dir.glob('./testDir_2/**/testDir_tmp1')
assert_equal 1, result.size
result.each {|f| assert File.exist?(f) }
ensure
FileUtils.rm_r("testDir_2") rescue nil
end

def test_glob_with_blocks
Dir.mkdir("testDir_3")
open("testDir_3/testDir_tmp1", "w").close
FileUtils.touch "testDir_3/testDir_tmp1"
vals = []
glob_val = Dir.glob('./testDir_3/**/*tmp1'){|f| vals << f}
glob_val = Dir.glob('./testDir_3/**/*tmp1') { |f| vals << f }
assert_equal(true, glob_val.nil?)
assert_equal(1, vals.size)
assert_equal(true, File.exists?(vals[0])) unless vals.empty?
ensure
FileUtils.rm_r("testDir_3") rescue nil
end

def test_dir_dot_does_not_throw_exception
Expand Down

0 comments on commit e927035

Please sign in to comment.