From cd10d102c736274056425f6a1ab6d83da1055c80 Mon Sep 17 00:00:00 2001 From: Robert Haines Date: Sat, 24 Feb 2024 08:41:39 +0000 Subject: [PATCH] Fix `require`s to isolate all files as much as possible. --- lib/zip-container.rb | 16 +--------------- lib/zip-container/container.rb | 5 ++++- lib/zip-container/dir.rb | 4 +++- lib/zip-container/entries/directory.rb | 6 +++++- lib/zip-container/entries/entry.rb | 4 +++- lib/zip-container/entries/file.rb | 4 +++- lib/zip-container/entries/managed.rb | 16 ++++++++++++---- lib/zip-container/entries/reserved.rb | 4 +++- lib/zip-container/errors.rb | 4 +++- lib/zip-container/file.rb | 3 +++ lib/zip-container/util.rb | 5 ++++- test/create_dir_test.rb | 5 +++-- test/create_file_test.rb | 5 +++-- test/errors_test.rb | 3 ++- test/managed_entries_test.rb | 10 +++++++--- test/read_dir_test.rb | 5 +++-- test/read_file_test.rb | 5 +++-- test/reserved_names_test.rb | 8 ++++++-- test/util_test.rb | 5 +++-- 19 files changed, 74 insertions(+), 43 deletions(-) diff --git a/lib/zip-container.rb b/lib/zip-container.rb index 1ae6220..55d06c9 100644 --- a/lib/zip-container.rb +++ b/lib/zip-container.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013, 2014 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,20 +30,6 @@ # # Author: Robert Haines -require 'rubygems' -require 'bundler/setup' - -require 'zip/filesystem' - -require 'zip-container/util' -require 'zip-container/version' -require 'zip-container/errors' -require 'zip-container/entries/reserved' -require 'zip-container/entries/managed' -require 'zip-container/entries/entry' -require 'zip-container/entries/file' -require 'zip-container/entries/directory' -require 'zip-container/container' require 'zip-container/file' require 'zip-container/dir' diff --git a/lib/zip-container/container.rb b/lib/zip-container/container.rb index 115a82b..fdb06f0 100644 --- a/lib/zip-container/container.rb +++ b/lib/zip-container/container.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2014, 2015 The University of Manchester, UK. +# Copyright (c) 2014-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,9 @@ # # Author: Robert Haines +require_relative 'entries/managed' +require_relative 'entries/reserved' + ## module ZipContainer # The superclass of anything that represents a Zip Container. That diff --git a/lib/zip-container/dir.rb b/lib/zip-container/dir.rb index ff66e81..fec1314 100644 --- a/lib/zip-container/dir.rb +++ b/lib/zip-container/dir.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2014 The University of Manchester, UK. +# Copyright (c) 2014-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,8 @@ # # Author: Robert Haines +require_relative 'container' + require 'forwardable' module ZipContainer diff --git a/lib/zip-container/entries/directory.rb b/lib/zip-container/entries/directory.rb index 8262ecb..7220f5f 100644 --- a/lib/zip-container/entries/directory.rb +++ b/lib/zip-container/entries/directory.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2015 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,10 @@ # # Author: Robert Haines +require_relative 'entry' +require_relative 'managed' +require_relative 'reserved' + ## module ZipContainer # A ManagedDirectory acts as the interface to a set of (possibly) managed diff --git a/lib/zip-container/entries/entry.rb b/lib/zip-container/entries/entry.rb index 73add26..3106e50 100644 --- a/lib/zip-container/entries/entry.rb +++ b/lib/zip-container/entries/entry.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,8 @@ # # Author: Robert Haines +require_relative '../util' + ## module ZipContainer # ManagedEntry is the superclass of ManagedDirectory and ManagedFile. It diff --git a/lib/zip-container/entries/file.rb b/lib/zip-container/entries/file.rb index b622ad5..db37cc1 100644 --- a/lib/zip-container/entries/file.rb +++ b/lib/zip-container/entries/file.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2015 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,8 @@ # # Author: Robert Haines +require_relative 'entry' + ## module ZipContainer # A ManagedFile is used to reserve a filename in a Container namespace. diff --git a/lib/zip-container/entries/managed.rb b/lib/zip-container/entries/managed.rb index 43148fa..91b010d 100644 --- a/lib/zip-container/entries/managed.rb +++ b/lib/zip-container/entries/managed.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013, 2014 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,11 @@ # # Author: Robert Haines +require_relative 'entry' +require_relative 'file' +require_relative '../errors' +require_relative '../util' + ## module ZipContainer # This module provides support for managed file and directory entries. @@ -203,15 +208,18 @@ def initialize_managed_entries(entries = []) # the container namespace and act as an interface to the (possibly) # managed files within it. def register_managed_entry(entry) - unless entry.is_a?(ManagedDirectory) || entry.is_a?(ManagedFile) + unless entry.is_a?(ManagedEntry) raise ArgumentError, 'The supplied entry must be of type ' \ 'ManagedDirectory or ManagedFile or a subclass of either.' end entry.parent = self - @directories[entry.name] = entry if entry.is_a? ManagedDirectory - @files[entry.name] = entry if entry.is_a? ManagedFile + if entry.is_a?(ManagedFile) + @files[entry.name] = entry + else + @directories[entry.name] = entry + end end private diff --git a/lib/zip-container/entries/reserved.rb b/lib/zip-container/entries/reserved.rb index e8dbf9e..85c41c6 100644 --- a/lib/zip-container/entries/reserved.rb +++ b/lib/zip-container/entries/reserved.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013, 2014 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,8 @@ # # Author: Robert Haines +require_relative '../util' + ## module ZipContainer # This module provides support for reserved names. diff --git a/lib/zip-container/errors.rb b/lib/zip-container/errors.rb index b314e46..3144776 100644 --- a/lib/zip-container/errors.rb +++ b/lib/zip-container/errors.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2015 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,8 @@ # # Author: Robert Haines +require 'zip/errors' + ## module ZipContainer # The base of all errors raised by this library. diff --git a/lib/zip-container/file.rb b/lib/zip-container/file.rb index 822c31e..ce2c36a 100644 --- a/lib/zip-container/file.rb +++ b/lib/zip-container/file.rb @@ -30,6 +30,9 @@ # # Author: Robert Haines +require_relative 'container' + +require 'zip/filesystem' require 'forwardable' module ZipContainer diff --git a/lib/zip-container/util.rb b/lib/zip-container/util.rb index dc41690..56f4416 100644 --- a/lib/zip-container/util.rb +++ b/lib/zip-container/util.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2014 The University of Manchester, UK. +# Copyright (c) 2014-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,6 +30,9 @@ # # Author: Robert Haines +require 'zip' # Remove this when we update to rubyzip 3.x. +require 'zip/entry' + ## module ZipContainer # Utility methods useful throughout the rest of the ZipContainer library. diff --git a/test/create_dir_test.rb b/test/create_dir_test.rb index 44dd5ae..249599a 100644 --- a/test/create_dir_test.rb +++ b/test/create_dir_test.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2014-2023 The University of Manchester, UK. +# Copyright (c) 2014-2024 The University of Manchester, UK. # # All rights reserved. # @@ -31,8 +31,9 @@ # Author: Robert Haines require_relative 'test_helper' + require 'tmpdir' -require 'zip-container' +require 'zip-container/dir' class TestCreateDir < Minitest::Test def test_create_container diff --git a/test/create_file_test.rb b/test/create_file_test.rb index 2ca1996..990c998 100644 --- a/test/create_file_test.rb +++ b/test/create_file_test.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2023 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -31,8 +31,9 @@ # Author: Robert Haines require_relative 'test_helper' + require 'tmpdir' -require 'zip-container' +require 'zip-container/file' class TestCreateFile < Minitest::Test # Check creation of standard empty container files. diff --git a/test/errors_test.rb b/test/errors_test.rb index a3c2dc7..7340fb7 100644 --- a/test/errors_test.rb +++ b/test/errors_test.rb @@ -31,7 +31,8 @@ # Author: Robert Haines require_relative 'test_helper' -require 'zip-container' + +require 'zip-container/errors' class TestExceptions < Minitest::Test def test_rescue_container_errors diff --git a/test/managed_entries_test.rb b/test/managed_entries_test.rb index 9433acb..cb8b360 100644 --- a/test/managed_entries_test.rb +++ b/test/managed_entries_test.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2023 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -30,10 +30,14 @@ # # Author: Robert Haines +require_relative 'helpers/entry_lists' require_relative 'test_helper' + require 'tmpdir' -require 'zip-container' -require_relative 'helpers/entry_lists' +require 'zip-container/file' +require 'zip-container/dir' +require 'zip-container/entries/file' +require 'zip-container/entries/directory' # Classes to test managed entries. class ManagedZipContainer < ZipContainer::File diff --git a/test/read_dir_test.rb b/test/read_dir_test.rb index c3c86f7..7a02466 100644 --- a/test/read_dir_test.rb +++ b/test/read_dir_test.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2014-2023 The University of Manchester, UK. +# Copyright (c) 2014-2024 The University of Manchester, UK. # # All rights reserved. # @@ -31,9 +31,10 @@ # Author: Robert Haines require_relative 'test_helper' + require 'tmpdir' require 'os' -require 'zip-container' +require 'zip-container/dir' class TestReadDir < Minitest::Test # Check that the empty directory does not verify. diff --git a/test/read_file_test.rb b/test/read_file_test.rb index 31b9d27..58817b3 100644 --- a/test/read_file_test.rb +++ b/test/read_file_test.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2023 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -31,7 +31,8 @@ # Author: Robert Haines require_relative 'test_helper' -require 'zip-container' + +require 'zip-container/file' class TestReadFile < Minitest::Test # Check that the null file does not verify. diff --git a/test/reserved_names_test.rb b/test/reserved_names_test.rb index 68fe0bc..baa01db 100644 --- a/test/reserved_names_test.rb +++ b/test/reserved_names_test.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2023 The University of Manchester, UK. +# Copyright (c) 2013-2024 The University of Manchester, UK. # # All rights reserved. # @@ -31,7 +31,11 @@ # Author: Robert Haines require_relative 'test_helper' -require 'zip-container' + +require 'zip-container/errors' +require 'zip-container/file' +require 'zip-container/entries/directory' +require 'zip-container/entries/file' # A class to test the overriding of reserved and managed names. class NewZipContainer < ZipContainer::File diff --git a/test/util_test.rb b/test/util_test.rb index f7199b4..63bf057 100644 --- a/test/util_test.rb +++ b/test/util_test.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2014-2023 The University of Manchester, UK. +# Copyright (c) 2014-2024 The University of Manchester, UK. # # All rights reserved. # @@ -31,8 +31,9 @@ # Author: Robert Haines require_relative 'test_helper' + +require 'zip-container/util' require 'uri' -require 'zip-container' class Util include ZipContainer::Util