Skip to content

Commit bc239fe

Browse files
authored
Merge pull request jruby#8654 from danini-the-panini/ds-fix-dir-glob-windows
fix glob dir on windows
2 parents 51fd262 + 03945c6 commit bc239fe

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

core/src/main/java/org/jruby/util/Dir.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,11 +973,22 @@ private static int glob_helper(Ruby runtime, String cwd, byte[] scheme,
973973
}
974974
if ( fnmatch(magic, 0, magic.length, fileBytes, 0, fileBytes.length, flags) == 0 ) {
975975
buf.length(0);
976-
if (scheme != null && SLASH_INDEX == -1) buf.append(scheme);
977976
buf.append(base);
978977
buf.append( isRoot(base) ? EMPTY : SLASH );
979978
buf.append( getBytesInUTF8(file) );
980-
if ( SLASH_INDEX == -1 ) {
979+
boolean dirMatch = false;
980+
if (SLASH_INDEX == end - 1) {
981+
resource = JRubyFile.createResource(runtime, cwd, new String(buf.unsafeBytes(), buf.begin(), buf.length(), enc.getCharset()));
982+
dirMatch = resource.isDirectory();
983+
}
984+
if ( dirMatch || SLASH_INDEX == -1 ) {
985+
if (scheme != null) {
986+
byte[] bufBytes = buf.bytes();
987+
buf.length(0);
988+
buf.append(scheme);
989+
buf.append(bufBytes);
990+
}
991+
if (dirMatch) buf.append(SLASH);
981992
status = func.call(buf.getUnsafeBytes(), 0, buf.getRealSize(), enc, arg);
982993
if ( status != 0 ) break;
983994
continue;
@@ -993,7 +1004,7 @@ private static int glob_helper(Ruby runtime, String cwd, byte[] scheme,
9931004
for ( DirGlobber globber : links ) {
9941005
final ByteList link = globber.link;
9951006
if ( status == 0 ) {
996-
resource = JRubyFile.createResource(runtime, cwd, RubyString.byteListToString(link));
1007+
resource = JRubyFile.createResource(runtime, cwd, new String(link.unsafeBytes(), link.begin(), link.length(), enc.getCharset()));
9971008
if ( resource.isDirectory() ) {
9981009
final int len = link.getRealSize();
9991010
buf.length(0);

test/jruby/test_dir.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ def test_glob_with_blocks
213213
FileUtils.rm_r("testDir_3") rescue nil
214214
end
215215

216+
def test_glob_dir
217+
Dir.mkdir('testDir_4')
218+
Dir.mkdir('testDir_4/a')
219+
Dir.mkdir('testDir_4/b')
220+
FileUtils.touch('testDir_4/file.txt')
221+
assert_equal(Dir.glob('testDir_4/*/'), ['testDir_4/a/', 'testDir_4/b/'])
222+
Dir.chdir("testDir_4") do
223+
assert_equal(Dir.glob('*/'), ['a/', 'b/'])
224+
end
225+
FileUtils.rm_r("testDir_4") rescue nil
226+
end
227+
216228
def test_dir_dot_does_not_throw_exception
217229
# just makes sure this doesn't throw a Java exception
218230
Dir['.']

0 commit comments

Comments
 (0)