test/tc_managed_entries.rb in zip-container-1.1.0 vs test/tc_managed_entries.rb in zip-container-2.0.0

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (c) 2013 The University of Manchester, UK. +# Copyright (c) 2013, 2014 The University of Manchester, UK. # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: @@ -31,48 +31,54 @@ # Author: Robert Haines require 'test/unit' require 'tmpdir' require 'zip-container' +require 'helpers/entry_lists' # Classes to test managed entries. -class ManagedZipContainer < ZipContainer::Container +class ManagedZipContainer < ZipContainer::File private_class_method :new def initialize(filename) super(filename) - register_managed_entry(ZipContainer::ManagedDirectory.new("src", true)) - register_managed_entry(ZipContainer::ManagedDirectory.new("test")) + test_file = ZipContainer::ManagedFile.new("test.txt") + deep_file = ZipContainer::ManagedFile.new("deep.txt") + deep_dir = ZipContainer::ManagedDirectory.new("deep", + :entries => [deep_file]) + register_managed_entry(ZipContainer::ManagedDirectory.new("src", :required => true)) + register_managed_entry(ZipContainer::ManagedDirectory.new("test", + :hidden => true, :entries => [deep_dir, test_file])) register_managed_entry(ZipContainer::ManagedDirectory.new("lib")) - register_managed_entry(ZipContainer::ManagedFile.new("index.html", true)) + register_managed_entry(ZipContainer::ManagedFile.new("index.html", :required => true)) end end -class ExampleZipContainer < ZipContainer::Container +class ExampleZipContainer < ZipContainer::File private_class_method :new def initialize(filename) super(filename) - register_managed_entry(ZipContainer::ManagedDirectory.new("dir", true)) - register_managed_entry(ZipContainer::ManagedFile.new("greeting.txt", true)) + register_managed_entry(ZipContainer::ManagedDirectory.new("dir", :required => true)) + register_managed_entry(ZipContainer::ManagedFile.new("greeting.txt", :required => true)) end end -class ExampleZipContainer2 < ZipContainer::Container +class ExampleZipContainer2 < ZipContainer::File private_class_method :new def initialize(filename) super(filename) valid = Proc.new { |contents| contents.match(/[Hh]ello/) } register_managed_entry(ZipContainer::ManagedFile.new("greeting.txt", - true, valid)) + :required => true, :validation_proc => valid)) end def ExampleZipContainer2.create(filename, &block) super(filename, "application/example+zip", &block) end @@ -115,11 +121,11 @@ def test_create_standard_container Dir.mktmpdir do |dir| filename = File.join(dir, "test.container") assert_nothing_raised do - ZipContainer::Container.create(filename, $mimetype) do |c| + ZipContainer::File.create(filename, $mimetype) do |c| c.mkdir("META-INF") assert(c.file.exists?("META-INF")) c.file.open("META-INF/container.xml", "w") do |f| f.puts "<?xml version=\"1.0\"?>" @@ -127,11 +133,11 @@ assert(c.file.exists?("META-INF/container.xml")) end end assert_nothing_raised(ZipContainer::MalformedContainerError) do - ZipContainer::Container.verify!(filename) + ZipContainer::File.verify!(filename) end end end # Check that a ManagedZipContainer does not verify immediately after @@ -165,16 +171,125 @@ ManagedZipContainer.create(filename, $mimetype) do |c| c.dir.mkdir("src") c.file.open("index.html", "w") do |f| f.puts "<html />" end + + # Test hidden entries before and after creation. + assert_nil(c.find_entry("test", :include_hidden => true)) + assert_nil(c.find_entry("test/test.txt", :include_hidden => true)) + c.dir.mkdir("test") + c.file.open("test/test.txt", "w") do |f| + f.puts "A test!" + end + assert_not_nil(c.find_entry("test", :include_hidden => true)) + assert_not_nil(c.find_entry("test/test.txt", :include_hidden => true)) + + # Test deep hidden entries before and after creation. + assert_nil(c.find_entry("test/deep", :include_hidden => true)) + assert_nil(c.find_entry("test/deep/deep.txt", :include_hidden => true)) + c.dir.mkdir("test/deep") + c.file.open("test/deep/deep.txt", "w") do |f| + f.puts "A deep test!" + end + assert_not_nil(c.find_entry("test/deep", :include_hidden => true)) + assert_not_nil(c.find_entry("test/deep/deep.txt", :include_hidden => true)) end end assert(ManagedZipContainer.verify(filename)) assert_nothing_raised(ZipContainer::MalformedContainerError) do ManagedZipContainer.verify!(filename) end + end + end + + def test_hidden_entries + assert_nothing_raised do + ManagedZipContainer.open($subclass) do |c| + refute(c.hidden_entry?("src")) + refute(c.hidden_file?("src")) + refute(c.hidden_directory?("src")) + + assert(c.hidden_entry?("test")) + assert(c.hidden_directory?("test")) + assert(c.hidden_entry?("test/")) + assert(c.hidden_directory?("test/")) + refute(c.hidden_file?("test")) + + assert(c.hidden_entry?("test/deep")) + assert(c.hidden_directory?("test/deep")) + assert(c.hidden_entry?("test/deep/deep.txt")) + assert(c.hidden_file?("test/deep/deep.txt")) + end + end + end + + def test_find_entry + assert_nothing_raised do + ManagedZipContainer.open($subclass) do |c| + assert_not_nil(c.find_entry("src")) + assert_not_nil(c.find_entry("src", :include_hidden => true)) + + assert_nil(c.find_entry("test")) + assert_nil(c.find_entry("test/test.txt")) + assert_not_nil(c.find_entry("test", :include_hidden => true)) + assert_not_nil(c.find_entry("test/test.txt", :include_hidden => true)) + + assert_nil(c.find_entry("test/deep")) + assert_nil(c.find_entry("test/deep/deep.txt")) + assert_not_nil(c.find_entry("test/deep", :include_hidden => true)) + assert_not_nil(c.find_entry("test/deep/deep.txt", :include_hidden => true)) + end + 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_glob + ManagedZipContainer.open($subclass) do |c| + assert_equal(["index.html"], entry_list_names(c.glob("in*"))) + assert_equal([], c.glob("test/**/*")) + assert_equal(["test/test.txt", "test/deep", "test/deep/deep.txt"], + entry_list_names(c.glob("test/**/*", :include_hidden => true))) + assert_equal(["test/test.txt", "test/deep/deep.txt"], + entry_list_names(c.glob("**/*.TXT", ::File::FNM_CASEFOLD, + :include_hidden => true))) end end def test_create_subclassed_mimetype Dir.mktmpdir do |dir|