Define the EntryExistsError message within the error class.

This commit is contained in:
Robert Haines 2022-08-14 22:05:42 +01:00
parent 04cc10a80f
commit 7097492dc8
5 changed files with 28 additions and 13 deletions

View File

@ -2,7 +2,6 @@
module Zip
class Error < StandardError; end
class EntryExistsError < Error; end
class DestinationFileExistsError < Error; end
class EntryNameError < Error; end
class EntrySizeError < Error; end
@ -33,6 +32,18 @@ module Zip
end
end
class EntryExistsError < Error
def initialize(source, name)
super()
@source = source
@name = name
end
def message
"'#{@source}' failed. Entry #{@name} already exists."
end
end
class SplitArchiveError < Error
def message
'Rubyzip cannot extract from split archives at this time.'

View File

@ -362,15 +362,12 @@ module Zip
end
def check_entry_exists(entry_name, continue_on_exists_proc, proc_name)
continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
return unless @cdir.include?(entry_name)
if continue_on_exists_proc.call
remove get_entry(entry_name)
else
raise ::Zip::EntryExistsError,
proc_name + " failed. Entry #{entry_name} already exists"
end
continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
raise ::Zip::EntryExistsError.new proc_name, entry_name unless continue_on_exists_proc.call
remove get_entry(entry_name)
end
def check_file(path)

View File

@ -40,9 +40,10 @@ class ZipCaseSensitivityTest < MiniTest::Test
SRC_FILES.each { |fn, _en| assert(::File.exist?(fn)) }
zf = ::Zip::File.new(EMPTY_FILENAME, create: true)
assert_raises Zip::EntryExistsError do
error = assert_raises Zip::EntryExistsError do
SRC_FILES.each { |fn, en| zf.add(en, fn) }
end
assert_match(/'add'/, error.message)
end
# Ensure that names are treated case insensitively when reading files and +case_insensitive_match = true+

View File

@ -389,11 +389,12 @@ class ZipFileTest < MiniTest::Test
end
def test_add_existing_entry_name
assert_raises(::Zip::EntryExistsError) do
error = assert_raises(::Zip::EntryExistsError) do
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
zf.add(zf.entries.first.name, 'test/data/file2.txt')
end
end
assert_match(/'add'/, error.message)
end
def test_add_existing_entry_name_replace
@ -538,11 +539,12 @@ class ZipFileTest < MiniTest::Test
old_entries = nil
::Zip::File.open(TEST_ZIP.zip_name) { |zf| old_entries = zf.entries }
assert_raises(::Zip::EntryExistsError) do
error = assert_raises(::Zip::EntryExistsError) do
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
zf.rename(zf.entries[0], zf.entries[1].name)
end
end
assert_match(/'rename'/, error.message)
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
assert_equal(old_entries.sort.map(&:name), zf.entries.sort.map(&:name))
@ -586,7 +588,10 @@ class ZipFileTest < MiniTest::Test
def test_rename_entry_to_existing_entry
entry1, entry2, * = TEST_ZIP.entry_names
zf = ::Zip::File.new(TEST_ZIP.zip_name)
assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
error = assert_raises(::Zip::EntryExistsError) do
zf.rename(entry1, entry2)
end
assert_match(/'rename'/, error.message)
ensure
zf.close
end

View File

@ -46,11 +46,12 @@ class ZipSettingsTest < MiniTest::Test
def test_false_continue_on_exists_proc
Zip.continue_on_exists_proc = false
assert_raises(::Zip::EntryExistsError) do
error = assert_raises(::Zip::EntryExistsError) do
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
zf.add(zf.entries.first.name, 'test/data/file2.txt')
end
end
assert_match(/'add'/, error.message)
end
def test_true_continue_on_exists_proc