File: file_extract_directory_test.rb

package info (click to toggle)
ruby-zip 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,120 kB
  • sloc: ruby: 9,958; makefile: 23
file content (59 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

require_relative 'test_helper'
require_relative 'helpers/common_zipfile_fixtures'

class ZipFileExtractDirectoryTest < Minitest::Test
  include CommonZipFileFixture

  TEST_OUT_NAME = 'test/data/generated/emptyOutDir'

  def open_zip(&a_proc)
    assert(!a_proc.nil?)
    ::Zip::File.open(TestZipFile::TEST_ZIP4.zip_name, &a_proc)
  end

  def extract_test_dir(&a_proc)
    open_zip do |zf|
      zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &a_proc)
    end
  end

  def setup
    super

    Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME
    FileUtils.rm_f(TEST_OUT_NAME)
  end

  def test_extract_directory
    extract_test_dir
    assert(File.directory?(TEST_OUT_NAME))
  end

  def test_extract_directory_exists_as_dir
    Dir.mkdir TEST_OUT_NAME
    extract_test_dir
    assert(File.directory?(TEST_OUT_NAME))
  end

  def test_extract_directory_exists_as_file
    File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
    assert_raises(::Zip::DestinationExistsError) do
      extract_test_dir
    end
  end

  def test_extract_directory_exists_as_file_overwrite
    File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
    called = false
    extract_test_dir do |entry, dest_path|
      called = true
      assert_equal(File.absolute_path(TEST_OUT_NAME), dest_path)
      assert(entry.directory?)
      true
    end
    assert(called)
    assert(File.directory?(TEST_OUT_NAME))
  end
end