Skip to content

Commit

Permalink
Rename DestinationFileExistsError -> DestinationExistsError.
Browse files Browse the repository at this point in the history
And define the error message within the class.
  • Loading branch information
hainesr committed Aug 16, 2022
1 parent e3f0aec commit 750d372
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
15 changes: 6 additions & 9 deletions lib/zip/entry.rb
Expand Up @@ -689,9 +689,9 @@ def set_time(binary_dos_date, binary_dos_time)

def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exists_proc })
if ::File.exist?(dest_path) && !yield(self, dest_path)
raise ::Zip::DestinationFileExistsError,
"Destination '#{dest_path}' already exists"
raise ::Zip::DestinationExistsError, dest_path
end

::File.open(dest_path, 'wb') do |os|
get_input_stream do |is|
bytes_written = 0
Expand All @@ -718,14 +718,11 @@ 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
else
raise ::Zip::DestinationFileExistsError,
"Cannot create directory '#{dest_path}'. " \
'A file already exists with that name'
end
raise ::Zip::DestinationExistsError, dest_path unless block_given? && yield(self, dest_path)

::FileUtils.rm_f dest_path
end

::FileUtils.mkdir_p(dest_path)
set_extra_attributes_on_path(dest_path)
end
Expand Down
13 changes: 12 additions & 1 deletion lib/zip/errors.rb
Expand Up @@ -2,7 +2,6 @@

module Zip
class Error < StandardError; end
class DestinationFileExistsError < Error; end

class CompressionMethodError < Error
attr_reader :compression_method
Expand Down Expand Up @@ -30,6 +29,18 @@ def message
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
def initialize(source, name)
super()
Expand Down
4 changes: 3 additions & 1 deletion test/file_extract_directory_test.rb
Expand Up @@ -38,7 +38,9 @@ def test_extract_directory_exists_as_dir

def test_extract_directory_exists_as_file
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

def test_extract_directory_exists_as_file_overwrite
Expand Down
3 changes: 2 additions & 1 deletion test/file_extract_test.rb
Expand Up @@ -39,11 +39,12 @@ def test_extract_exists
text = 'written 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|
zf.extract(zf.entries.first, EXTRACTED_FILENAME)
end
end

File.open(EXTRACTED_FILENAME, 'r') do |f|
assert_equal(text, f.read)
end
Expand Down
4 changes: 3 additions & 1 deletion test/settings_test.rb
Expand Up @@ -40,7 +40,9 @@ def test_true_on_exists_proc
def test_false_on_exists_proc
Zip.on_exists_proc = false
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

def test_false_continue_on_exists_proc
Expand Down

0 comments on commit 750d372

Please sign in to comment.