From 1673da744d5618eceabbc98b9990ad85b457c766 Mon Sep 17 00:00:00 2001 From: Robert Haines Date: Mon, 30 Apr 2018 19:43:23 +0100 Subject: [PATCH 1/2] Pass glob through from ZipFileNameMapper. Just pass the basic glob straight through to the underlying Zip::File implementation. --- lib/zip/filesystem.rb | 4 ++++ test/filesystem/directory_test.rb | 23 ++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/zip/filesystem.rb b/lib/zip/filesystem.rb index fb64325b..692017ef 100644 --- a/lib/zip/filesystem.rb +++ b/lib/zip/filesystem.rb @@ -573,6 +573,10 @@ def get_output_stream(fileName, permissionInt = nil, &aProc) @zipFile.get_output_stream(expand_to_entry(fileName), permissionInt, &aProc) end + def glob(*args, &block) + @zipFile.glob(*args, &block) + end + def read(fileName) @zipFile.read(expand_to_entry(fileName)) end diff --git a/test/filesystem/directory_test.rb b/test/filesystem/directory_test.rb index ec0a7371..f20a6dc1 100644 --- a/test/filesystem/directory_test.rb +++ b/test/filesystem/directory_test.rb @@ -3,6 +3,7 @@ class ZipFsDirectoryTest < MiniTest::Test TEST_ZIP = 'test/data/generated/zipWithDirs_copy.zip' + GLOB_TEST_ZIP = 'test/data/globTest.zip' def setup FileUtils.cp('test/data/zipWithDirs.zip', TEST_ZIP) @@ -93,11 +94,23 @@ def test_chroot end end - # Globbing not supported yet - # def test_glob - # # test alias []-operator too - # fail "implement test" - # end + def test_glob + globbed_files = [ + 'globTest/foo/bar/baz/foo.txt', + 'globTest/foo.txt', + 'globTest/food.txt' + ] + + ::Zip::File.open(GLOB_TEST_ZIP) do |zf| + zf.dir.glob('**/*.txt') do |f| + assert globbed_files.include?(f.name) + end + + zf.dir.glob('globTest/foo/**/*.txt') do |f| + assert_equal globbed_files[0], f.name + end + end + end def test_open_new ::Zip::File.open(TEST_ZIP) do |zf| From aa6284db7ac4d3d2f708fb262304420e81c8abd3 Mon Sep 17 00:00:00 2001 From: Robert Haines Date: Mon, 30 Apr 2018 20:04:49 +0100 Subject: [PATCH 2/2] When globbing in ZipFSDir, take CWD into account. --- lib/zip/filesystem.rb | 4 ++-- test/filesystem/directory_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/zip/filesystem.rb b/lib/zip/filesystem.rb index 692017ef..81ad1a18 100644 --- a/lib/zip/filesystem.rb +++ b/lib/zip/filesystem.rb @@ -573,8 +573,8 @@ def get_output_stream(fileName, permissionInt = nil, &aProc) @zipFile.get_output_stream(expand_to_entry(fileName), permissionInt, &aProc) end - def glob(*args, &block) - @zipFile.glob(*args, &block) + def glob(pattern, *flags, &block) + @zipFile.glob(expand_to_entry(pattern), *flags, &block) end def read(fileName) diff --git a/test/filesystem/directory_test.rb b/test/filesystem/directory_test.rb index f20a6dc1..f36ede53 100644 --- a/test/filesystem/directory_test.rb +++ b/test/filesystem/directory_test.rb @@ -109,6 +109,11 @@ def test_glob zf.dir.glob('globTest/foo/**/*.txt') do |f| assert_equal globbed_files[0], f.name end + + zf.dir.chdir('globTest/foo') + zf.dir.glob('**/*.txt') do |f| + assert_equal globbed_files[0], f.name + end end end