diff --git a/dir.c b/dir.c index a7874bc73d..1670a476ee 100644 --- a/dir.c +++ b/dir.c @@ -2068,10 +2068,11 @@ glob_helper( if (status) return status; } if (match_dir && pathtype == path_directory) { - const char *subpath = path + baselen + (baselen && path[baselen] == '/'); - char *tmp = join_path(subpath, namelen, dirsep, "", 0); + int seplen = (baselen && path[baselen] == '/'); + const char *subpath = path + baselen + seplen; + char *tmp = join_path(subpath, namelen - seplen, dirsep, "", 0); if (!tmp) return -1; - status = glob_call_func(funcs->match, tmp + (baselen ? dirsep : 0), arg, enc); + status = glob_call_func(funcs->match, tmp, arg, enc); GLOB_FREE(tmp); if (status) return status; } diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 5323c764f5..9ac56ec09d 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -11,11 +11,13 @@ class TestDir < Test::Unit::TestCase $VERBOSE = nil @root = File.realpath(Dir.mktmpdir('__test_dir__')) @nodir = File.join(@root, "dummy") + @dirs = [] for i in "a".."z" if i.ord % 2 == 0 FileUtils.touch(File.join(@root, i)) else FileUtils.mkdir(File.join(@root, i)) + @dirs << File.join(i, "") end end end @@ -210,18 +212,38 @@ class TestDir < Test::Unit::TestCase def test_glob_base files = %w[a/foo.c c/bar.c] files.each {|n| File.write(File.join(@root, n), "")} + Dir.mkdir(File.join(@root, "a/dir")) + dirs = @dirs + %w[a/dir/] + dirs.sort! assert_equal(files, Dir.glob("*/*.c", base: @root).sort) assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: ".").sort}) assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.glob("*.c", base: "a").sort}) assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: "").sort}) assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: nil).sort}) + assert_equal(@dirs, Dir.glob("*/", base: @root).sort) + assert_equal(@dirs, Dir.chdir(@root) {Dir.glob("*/", base: ".").sort}) + assert_equal(%w[dir/], Dir.chdir(@root) {Dir.glob("*/", base: "a").sort}) + assert_equal(@dirs, Dir.chdir(@root) {Dir.glob("*/", base: "").sort}) + assert_equal(@dirs, Dir.chdir(@root) {Dir.glob("*/", base: nil).sort}) + assert_equal(dirs, Dir.glob("**/*/", base: @root).sort) + assert_equal(dirs, Dir.chdir(@root) {Dir.glob("**/*/", base: ".").sort}) + assert_equal(%w[dir/], Dir.chdir(@root) {Dir.glob("**/*/", base: "a").sort}) + assert_equal(dirs, Dir.chdir(@root) {Dir.glob("**/*/", base: "").sort}) + assert_equal(dirs, Dir.chdir(@root) {Dir.glob("**/*/", base: nil).sort}) end def test_glob_base_dir files = %w[a/foo.c c/bar.c] files.each {|n| File.write(File.join(@root, n), "")} + Dir.mkdir(File.join(@root, "a/dir")) + dirs = @dirs + %w[a/dir/] + dirs.sort! assert_equal(files, Dir.open(@root) {|d| Dir.glob("*/*.c", base: d)}.sort) - assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("*", base: d)}}) + assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("*.c", base: d)}}) + assert_equal(@dirs, Dir.open(@root) {|d| Dir.glob("*/", base: d).sort}) + assert_equal(%w[dir/], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("*/", base: d).sort}}) + assert_equal(dirs, Dir.open(@root) {|d| Dir.glob("**/*/", base: d).sort}) + assert_equal(%w[dir/], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("**/*/", base: d).sort}}) end def assert_entries(entries, children_only = false)