Skip to content

Commit

Permalink
Update get_entry to take hidden files into account.
Browse files Browse the repository at this point in the history
  • Loading branch information
hainesr committed Jun 26, 2014
1 parent 178042b commit ce5ba97
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/zip-container/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class File

extend Forwardable
def_delegators :@zipfile, :comment, :comment=, :commit_required?, :each,
:entries, :extract, :get_entry, :get_input_stream, :glob, :name, :read,
:size
:entries, :extract, :get_input_stream, :glob, :name, :read, :size

private_class_method :new

Expand Down Expand Up @@ -255,6 +254,23 @@ def find_entry(entry_name, options = {})
@zipfile.find_entry(entry_name)
end

# :call-seq:
# get_entry(entry, options = {}) -> Zip::Entry or nil
#
# Searches for an entry like find_entry, but throws Errno::ENOENT if no
# entry is found or if the specified entry is hidden for normal use. You
# can specify <tt>:include_hidden => true</tt> to include hidden entries
# in the search.
def get_entry(entry, options = {})
options = {:include_hidden => false}.merge(options)

unless options[:include_hidden]
raise Errno::ENOENT, entry if hidden_entry?(entry)
end

@zipfile.get_entry(entry)
end

# :call-seq:
# get_output_stream(entry, permission = nil) -> stream
# get_output_stream(entry, permission = nil) {|stream| ...}
Expand Down
37 changes: 37 additions & 0 deletions test/tc_managed_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,43 @@ def test_find_entry
end
end

def test_get_entry
ManagedZipContainer.open($subclass) do |c|
assert_nothing_raised(Errno::ENOENT) do
c.get_entry("src")
end
assert_nothing_raised(Errno::ENOENT) do
c.get_entry("src", :include_hidden => true)
end

assert_raise(Errno::ENOENT) do
c.get_entry("test")
end
assert_raise(Errno::ENOENT) do
c.get_entry("test/test.txt")
end
assert_nothing_raised(Errno::ENOENT) do
c.get_entry("test", :include_hidden => true)
end
assert_nothing_raised(Errno::ENOENT) do
c.get_entry("test/test.txt", :include_hidden => true)
end

assert_raise(Errno::ENOENT) do
c.get_entry("test/deep")
end
assert_raise(Errno::ENOENT) do
c.get_entry("test/deep/deep.txt")
end
assert_nothing_raised(Errno::ENOENT) do
c.get_entry("test/deep", :include_hidden => true)
end
assert_nothing_raised(Errno::ENOENT) do
c.get_entry("test/deep/deep.txt", :include_hidden => true)
end
end
end

def test_create_subclassed_mimetype
Dir.mktmpdir do |dir|
filename = File.join(dir, "test.container")
Expand Down

0 comments on commit ce5ba97

Please sign in to comment.