Rename DestinationFileExistsError -> DestinationExistsError.

And define the error message within the class.
This commit is contained in:
Robert Haines 2022-08-16 11:13:30 +01:00
parent e3f0aecf93
commit 750d372380
5 changed files with 26 additions and 13 deletions

View File

@ -689,9 +689,9 @@ module Zip
def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exists_proc }) def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exists_proc })
if ::File.exist?(dest_path) && !yield(self, dest_path) if ::File.exist?(dest_path) && !yield(self, dest_path)
raise ::Zip::DestinationFileExistsError, raise ::Zip::DestinationExistsError, dest_path
"Destination '#{dest_path}' already exists"
end end
::File.open(dest_path, 'wb') do |os| ::File.open(dest_path, 'wb') do |os|
get_input_stream do |is| get_input_stream do |is|
bytes_written = 0 bytes_written = 0
@ -718,14 +718,11 @@ module Zip
return if ::File.directory?(dest_path) return if ::File.directory?(dest_path)
if ::File.exist?(dest_path) if ::File.exist?(dest_path)
if block_given? && yield(self, dest_path) raise ::Zip::DestinationExistsError, dest_path unless block_given? && yield(self, dest_path)
::FileUtils.rm_f dest_path ::FileUtils.rm_f dest_path
else
raise ::Zip::DestinationFileExistsError,
"Cannot create directory '#{dest_path}'. " \
'A file already exists with that name'
end
end end
::FileUtils.mkdir_p(dest_path) ::FileUtils.mkdir_p(dest_path)
set_extra_attributes_on_path(dest_path) set_extra_attributes_on_path(dest_path)
end end

View File

@ -2,7 +2,6 @@
module Zip module Zip
class Error < StandardError; end class Error < StandardError; end
class DestinationFileExistsError < Error; end
class CompressionMethodError < Error class CompressionMethodError < Error
attr_reader :compression_method attr_reader :compression_method
@ -30,6 +29,18 @@ module Zip
end end
end end
class DestinationExistsError < Error
def initialize(destination)
super()
@destination = destination
end
def message
"Cannot create file or directory '#{@destination}'. " \
'A file already exists with that name.'
end
end
class EntryExistsError < Error class EntryExistsError < Error
def initialize(source, name) def initialize(source, name)
super() super()

View File

@ -38,7 +38,9 @@ class ZipFileExtractDirectoryTest < MiniTest::Test
def test_extract_directory_exists_as_file def test_extract_directory_exists_as_file
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' } File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
assert_raises(::Zip::DestinationFileExistsError) { extract_test_dir } assert_raises(::Zip::DestinationExistsError) do
extract_test_dir
end
end end
def test_extract_directory_exists_as_file_overwrite def test_extract_directory_exists_as_file_overwrite

View File

@ -39,11 +39,12 @@ class ZipFileExtractTest < MiniTest::Test
text = 'written text' text = 'written text'
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(text) } ::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(text) }
assert_raises(::Zip::DestinationFileExistsError) do assert_raises(::Zip::DestinationExistsError) do
::Zip::File.open(TEST_ZIP.zip_name) do |zf| ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
zf.extract(zf.entries.first, EXTRACTED_FILENAME) zf.extract(zf.entries.first, EXTRACTED_FILENAME)
end end
end end
File.open(EXTRACTED_FILENAME, 'r') do |f| File.open(EXTRACTED_FILENAME, 'r') do |f|
assert_equal(text, f.read) assert_equal(text, f.read)
end end

View File

@ -40,7 +40,9 @@ class ZipSettingsTest < MiniTest::Test
def test_false_on_exists_proc def test_false_on_exists_proc
Zip.on_exists_proc = false Zip.on_exists_proc = false
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' } File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
assert_raises(Zip::DestinationFileExistsError) { extract_test_dir } assert_raises(Zip::DestinationExistsError) do
extract_test_dir
end
end end
def test_false_continue_on_exists_proc def test_false_continue_on_exists_proc