diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 927e273..0ba0e48 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -12,7 +12,7 @@ jobs: - name: Install and set up ruby uses: ruby/setup-ruby@v1 with: - ruby-version: '2.6' + ruby-version: '3.0' bundler-cache: true - name: Rubocop diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 386de7a..46117a0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,10 +8,10 @@ jobs: fail-fast: false matrix: os: [ubuntu] - ruby: ['2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3', head, jruby, jruby-head, truffleruby, truffleruby-head] + ruby: ['3.0', '3.1', '3.2', '3.3', head, jruby, jruby-head, truffleruby, truffleruby-head] include: - - { os: macos , ruby: '2.5' } - - { os: windows, ruby: '2.5' } + - { os: macos , ruby: '3.0' } + - { os: windows, ruby: '3.0' } # head builds - { os: windows, ruby: ucrt } - { os: windows, ruby: mswin } diff --git a/.rubocop.yml b/.rubocop.yml index 056c516..49bed3d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,7 +8,7 @@ inherit_from: .rubocop_todo.yml # we get errors if our ruby version doesn't match. AllCops: SuggestExtensions: false - TargetRubyVersion: 2.5 + TargetRubyVersion: 3.0 NewCops: enable # Allow this in this file because adding the extra lines is pointless. diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2b3f89b..ff5238c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,6 +6,9 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. +Gemspec/DevelopmentDependencies: + Enabled: false + # Offense count: 7 Lint/MissingSuper: Exclude: diff --git a/Rakefile b/Rakefile index 7329902..f1e8147 100644 --- a/Rakefile +++ b/Rakefile @@ -19,7 +19,7 @@ RDoc::Task.new do |rdoc| rdoc.rdoc_files.include('README.md', 'lib/**/*.rb') rdoc.options << '--markup=markdown' rdoc.options << '--tab-width=2' - rdoc.options << "-t Rubyzip version #{::Zip::VERSION}" + rdoc.options << "-t Rubyzip version #{Zip::VERSION}" end RuboCop::RakeTask.new diff --git a/lib/zip.rb b/lib/zip.rb index b2ccc67..5d42122 100644 --- a/lib/zip.rb +++ b/lib/zip.rb @@ -65,7 +65,7 @@ module Zip @on_exists_proc = false @continue_on_exists_proc = false @sort_entries = false - @default_compression = ::Zlib::DEFAULT_COMPRESSION + @default_compression = Zlib::DEFAULT_COMPRESSION @write_zip64_support = true @warn_invalid_date = true @case_insensitive_match = false diff --git a/lib/zip/central_directory.rb b/lib/zip/central_directory.rb index 53d213f..61be6fd 100644 --- a/lib/zip/central_directory.rb +++ b/lib/zip/central_directory.rb @@ -28,7 +28,7 @@ module Zip mark_dirty :<<, :comment=, :delete - def initialize(entries = EntrySet.new, comment = '') #:nodoc: + def initialize(entries = EntrySet.new, comment = '') # :nodoc: super(dirty_on_create: false) @entry_set = entries.kind_of?(EntrySet) ? entries : EntrySet.new(entries) @comment = comment @@ -39,7 +39,7 @@ module Zip read_central_directory_entries(io) end - def write_to_stream(io) #:nodoc: + def write_to_stream(io) # :nodoc: cdir_offset = io.tell @entry_set.each { |entry| entry.write_c_dir_entry(io) } eocd_offset = io.tell @@ -61,7 +61,7 @@ module Zip @size end - def ==(other) #:nodoc: + def ==(other) # :nodoc: return false unless other.kind_of?(CentralDirectory) @entry_set.entries.sort == other.entries.sort && comment == other.comment @@ -69,7 +69,7 @@ module Zip private - def write_e_o_c_d(io, offset, cdir_size) #:nodoc: + def write_e_o_c_d(io, offset, cdir_size) # :nodoc: tmp = [ END_OF_CD_SIG, 0, # @numberOfThisDisk @@ -84,7 +84,7 @@ module Zip io << @comment end - def write_64_e_o_c_d(io, offset, cdir_size) #:nodoc: + def write_64_e_o_c_d(io, offset, cdir_size) # :nodoc: tmp = [ ZIP64_END_OF_CD_SIG, 44, # size of zip64 end of central directory record (excludes signature and field itself) @@ -110,7 +110,7 @@ module Zip io << tmp.pack('VVQ> 24).chr, ~@key2) end diff --git a/lib/zip/entry.rb b/lib/zip/entry.rb index 38ebcb4..1895923 100644 --- a/lib/zip/entry.rb +++ b/lib/zip/entry.rb @@ -185,12 +185,12 @@ module Zip # Dynamic checkers %w[directory file symlink].each do |k| - define_method "#{k}?" do + define_method :"#{k}?" do file_type_is?(k.to_sym) end end - def name_is_directory? #:nodoc:all + def name_is_directory? # :nodoc:all @name.end_with?('/') end @@ -207,7 +207,7 @@ module Zip ::File.absolute_path(cleanpath.to_s, root).match?(/([A-Z]:)?#{naive}/i) end - def local_entry_offset #:nodoc:all + def local_entry_offset # :nodoc:all local_header_offset + @local_header_size end @@ -223,7 +223,7 @@ module Zip @comment ? @comment.bytesize : 0 end - def calculate_local_header_size #:nodoc:all + def calculate_local_header_size # :nodoc:all LOCAL_ENTRY_STATIC_HEADER_LENGTH + name_size + extra_size end @@ -239,12 +239,12 @@ module Zip "Local header size changed (#{@local_header_size} -> #{new_size})" end - def cdir_header_size #:nodoc:all + def cdir_header_size # :nodoc:all CDIR_ENTRY_STATIC_HEADER_LENGTH + name_size + (@extra ? @extra.c_dir_size : 0) + comment_size end - def next_header_offset #:nodoc:all + def next_header_offset # :nodoc:all local_entry_offset + compressed_size end @@ -266,7 +266,7 @@ module Zip raise "unknown file type #{inspect}" unless directory? || file? || symlink? - __send__("create_#{ftype}", extract_path, &block) + __send__(:"create_#{ftype}", extract_path, &block) self end @@ -275,7 +275,7 @@ module Zip end class << self - def read_c_dir_entry(io) #:nodoc:all + def read_c_dir_entry(io) # :nodoc:all path = if io.respond_to?(:path) io.path else @@ -314,7 +314,7 @@ module Zip @extra_length = buf.unpack('VCCvvvvVVVvv') end - def read_local_entry(io) #:nodoc:all + def read_local_entry(io) # :nodoc:all @dirty = false # No changes at this point. @local_header_offset = io.tell @@ -371,7 +371,7 @@ module Zip @extra ? @extra.local_size : 0].pack('VvvvvvVVVvv') end - def write_local_entry(io, rewrite: false) #:nodoc:all + def write_local_entry(io, rewrite: false) # :nodoc:all prep_local_zip64_extra verify_local_header_size! if rewrite @local_header_offset = io.tell @@ -463,7 +463,7 @@ module Zip end end - def read_c_dir_entry(io) #:nodoc:all + def read_c_dir_entry(io) # :nodoc:all @dirty = false # No changes at this point. static_sized_fields_buf = io.read(::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH) check_c_dir_entry_static_header_length(static_sized_fields_buf) @@ -556,7 +556,7 @@ module Zip ].pack('VCCvvvvvVVVvvvvvVV') end - def write_c_dir_entry(io) #:nodoc:all + def write_c_dir_entry(io) # :nodoc:all prep_cdir_zip64_extra case @fstype @@ -574,7 +574,7 @@ module Zip end unless ft.nil? - @external_file_attributes = (ft << 12 | (@unix_perms & 0o7777)) << 16 + @external_file_attributes = ((ft << 12) | (@unix_perms & 0o7777)) << 16 end end @@ -639,7 +639,7 @@ module Zip if name_is_directory? raise ArgumentError, "entry name '#{newEntry}' indicates directory entry, but " \ - "'#{src_path}' is not a directory" + "'#{src_path}' is not a directory" end :file when 'directory' @@ -649,7 +649,7 @@ module Zip if name_is_directory? raise ArgumentError, "entry name '#{newEntry}' indicates directory entry, but " \ - "'#{src_path}' is not a directory" + "'#{src_path}' is not a directory" end :symlink else @@ -661,7 +661,7 @@ module Zip get_extra_attributes_from_path(@filepath) end - def write_to_zip_output_stream(zip_output_stream) #:nodoc:all + def write_to_zip_output_stream(zip_output_stream) # :nodoc:all if ftype == :directory zip_output_stream.put_next_entry(self) elsif @filepath @@ -749,7 +749,7 @@ module Zip # apply missing data from the zip64 extra information field, if present # (required when file sizes exceed 2**32, but can be used for all files) - def parse_zip64_extra(for_local_header) #:nodoc:all + def parse_zip64_extra(for_local_header) # :nodoc:all return unless zip64? if for_local_header diff --git a/lib/zip/entry_set.rb b/lib/zip/entry_set.rb index 941e92e..cb4700d 100644 --- a/lib/zip/entry_set.rb +++ b/lib/zip/entry_set.rb @@ -61,12 +61,12 @@ module Zip end def glob(pattern, flags = ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH | ::File::FNM_EXTGLOB) - entries.map do |entry| + entries.filter_map do |entry| next nil unless ::File.fnmatch(pattern, entry.name.chomp('/'), flags) yield(entry) if block_given? entry - end.compact + end end protected diff --git a/lib/zip/errors.rb b/lib/zip/errors.rb index c757a8d..5b75e9a 100644 --- a/lib/zip/errors.rb +++ b/lib/zip/errors.rb @@ -43,7 +43,7 @@ module Zip def message "Cannot create file or directory '#{@destination}'. " \ - 'A file already exists with that name.' + 'A file already exists with that name.' end end @@ -111,8 +111,8 @@ module Zip def message "The local header of this entry ('#{@entry.name}') does not contain " \ - 'the correct metadata for `Zip::InputStream` to be able to ' \ - 'uncompress it. Please use `Zip::File` instead of `Zip::InputStream`.' + 'the correct metadata for `Zip::InputStream` to be able to ' \ + 'uncompress it. Please use `Zip::File` instead of `Zip::InputStream`.' end end end diff --git a/lib/zip/extra_field.rb b/lib/zip/extra_field.rb index 95dd5db..31d75d2 100644 --- a/lib/zip/extra_field.rb +++ b/lib/zip/extra_field.rb @@ -21,8 +21,8 @@ module Zip def extra_field_type_unknown(binstr, len, index, local) self['Unknown'] ||= Unknown.new - if !len || len + 4 > binstr[index..-1].bytesize - self['Unknown'].merge(binstr[index..-1], local: local) + if !len || len + 4 > binstr[index..].bytesize + self['Unknown'].merge(binstr[index..], local: local) return end diff --git a/lib/zip/extra_field/generic.rb b/lib/zip/extra_field/generic.rb index 137efe9..ddd8eed 100644 --- a/lib/zip/extra_field/generic.rb +++ b/lib/zip/extra_field/generic.rb @@ -21,17 +21,17 @@ module Zip return false end - [binstr[2, 2].unpack1('v'), binstr[4..-1]] + [binstr[2, 2].unpack1('v'), binstr[4..]] end def to_local_bin s = pack_for_local - self.class.const_get(:HEADER_ID) + [s.bytesize].pack('v') << s + (self.class.const_get(:HEADER_ID) + [s.bytesize].pack('v')) << s end def to_c_dir_bin s = pack_for_c_dir - self.class.const_get(:HEADER_ID) + [s.bytesize].pack('v') << s + (self.class.const_get(:HEADER_ID) + [s.bytesize].pack('v')) << s end end end diff --git a/lib/zip/extra_field/ntfs.rb b/lib/zip/extra_field/ntfs.rb index be1541d..eac99ad 100644 --- a/lib/zip/extra_field/ntfs.rb +++ b/lib/zip/extra_field/ntfs.rb @@ -25,7 +25,7 @@ module Zip size, content = initial_parse(binstr) (size && content) || return - content = content[4..-1] + content = content[4..] tags = parse_tags(content) tag1 = tags[1] @@ -86,7 +86,7 @@ module Zip end def from_ntfs_time(ntfs_time) - ::Zip::DOSTime.at(ntfs_time / WINDOWS_TICK - SEC_TO_UNIX_EPOCH) + ::Zip::DOSTime.at((ntfs_time / WINDOWS_TICK) - SEC_TO_UNIX_EPOCH) end def to_ntfs_time(time) diff --git a/lib/zip/file.rb b/lib/zip/file.rb index 9c66dc1..85a7503 100644 --- a/lib/zip/file.rb +++ b/lib/zip/file.rb @@ -356,7 +356,7 @@ module Zip # This zip is probably a non-empty StringIO. @create = false @cdir.read_from_stream(path_or_io) - elsif !@create && ::File.zero?(@name) + elsif !@create && ::File.empty?(@name) # A file exists, but it is empty, and we've said we're # NOT creating a new zip. raise Error, "File #{@name} has zero size. Did you mean to pass the create flag?" diff --git a/lib/zip/file_split.rb b/lib/zip/file_split.rb index 0f6c694..de5364a 100644 --- a/lib/zip/file_split.rb +++ b/lib/zip/file_split.rb @@ -7,13 +7,7 @@ module Zip DATA_BUFFER_SIZE = 8192 def get_segment_size_for_split(segment_size) - if MIN_SEGMENT_SIZE > segment_size - MIN_SEGMENT_SIZE - elsif MAX_SEGMENT_SIZE < segment_size - MAX_SEGMENT_SIZE - else - segment_size - end + segment_size.clamp(MIN_SEGMENT_SIZE, MAX_SEGMENT_SIZE) end def get_partial_zip_file_name(zip_file_name, partial_zip_file_name) diff --git a/lib/zip/filesystem/dir.rb b/lib/zip/filesystem/dir.rb index a5e1ef9..5ae4ac4 100644 --- a/lib/zip/filesystem/dir.rb +++ b/lib/zip/filesystem/dir.rb @@ -45,8 +45,8 @@ module Zip entries end - def glob(*args, &block) - @mapped_zip.glob(*args, &block) + def glob(...) + @mapped_zip.glob(...) end def foreach(directory_name) diff --git a/lib/zip/filesystem/zip_file_name_mapper.rb b/lib/zip/filesystem/zip_file_name_mapper.rb index 6b9c7f5..77a4e69 100644 --- a/lib/zip/filesystem/zip_file_name_mapper.rb +++ b/lib/zip/filesystem/zip_file_name_mapper.rb @@ -74,7 +74,7 @@ module Zip private def expand_to_entry(path) - expand_path(path)[1..-1] + expand_path(path)[1..] end end end diff --git a/lib/zip/input_stream.rb b/lib/zip/input_stream.rb index f48466c..56ecf4f 100644 --- a/lib/zip/input_stream.rb +++ b/lib/zip/input_stream.rb @@ -144,8 +144,7 @@ module Zip 'A password is required to decode this zip file' end - if @current_entry.incomplete? && @current_entry.compressed_size == 0 \ - && !@complete_entry + if @current_entry.incomplete? && @current_entry.compressed_size == 0 && !@complete_entry raise StreamingError, @current_entry end @@ -166,8 +165,8 @@ module Zip return ::Zip::NullDecompressor if @current_entry.nil? decompressed_size = - if @current_entry.incomplete? && @current_entry.crc == 0 \ - && @current_entry.size == 0 && @complete_entry + if @current_entry.incomplete? && @current_entry.crc == 0 && + @current_entry.size == 0 && @complete_entry @complete_entry.size else @current_entry.size diff --git a/lib/zip/ioextras.rb b/lib/zip/ioextras.rb index fbc77c6..6668896 100644 --- a/lib/zip/ioextras.rb +++ b/lib/zip/ioextras.rb @@ -12,7 +12,7 @@ module Zip def copy_stream_n(ostream, istream, nbytes) toread = nbytes while toread > 0 && !istream.eof? - tr = toread > CHUNK_SIZE ? CHUNK_SIZE : toread + tr = [toread, CHUNK_SIZE].min ostream.write(istream.read(tr, +'')) toread -= tr end diff --git a/lib/zip/ioextras/abstract_input_stream.rb b/lib/zip/ioextras/abstract_input_stream.rb index c132725..ed95e63 100644 --- a/lib/zip/ioextras/abstract_input_stream.rb +++ b/lib/zip/ioextras/abstract_input_stream.rb @@ -76,13 +76,13 @@ module Zip a_sep_string = "#{$INPUT_RECORD_SEPARATOR}#{$INPUT_RECORD_SEPARATOR}" if a_sep_string.empty? buffer_index = 0 - over_limit = (number_of_bytes && @output_buffer.bytesize >= number_of_bytes) + over_limit = number_of_bytes && @output_buffer.bytesize >= number_of_bytes while (match_index = @output_buffer.index(a_sep_string, buffer_index)).nil? && !over_limit buffer_index = [buffer_index, @output_buffer.bytesize - a_sep_string.bytesize].max return @output_buffer.empty? ? nil : flush if input_finished? @output_buffer << produce_input - over_limit = (number_of_bytes && @output_buffer.bytesize >= number_of_bytes) + over_limit = number_of_bytes && @output_buffer.bytesize >= number_of_bytes end sep_index = [ match_index + a_sep_string.bytesize, diff --git a/lib/zip/output_stream.rb b/lib/zip/output_stream.rb index 0c4e93c..095126b 100644 --- a/lib/zip/output_stream.rb +++ b/lib/zip/output_stream.rb @@ -139,8 +139,8 @@ module Zip return unless @current_entry finish - @current_entry.compressed_size = @output_stream.tell - \ - @current_entry.local_header_offset - \ + @current_entry.compressed_size = @output_stream.tell - + @current_entry.local_header_offset - @current_entry.calculate_local_header_size @current_entry.size = @compressor.size @current_entry.crc = @compressor.crc diff --git a/rubyzip.gemspec b/rubyzip.gemspec index 4c55767..f9d85f1 100644 --- a/rubyzip.gemspec +++ b/rubyzip.gemspec @@ -4,7 +4,7 @@ require_relative 'lib/zip/version' Gem::Specification.new do |s| s.name = 'rubyzip' - s.version = ::Zip::VERSION + s.version = Zip::VERSION s.authors = ['Robert Haines', 'John Lees-Miller', 'Alexander Simonov'] s.email = [ 'hainesr@gmail.com', 'jdleesmiller@gmail.com', 'alex@simonov.me' @@ -18,21 +18,22 @@ Gem::Specification.new do |s| s.license = 'BSD-2-Clause' s.metadata = { - 'bug_tracker_uri' => 'https://github.com/rubyzip/rubyzip/issues', - 'changelog_uri' => "https://github.com/rubyzip/rubyzip/blob/v#{s.version}/Changelog.md", - 'documentation_uri' => "https://www.rubydoc.info/gems/rubyzip/#{s.version}", - 'source_code_uri' => "https://github.com/rubyzip/rubyzip/tree/v#{s.version}", - 'wiki_uri' => 'https://github.com/rubyzip/rubyzip/wiki' + 'bug_tracker_uri' => 'https://github.com/rubyzip/rubyzip/issues', + 'changelog_uri' => "https://github.com/rubyzip/rubyzip/blob/v#{s.version}/Changelog.md", + 'documentation_uri' => "https://www.rubydoc.info/gems/rubyzip/#{s.version}", + 'source_code_uri' => "https://github.com/rubyzip/rubyzip/tree/v#{s.version}", + 'wiki_uri' => 'https://github.com/rubyzip/rubyzip/wiki', + 'rubygems_mfa_required' => 'true' } - s.required_ruby_version = '>= 2.5' + s.required_ruby_version = '>= 3.0' - s.add_development_dependency 'minitest', '~> 5.4' - s.add_development_dependency 'rake', '~> 12.3.3' - s.add_development_dependency 'rdoc', '~> 6.4.0' - s.add_development_dependency 'rubocop', '~> 1.12.0' - s.add_development_dependency 'rubocop-performance', '~> 1.10.0' - s.add_development_dependency 'rubocop-rake', '~> 0.5.0' - s.add_development_dependency 'simplecov', '~> 0.18.0' + s.add_development_dependency 'minitest', '~> 5.22.0' + s.add_development_dependency 'rake', '~> 13.1.0' + s.add_development_dependency 'rdoc', '~> 6.6.2' + s.add_development_dependency 'rubocop', '~> 1.61.0' + s.add_development_dependency 'rubocop-performance', '~> 1.20.0' + s.add_development_dependency 'rubocop-rake', '~> 0.6.0' + s.add_development_dependency 'simplecov', '~> 0.22.0' s.add_development_dependency 'simplecov-lcov', '~> 0.8' end diff --git a/samples/example_filesystem.rb b/samples/example_filesystem.rb index 6909848..ec0075e 100755 --- a/samples/example_filesystem.rb +++ b/samples/example_filesystem.rb @@ -7,7 +7,7 @@ require 'zip/filesystem' EXAMPLE_ZIP = 'filesystem.zip' -File.delete(EXAMPLE_ZIP) if File.exist?(EXAMPLE_ZIP) +FileUtils.rm_f(EXAMPLE_ZIP) Zip::File.open(EXAMPLE_ZIP, create: true) do |zf| zf.file.open('file1.txt', 'w') { |os| os.write 'first file1.txt' } diff --git a/samples/write_simple.rb b/samples/write_simple.rb index f793920..84ac138 100755 --- a/samples/write_simple.rb +++ b/samples/write_simple.rb @@ -5,7 +5,7 @@ $LOAD_PATH << '../lib' require 'zip' -::Zip::OutputStream.open('simple.zip') do |zos| +Zip::OutputStream.open('simple.zip') do |zos| zos.put_next_entry 'entry.txt' zos.puts 'Hello world' end diff --git a/test/case_sensitivity_test.rb b/test/case_sensitivity_test.rb index 07e8457..9a4d84f 100644 --- a/test/case_sensitivity_test.rb +++ b/test/case_sensitivity_test.rb @@ -18,7 +18,7 @@ class ZipCaseSensitivityTest < MiniTest::Test def test_add_case_sensitive ::Zip.case_insensitive_match = false - SRC_FILES.each { |fn, _en| assert(::File.exist?(fn)) } + SRC_FILES.each { |(fn, _en)| assert(::File.exist?(fn)) } zf = ::Zip::File.new(EMPTY_FILENAME, create: true) SRC_FILES.each { |fn, en| zf.add(en, fn) } @@ -37,7 +37,7 @@ class ZipCaseSensitivityTest < MiniTest::Test def test_add_case_insensitive ::Zip.case_insensitive_match = true - SRC_FILES.each { |fn, _en| assert(::File.exist?(fn)) } + SRC_FILES.each { |(fn, _en)| assert(::File.exist?(fn)) } zf = ::Zip::File.new(EMPTY_FILENAME, create: true) error = assert_raises Zip::EntryExistsError do @@ -50,7 +50,7 @@ class ZipCaseSensitivityTest < MiniTest::Test def test_add_case_sensitive_read_case_insensitive ::Zip.case_insensitive_match = false - SRC_FILES.each { |fn, _en| assert(::File.exist?(fn)) } + SRC_FILES.each { |(fn, _en)| assert(::File.exist?(fn)) } zf = ::Zip::File.new(EMPTY_FILENAME, create: true) SRC_FILES.each { |fn, en| zf.add(en, fn) } diff --git a/test/deflater_test.rb b/test/deflater_test.rb index fb78220..9e6b1e2 100644 --- a/test/deflater_test.rb +++ b/test/deflater_test.rb @@ -49,7 +49,7 @@ class DeflaterTest < MiniTest::Test private def load_file(filename) - File.open(filename, 'rb', &:read) + File.binread(filename) end def deflate(data, filename) diff --git a/test/encryption_test.rb b/test/encryption_test.rb index 6fd6395..0561281 100644 --- a/test/encryption_test.rb +++ b/test/encryption_test.rb @@ -16,7 +16,7 @@ class EncryptionTest < MiniTest::Test end def test_encrypt - content = File.open(INPUT_FILE1, 'r').read + content = File.read(INPUT_FILE1) test_filename = 'top_secret_file.txt' password = 'swordfish' @@ -58,11 +58,11 @@ class EncryptionTest < MiniTest::Test entry = zis.get_next_entry assert_equal 'file1.txt', entry.name assert_equal 1_327, entry.size - assert_equal ::File.open(INPUT_FILE1, 'r').read, zis.read + assert_equal ::File.read(INPUT_FILE1), zis.read entry = zis.get_next_entry assert_equal 'file2.txt', entry.name assert_equal 41_234, entry.size - assert_equal ::File.open(INPUT_FILE2, 'r').read, zis.read + assert_equal ::File.read(INPUT_FILE2), zis.read end end end diff --git a/test/file_extract_directory_test.rb b/test/file_extract_directory_test.rb index 17d0995..8c70d75 100644 --- a/test/file_extract_directory_test.rb +++ b/test/file_extract_directory_test.rb @@ -22,7 +22,7 @@ class ZipFileExtractDirectoryTest < MiniTest::Test super Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME - File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME + FileUtils.rm_f(TEST_OUT_NAME) end def test_extract_directory diff --git a/test/file_extract_test.rb b/test/file_extract_test.rb index 77da4a6..9e353d1 100644 --- a/test/file_extract_test.rb +++ b/test/file_extract_test.rb @@ -38,7 +38,7 @@ class ZipFileExtractTest < MiniTest::Test def test_extract_exists text = 'written text' - ::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(text) } + ::File.write(EXTRACTED_FILENAME, text) assert_raises(::Zip::DestinationExistsError) do ::Zip::File.open(TEST_ZIP.zip_name) do |zf| @@ -53,7 +53,7 @@ class ZipFileExtractTest < MiniTest::Test def test_extract_exists_overwrite text = 'written text' - ::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(text) } + ::File.write(EXTRACTED_FILENAME, text) called_correctly = false ::Zip::File.open(TEST_ZIP.zip_name) do |zf| @@ -123,9 +123,7 @@ class ZipFileExtractTest < MiniTest::Test assert data.include?(true_size_bytes) data.gsub! true_size_bytes, fake_size_bytes - File.open(fake_zip, 'wb') do |file| - file.write data - end + File.binwrite(fake_zip, data) Dir.chdir tmp do ::Zip::File.open(fake_zip) do |zf| @@ -187,9 +185,7 @@ class ZipFileExtractTest < MiniTest::Test assert data.include?(true_size_bytes) data.gsub! true_size_bytes, fake_size_bytes - File.open(fake_zip, 'wb') do |file| - file.write data - end + File.binwrite(fake_zip, data) Dir.chdir tmp do ::Zip::File.open(fake_zip) do |zf| diff --git a/test/file_split_test.rb b/test/file_split_test.rb index 0e54cc9..859bf93 100644 --- a/test/file_split_test.rb +++ b/test/file_split_test.rb @@ -15,10 +15,10 @@ class ZipFileSplitTest < MiniTest::Test def teardown File.delete(TEST_ZIP.zip_name) - File.delete(UNSPLITTED_FILENAME) if File.exist?(UNSPLITTED_FILENAME) + FileUtils.rm_f(UNSPLITTED_FILENAME) Dir["#{TEST_ZIP.zip_name}.*"].each do |zip_file_name| - File.delete(zip_file_name) if File.exist?(zip_file_name) + FileUtils.rm_f(zip_file_name) end end @@ -33,7 +33,7 @@ class ZipFileSplitTest < MiniTest::Test return if result.nil? - Dir["#{TEST_ZIP.zip_name}.*"].sort.each_with_index do |zip_file_name, index| + Dir["#{TEST_ZIP.zip_name}.*"].each_with_index do |zip_file_name, index| File.open(zip_file_name, 'rb') do |zip_file| zip_file.read([::Zip::SPLIT_FILE_SIGNATURE].pack('V').size) if index.zero? File.open(UNSPLITTED_FILENAME, 'ab') do |file| diff --git a/test/file_test.rb b/test/file_test.rb index b6dd766..b09f78b 100644 --- a/test/file_test.rb +++ b/test/file_test.rb @@ -22,7 +22,7 @@ class ZipFileTest < MiniTest::Test zf.comment = comment end - ::File.open(EMPTY_FILENAME, 'wb') { |file| file.write buffer.string } + ::File.binwrite(EMPTY_FILENAME, buffer.string) zf_read = ::Zip::File.new(EMPTY_FILENAME) assert_equal(comment, zf_read.comment) @@ -105,7 +105,7 @@ class ZipFileTest < MiniTest::Test assert_equal(entry.time, custom_entry_args[:time]) zf.get_output_stream('entry.bin') do |os| - os.write(::File.open('test/data/generated/5entry.zip', 'rb').read) + os.write(::File.binread('test/data/generated/5entry.zip')) end end @@ -113,7 +113,7 @@ class ZipFileTest < MiniTest::Test assert_equal(count + 3, zf.size) assert_equal('Putting stuff in new_entry.txt', zf.read('new_entry.txt')) assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt')) - assert_equal(File.open('test/data/generated/5entry.zip', 'rb').read, zf.read('entry.bin')) + assert_equal(File.binread('test/data/generated/5entry.zip'), zf.read('entry.bin')) end end @@ -693,7 +693,7 @@ class ZipFileTest < MiniTest::Test old_name = zf.entries.first zf.rename(old_name, new_name) buffer = zf.write_buffer(::StringIO.new) - File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string } + File.binwrite(TEST_ZIP.zip_name, buffer.string) zf_read = ::Zip::File.new(TEST_ZIP.zip_name) refute_nil(zf_read.entries.detect { |e| e.name == new_name }) assert_nil(zf_read.entries.detect { |e| e.name == old_name }) diff --git a/test/filesystem/file_nonmutating_test.rb b/test/filesystem/file_nonmutating_test.rb index e6b1f13..46d700b 100644 --- a/test/filesystem/file_nonmutating_test.rb +++ b/test/filesystem/file_nonmutating_test.rb @@ -406,7 +406,7 @@ class FileNonmutatingTest < MiniTest::Test zf.file.foreach('test/data/file1.txt') do |l| # Ruby replaces \n with \r\n automatically on windows - newline = Zip::RUNNING_ON_WINDOWS ? l.gsub(/\r\n/, "\n") : l + newline = Zip::RUNNING_ON_WINDOWS ? l.gsub("\r\n", "\n") : l assert_equal(ref[index], newline) index = index.next end @@ -420,7 +420,7 @@ class FileNonmutatingTest < MiniTest::Test zf.file.foreach('test/data/file1.txt', ' ') do |l| # Ruby replaces \n with \r\n automatically on windows - newline = Zip::RUNNING_ON_WINDOWS ? l.gsub(/\r\n/, "\n") : l + newline = Zip::RUNNING_ON_WINDOWS ? l.gsub("\r\n", "\n") : l assert_equal(ref[index], newline) index = index.next end @@ -486,7 +486,7 @@ class FileNonmutatingTest < MiniTest::Test zip_file = zf.file.readlines('test/data/file1.txt') # Ruby replaces \n with \r\n automatically on windows - zip_file.each { |l| l.gsub!(/\r\n/, "\n") } if Zip::RUNNING_ON_WINDOWS + zip_file.each { |l| l.gsub!("\r\n", "\n") } if Zip::RUNNING_ON_WINDOWS assert_equal(orig_file, zip_file) end @@ -498,7 +498,7 @@ class FileNonmutatingTest < MiniTest::Test # Ruby replaces \n with \r\n automatically on windows zip_file = if Zip::RUNNING_ON_WINDOWS - zf.file.read('test/data/file1.txt').gsub(/\r\n/, "\n") + zf.file.read('test/data/file1.txt').gsub("\r\n", "\n") else zf.file.read('test/data/file1.txt') end diff --git a/test/gentestfiles.rb b/test/gentestfiles.rb index 5082042..75ce4d8 100755 --- a/test/gentestfiles.rb +++ b/test/gentestfiles.rb @@ -20,7 +20,7 @@ class TestFiles class << self def create_test_files - Dir.mkdir 'test/data/generated' unless Dir.exist?('test/data/generated') + FileUtils.mkdir_p 'test/data/generated' ASCII_TEST_FILES.each_with_index do |filename, index| create_random_ascii(filename, 1E4 * (index + 1)) diff --git a/test/output_stream_test.rb b/test/output_stream_test.rb index b87f64f..16254f1 100644 --- a/test/output_stream_test.rb +++ b/test/output_stream_test.rb @@ -29,7 +29,7 @@ class ZipOutputStreamTest < MiniTest::Test zos.comment = TEST_ZIP.comment write_test_zip(zos) end - File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string } + File.binwrite(TEST_ZIP.zip_name, buffer.string) assert_test_zip_contents(TEST_ZIP) end @@ -50,7 +50,7 @@ class ZipOutputStreamTest < MiniTest::Test end tmp_file.rewind - File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write(tmp_file.read) } + File.binwrite(TEST_ZIP.zip_name, tmp_file.read) tmp_file.unlink assert_test_zip_contents(TEST_ZIP) @@ -75,7 +75,7 @@ class ZipOutputStreamTest < MiniTest::Test zos.comment = TEST_ZIP.comment write_test_zip(zos) end - File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string } + File.binwrite(TEST_ZIP.zip_name, buffer.string) assert_test_zip_contents(TEST_ZIP) end diff --git a/test/settings_test.rb b/test/settings_test.rb index 3bf66e4..9e280aa 100644 --- a/test/settings_test.rb +++ b/test/settings_test.rb @@ -12,7 +12,7 @@ class ZipSettingsTest < MiniTest::Test super Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME - File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME + FileUtils.rm_f(TEST_OUT_NAME) end def teardown diff --git a/test/stored_support_test.rb b/test/stored_support_test.rb index 4e0cdad..e1cd181 100644 --- a/test/stored_support_test.rb +++ b/test/stored_support_test.rb @@ -14,11 +14,11 @@ class StoredSupportTest < MiniTest::Test entry = zis.get_next_entry assert_equal 'file1.txt', entry.name assert_equal 1_327, entry.size - assert_equal ::File.open(INPUT_FILE1, 'r').read, zis.read + assert_equal ::File.read(INPUT_FILE1), zis.read entry = zis.get_next_entry assert_equal 'file2.txt', entry.name assert_equal 41_234, entry.size - assert_equal ::File.open(INPUT_FILE2, 'r').read, zis.read + assert_equal ::File.read(INPUT_FILE2), zis.read end end @@ -29,11 +29,11 @@ class StoredSupportTest < MiniTest::Test entry = zis.get_next_entry assert_equal 'file1.txt', entry.name assert_equal 1_327, entry.size - assert_equal ::File.open(INPUT_FILE1, 'r').read, zis.read + assert_equal ::File.read(INPUT_FILE1), zis.read entry = zis.get_next_entry assert_equal 'file2.txt', entry.name assert_equal 41_234, entry.size - assert_equal ::File.open(INPUT_FILE2, 'r').read, zis.read + assert_equal ::File.read(INPUT_FILE2), zis.read end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index c3ab2bb..8f665b1 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,7 +12,7 @@ require 'gentestfiles' TestFiles.create_test_files TestZipFile.create_test_zips -::MiniTest.after_run do +MiniTest.after_run do FileUtils.rm_rf('test/data/generated') end @@ -137,7 +137,7 @@ module CommonZipFileFixture TEST_ZIP.zip_name = 'test/data/generated/5entry_copy.zip' def setup - File.delete(EMPTY_FILENAME) if File.exist?(EMPTY_FILENAME) + FileUtils.rm_f(EMPTY_FILENAME) FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name) end end