Fix Layout/EmptyLineAfterGuardClause cop.

This commit is contained in:
Robert Haines 2020-02-09 13:13:21 +00:00
parent fff2c41d68
commit cfe4972e71
23 changed files with 76 additions and 5 deletions

View File

@ -6,11 +6,6 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
# Offense count: 76
# Cop supports --auto-correct.
Layout/EmptyLineAfterGuardClause:
Enabled: false
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.

View File

@ -141,6 +141,7 @@ module Zip
def get_e_o_c_d(buf) #:nodoc:
sig_index = buf.rindex([END_OF_CDS].pack('V'))
raise Error, 'Zip end of central directory signature not found' unless sig_index
buf = buf.slice!((sig_index + 4)..(buf.bytesize))
def buf.read(count)
@ -166,8 +167,10 @@ module Zip
def get_64_e_o_c_d(buf) #:nodoc:
zip_64_start = buf.rindex([ZIP64_END_OF_CDS].pack('V'))
raise Error, 'Zip64 end of central directory signature not found' unless zip_64_start
zip_64_locator = buf.rindex([ZIP64_EOCD_LOCATOR].pack('V'))
raise Error, 'Zip64 end of central directory signature locator not found' unless zip_64_locator
buf = buf.slice!((zip_64_start + 4)..zip_64_locator)
def buf.read(count)
@ -198,6 +201,7 @@ module Zip
def ==(other) #:nodoc:
return false unless other.kind_of?(CentralDirectory)
@entry_set.entries.sort == other.entries.sort && comment == other.comment
end
end

View File

@ -12,6 +12,7 @@ module Zip
while length.nil? || (buffer.bytesize < length)
break if input_finished?
buffer << produce_input
end

View File

@ -48,6 +48,7 @@ module Zip
def check_name(name)
return unless name.start_with?('/')
raise ::Zip::EntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /"
end
@ -104,6 +105,7 @@ module Zip
def file_type_is?(type)
raise InternalError, "current filetype is unknown: #{inspect}" unless @ftype
@ftype == type
end
@ -124,6 +126,7 @@ module Zip
def name_safe?
cleanpath = Pathname.new(@name).cleanpath
return false unless cleanpath.relative?
root = ::File::SEPARATOR
naive_expanded_path = ::File.join(root, cleanpath.to_s)
::File.absolute_path(cleanpath.to_s, root) == naive_expanded_path
@ -153,6 +156,7 @@ module Zip
# that we didn't change the header size (and thus clobber file data or something)
def verify_local_header_size!
return if @local_header_size.nil?
new_size = calculate_local_header_size
raise Error, "local header size changed (#{@local_header_size} -> #{new_size})" if @local_header_size != new_size
end
@ -255,6 +259,7 @@ module Zip
unless @header_signature == ::Zip::LOCAL_ENTRY_SIGNATURE
raise ::Zip::Error, "Zip local header magic not found at location '#{local_header_offset}'"
end
set_time(@last_mod_date, @last_mod_time)
@name = io.read(@name_length)
@ -274,6 +279,7 @@ module Zip
@extra = ::Zip::ExtraField.new(extra)
end
end
parse_zip64_extra(true)
@local_header_size = calculate_local_header_size
end
@ -360,16 +366,19 @@ module Zip
def check_c_dir_entry_static_header_length(buf)
return if buf.bytesize == ::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH
raise Error, 'Premature end of file. Not enough data for zip cdir entry header'
end
def check_c_dir_entry_signature
return if header_signature == ::Zip::CENTRAL_DIRECTORY_ENTRY_SIGNATURE
raise Error, "Zip local header magic not found at location '#{local_header_offset}'"
end
def check_c_dir_entry_comment_size
return if @comment && @comment.bytesize == @comment_length
raise ::Zip::Error, 'Truncated cdir zip entry header'
end
@ -408,6 +417,7 @@ module Zip
def get_extra_attributes_from_path(path) # :nodoc:
return if Zip::RUNNING_ON_WINDOWS
stat = file_stat(path)
@unix_uid = stat.uid
@unix_gid = stat.gid
@ -494,6 +504,7 @@ module Zip
def ==(other)
return false unless other.class == self.class
# Compares contents of local entry and exposed fields
keys_equal = %w[compression_method crc compressed_size size name extra filepath].all? do |k|
other.__send__(k.to_sym) == __send__(k.to_sym)
@ -635,6 +646,7 @@ module Zip
def create_directory(dest_path)
return if ::File.directory?(dest_path)
if ::File.exist?(dest_path)
if block_given? && yield(self, dest_path)
::FileUtils.rm_f dest_path
@ -659,6 +671,7 @@ module Zip
# (required when file sizes exceed 2**32, but can be used for all files)
def parse_zip64_extra(for_local_header) #:nodoc:all
return if @extra['Zip64'].nil?
if for_local_header
@size, @compressed_size = @extra['Zip64'].parse(@size, @compressed_size)
else
@ -673,6 +686,7 @@ module Zip
# create a zip64 extra information field if we need one
def prep_zip64_extra(for_local_header) #:nodoc:all
return unless ::Zip.write_zip64_support
need_zip64 = @size >= 0xFFFFFFFF || @compressed_size >= 0xFFFFFFFF
need_zip64 ||= @local_header_offset >= 0xFFFFFFFF unless for_local_header
if need_zip64

View File

@ -50,6 +50,7 @@ module Zip
def ==(other)
return false unless other.kind_of?(EntrySet)
@entry_set.values == other.entry_set.values
end
@ -60,6 +61,7 @@ module Zip
def glob(pattern, flags = ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH | ::File::FNM_EXTGLOB)
entries.map do |entry|
next nil unless ::File.fnmatch(pattern, entry.name.chomp('/'), flags)
yield(entry) if block_given?
entry
end.compact

View File

@ -36,6 +36,7 @@ module Zip
def merge(binstr)
return if binstr.empty?
i = 0
while i < binstr.bytesize
id = binstr[i, 2]
@ -54,6 +55,7 @@ module Zip
unless (field_class = ID_MAP.values.find { |k| k.name == name })
raise Error, "Unknown extra field '#{name}'"
end
self[name] = field_class.new
end

View File

@ -19,11 +19,13 @@ module Zip
warn 'WARNING: weird extra field header ID. Skip parsing it.'
return false
end
[binstr[2, 2].unpack('v')[0], binstr[4..-1]]
end
def ==(other)
return false if self.class != other.class
each do |k, v|
return false if v != other[k]
end

View File

@ -19,6 +19,7 @@ module Zip
def merge(binstr)
return if binstr.empty?
size, content = initial_parse(binstr)
(size && content) || return
@ -27,6 +28,7 @@ module Zip
tag1 = tags[1]
return unless tag1
ntfs_mtime, ntfs_atime, ntfs_ctime = tag1.unpack('Q<Q<Q<')
ntfs_mtime && @mtime ||= from_ntfs_time(ntfs_mtime)
ntfs_atime && @atime ||= from_ntfs_time(ntfs_atime)
@ -65,12 +67,14 @@ module Zip
def parse_tags(content)
return {} if content.nil?
tags = {}
i = 0
while i < content.bytesize
tag, size = content[i, 4].unpack('vv')
i += 4
break unless tag && size
value = content[i, size]
i += size
tags[tag] = value

View File

@ -16,9 +16,11 @@ module Zip
def merge(binstr)
return if binstr.empty?
size, content = initial_parse(binstr)
# size: 0 for central directory. 4 for local header
return if !size || size == 0
atime, mtime, uid, gid = content.unpack('VVvv')
@uid ||= uid
@gid ||= gid

View File

@ -44,10 +44,13 @@ module Zip
# Parse the timestamps, in order, based on which flags are set.
return if times[0].nil?
@mtime ||= ::Zip::DOSTime.at(times.shift) unless @flag & MTIME_MASK == 0
return if times[0].nil?
@atime ||= ::Zip::DOSTime.at(times.shift) unless @flag & ATIME_MASK == 0
return if times[0].nil?
@ctime ||= ::Zip::DOSTime.at(times.shift) unless @flag & CTIME_MASK == 0
end

View File

@ -14,9 +14,11 @@ module Zip
def merge(binstr)
return if binstr.empty?
size, content = initial_parse(binstr)
# size: 0 for central directory. 4 for local header
return if !size || size == 0
uid, gid = content.unpack('vv')
@uid ||= uid
@gid ||= gid

View File

@ -26,6 +26,7 @@ module Zip
def merge(binstr)
return if binstr.empty?
_, @content = initial_parse(binstr)
end
@ -52,6 +53,7 @@ module Zip
def pack_for_local
# local header entries must contain original size and compressed size; other fields do not apply
return '' unless @original_size && @compressed_size
[@original_size, @compressed_size].pack('Q<Q<')
end

View File

@ -120,6 +120,7 @@ module Zip
def open(file_name, create = false, options = {})
zf = ::Zip::File.new(file_name, create, false, options)
return zf unless block_given?
begin
yield zf
ensure
@ -151,6 +152,7 @@ module Zip
zf = ::Zip::File.new(io, true, true, options)
return zf unless block_given?
yield zf
begin
@ -229,9 +231,11 @@ module Zip
def split(zip_file_name, segment_size = MAX_SEGMENT_SIZE, delete_zip_file = true, partial_zip_file_name = nil)
raise Error, "File #{zip_file_name} not found" unless ::File.exist?(zip_file_name)
raise Errno::ENOENT, zip_file_name unless ::File.readable?(zip_file_name)
zip_file_size = ::File.size(zip_file_name)
segment_size = get_segment_size_for_split(segment_size)
return if zip_file_size <= segment_size
segment_count = get_segment_count_for_split(zip_file_size, segment_size)
# Checking for correct zip structure
::Zip::File.open(zip_file_name) {}
@ -337,6 +341,7 @@ module Zip
# the zip archive.
def commit
return if name.is_a?(StringIO) || !commit_required?
on_success_replace do |tmp_file|
::Zip::OutputStream.open(tmp_file) do |zos|
@entry_set.each do |e|
@ -402,6 +407,7 @@ module Zip
# Creates a directory
def mkdir(entryName, permissionInt = 0o755)
raise Errno::EEXIST, "File exists - #{entryName}" if find_entry(entryName)
entryName = entryName.dup.to_s
entryName << '/' unless entryName.end_with?('/')
@entry_set << ::Zip::StreamableDirectory.new(@name, entryName, nil, permissionInt)
@ -424,6 +430,7 @@ module Zip
def check_entry_exists(entryName, continue_on_exists_proc, procedureName)
continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
return unless @entry_set.include?(entryName)
if continue_on_exists_proc.call
remove get_entry(entryName)
else

View File

@ -176,6 +176,7 @@ module Zip
unless exists?(fileName)
raise Errno::ENOENT, "No such file or directory - #{fileName}"
end
@mappedZip.find_entry(fileName)
end
private :get_entry
@ -382,6 +383,7 @@ module Zip
def stat(fileName)
raise Errno::ENOENT, fileName unless exists?(fileName)
ZipFsStat.new(self, fileName)
end
@ -408,6 +410,7 @@ module Zip
if directory?(fileName)
raise Errno::EISDIR, "Is a directory - \"#{fileName}\""
end
@mappedZip.remove(fileName)
end
end
@ -462,6 +465,7 @@ module Zip
unless @file.stat(aDirectoryName).directory?
raise Errno::EINVAL, "Invalid argument - #{aDirectoryName}"
end
@mappedZip.pwd = @file.expand_path(aDirectoryName)
end
@ -479,6 +483,7 @@ module Zip
unless @file.stat(aDirectoryName).directory?
raise Errno::ENOTDIR, aDirectoryName
end
path = @file.expand_path(aDirectoryName)
path << '/' unless path.end_with?('/')
path = Regexp.escape(path)
@ -493,6 +498,7 @@ module Zip
unless @file.stat(entryName).directory?
raise Errno::EINVAL, "Invalid argument - #{entryName}"
end
@mappedZip.remove(entryName)
end
alias rmdir delete
@ -521,26 +527,31 @@ module Zip
def each(&aProc)
raise IOError, 'closed directory' if @fileNames.nil?
@fileNames.each(&aProc)
end
def read
raise IOError, 'closed directory' if @fileNames.nil?
@fileNames[(@index += 1) - 1]
end
def rewind
raise IOError, 'closed directory' if @fileNames.nil?
@index = 0
end
def seek(anIntegerPosition)
raise IOError, 'closed directory' if @fileNames.nil?
@index = anIntegerPosition
end
def tell
raise IOError, 'closed directory' if @fileNames.nil?
@index
end
end

View File

@ -12,6 +12,7 @@ module Zip
while length.nil? || (@buffer.bytesize < length)
break if input_finished?
@buffer << produce_input
end
@ -32,6 +33,7 @@ module Zip
@zlib_inflater.inflate(input_stream.read(Decompressor::CHUNK_SIZE))
rescue Zlib::BufError
raise if retried >= 5 # how many times should we retry?
retried += 1
retry
end

View File

@ -73,6 +73,7 @@ module Zip
# Rewinds the stream to the beginning of the current entry
def rewind
return if @current_entry.nil?
@lineno = 0
@pos = 0
@archive_io.seek(@current_entry.local_header_offset, IO::SEEK_SET)
@ -91,6 +92,7 @@ module Zip
def open(filename_or_io, offset = 0, decrypter = nil)
zio = new(filename_or_io, offset, decrypter)
return zio unless block_given?
begin
yield zio
ensure
@ -123,6 +125,7 @@ module Zip
if @current_entry && @current_entry.encrypted? && @decrypter.is_a?(NullEncrypter)
raise Error, 'password required to decode zip file'
end
if @current_entry && @current_entry.incomplete? && @current_entry.crc == 0 \
&& @current_entry.compressed_size == 0 \
&& @current_entry.size == 0 && !@complete_entry

View File

@ -35,6 +35,7 @@ module Zip
if tbuf.nil? || tbuf.empty?
return nil if number_of_bytes
return ''
end
@ -69,6 +70,7 @@ module Zip
end
return read(number_of_bytes) if a_sep_string.nil?
a_sep_string = "#{$/}#{$/}" if a_sep_string.empty?
buffer_index = 0
@ -76,6 +78,7 @@ module Zip
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)
end
@ -97,6 +100,7 @@ module Zip
def readline(a_sep_string = $/)
ret_val = gets(a_sep_string)
raise EOFError unless ret_val
ret_val
end

View File

@ -49,6 +49,7 @@ module Zip
class << self
def open(file_name, encrypter = nil)
return new(file_name) unless block_given?
zos = new(file_name, false, encrypter)
yield zos
ensure
@ -66,6 +67,7 @@ module Zip
# Closes the stream and writes the central directory to the zip file
def close
return if @closed
finalize_current_entry
update_local_headers
write_central_directory
@ -76,6 +78,7 @@ module Zip
# Closes the stream and writes the central directory to the zip file
def close_buffer
return @output_stream if @closed
finalize_current_entry
update_local_headers
write_central_directory
@ -87,6 +90,7 @@ module Zip
# +entry+ can be a ZipEntry object or a string.
def put_next_entry(entry_name, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression)
raise Error, 'zip stream is closed' if @closed
new_entry = if entry_name.kind_of?(Entry)
entry_name
else
@ -105,6 +109,7 @@ module Zip
entry = entry.dup
raise Error, 'zip stream is closed' if @closed
raise Error, 'entry is not a ZipEntry' unless entry.is_a?(Entry)
finalize_current_entry
@entry_set << entry
src_pos = entry.local_header_offset
@ -123,6 +128,7 @@ module Zip
def finalize_current_entry
return unless @current_entry
finish
@current_entry.compressed_size = @output_stream.tell - @current_entry.local_header_offset - @current_entry.calculate_local_header_size
@current_entry.size = @compressor.size

View File

@ -22,6 +22,7 @@ module Zip
unless @temp_file.closed?
raise StandardError, "cannot open entry for reading while its open for writing - #{name}"
end
@temp_file.open # reopens tempfile from top
@temp_file.binmode
if block_given?

View File

@ -13,6 +13,7 @@ module Zip
Find.find(path) do |fileName|
yield(fileName)
next unless zipFilePattern.match(fileName) && File.file?(fileName)
begin
Zip::File.foreach(fileName) do |zipEntry|
yield(fileName + File::SEPARATOR + zipEntry.to_s)

View File

@ -28,6 +28,7 @@ class ZipFileSplitTest < MiniTest::Test
result = ::Zip::File.split(TEST_ZIP.zip_name, 65_536, false)
return if result.nil?
Dir["#{TEST_ZIP.zip_name}.*"].sort.each_with_index do |zip_file_name, index|
File.open(zip_file_name, 'rb') do |zip_file|
zip_file.read([::Zip::File::SPLIT_SIGNATURE].pack('V').size) if index == 0

View File

@ -623,6 +623,7 @@ class ZipFileTest < MiniTest::Test
Zip::File.open_buffer(f) do |zipfile|
zipfile.each do |entry|
next unless entry.name =~ /README.md/
data = zipfile.read(entry)
end
end

View File

@ -52,6 +52,7 @@ class TestFiles
def ensure_dir(name)
if File.exist?(name)
return if File.stat(name).directory?
File.delete(name)
end
Dir.mkdir(name)