Define the EntryExistsError message within the error class.
This commit is contained in:
parent
04cc10a80f
commit
7097492dc8
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
module Zip
|
module Zip
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
class EntryExistsError < Error; end
|
|
||||||
class DestinationFileExistsError < Error; end
|
class DestinationFileExistsError < Error; end
|
||||||
class EntryNameError < Error; end
|
class EntryNameError < Error; end
|
||||||
class EntrySizeError < Error; end
|
class EntrySizeError < Error; end
|
||||||
|
@ -33,6 +32,18 @@ module Zip
|
||||||
end
|
end
|
||||||
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
|
class SplitArchiveError < Error
|
||||||
def message
|
def message
|
||||||
'Rubyzip cannot extract from split archives at this time.'
|
'Rubyzip cannot extract from split archives at this time.'
|
||||||
|
|
|
@ -362,15 +362,12 @@ module Zip
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_entry_exists(entry_name, continue_on_exists_proc, proc_name)
|
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)
|
return unless @cdir.include?(entry_name)
|
||||||
|
|
||||||
if continue_on_exists_proc.call
|
continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
|
||||||
remove get_entry(entry_name)
|
raise ::Zip::EntryExistsError.new proc_name, entry_name unless continue_on_exists_proc.call
|
||||||
else
|
|
||||||
raise ::Zip::EntryExistsError,
|
remove get_entry(entry_name)
|
||||||
proc_name + " failed. Entry #{entry_name} already exists"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_file(path)
|
def check_file(path)
|
||||||
|
|
|
@ -40,9 +40,10 @@ class ZipCaseSensitivityTest < MiniTest::Test
|
||||||
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)
|
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) }
|
SRC_FILES.each { |fn, en| zf.add(en, fn) }
|
||||||
end
|
end
|
||||||
|
assert_match(/'add'/, error.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ensure that names are treated case insensitively when reading files and +case_insensitive_match = true+
|
# Ensure that names are treated case insensitively when reading files and +case_insensitive_match = true+
|
||||||
|
|
|
@ -389,11 +389,12 @@ class ZipFileTest < MiniTest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_add_existing_entry_name
|
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|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
||||||
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
assert_match(/'add'/, error.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_add_existing_entry_name_replace
|
def test_add_existing_entry_name_replace
|
||||||
|
@ -538,11 +539,12 @@ class ZipFileTest < MiniTest::Test
|
||||||
old_entries = nil
|
old_entries = nil
|
||||||
::Zip::File.open(TEST_ZIP.zip_name) { |zf| old_entries = zf.entries }
|
::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|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
||||||
zf.rename(zf.entries[0], zf.entries[1].name)
|
zf.rename(zf.entries[0], zf.entries[1].name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
assert_match(/'rename'/, error.message)
|
||||||
|
|
||||||
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
||||||
assert_equal(old_entries.sort.map(&:name), zf.entries.sort.map(&:name))
|
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
|
def test_rename_entry_to_existing_entry
|
||||||
entry1, entry2, * = TEST_ZIP.entry_names
|
entry1, entry2, * = TEST_ZIP.entry_names
|
||||||
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
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
|
ensure
|
||||||
zf.close
|
zf.close
|
||||||
end
|
end
|
||||||
|
|
|
@ -46,11 +46,12 @@ class ZipSettingsTest < MiniTest::Test
|
||||||
def test_false_continue_on_exists_proc
|
def test_false_continue_on_exists_proc
|
||||||
Zip.continue_on_exists_proc = false
|
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|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
||||||
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
assert_match(/'add'/, error.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_true_continue_on_exists_proc
|
def test_true_continue_on_exists_proc
|
||||||
|
|
Loading…
Reference in New Issue