Sha256: 42872558c50e21b07a6a1209b38cc8b365035079a8b8cc82e6bc4db4df52cacb

Contents?: true

Size: 1.67 KB

Versions: 32

Compression:

Stored size: 1.67 KB

Contents

require 'test_helper'
require 'ndr_import/helpers/file/zip'
require 'zip'

# Zip file helper tests
class ZipTest < ActiveSupport::TestCase
  # This is a test importer class to test the Zip file helper mixin
  class TestImporter
    include NdrImport::Helpers::File::Zip
  end

  def setup
    @home = SafePath.new('test_space_rw')
    @permanent_test_files = SafePath.new('permanent_test_files')
    @importer = TestImporter.new
  end

  test '.unzip should reject non SafePath arguments' do
    zip = @home.join('imaginary.zip')

    assert_raises ArgumentError do
      @importer.send(:unzip_file, zip.to_s, @home.to_s)
    end

    assert_raises ArgumentError do
      @importer.send(:unzip_file, zip.to_s, @home)
    end

    assert_raises ArgumentError do
      @importer.send(:unzip_file, zip, @home.to_s)
    end
  end

  test '.unzip unzip zip file' do
    zip_name = @home.join('test.zip')

    files = [
      @home.join('f1'),
      @home.join('f2'),
      @home.join('f3')
    ]

    files.each do |fname|
      File.open(fname, 'w') { |f| f.write "test #{fname}" }
    end

    ::Zip::File.open(zip_name, Zip::File::CREATE) do |zipfile|
      files.each do |fname|
        zipfile.add(File.basename(fname.to_s), fname.to_s)
      end
    end

    File.delete(*files)

    files.each do |fname|
      assert !File.exist?(fname)
    end

    assert File.exist?(zip_name)
    dest = @home.join('unziped')

    @importer.send(:unzip_file, zip_name, dest)

    files.each do |fname|
      assert File.exist?(dest.join(File.basename(fname)))
    end

    files.each do |fname|
      File.delete(dest.join(File.basename(fname)))
    end

    File.delete(zip_name)
    FileUtils.rm_r(dest)
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
ndr_import-8.5.0 test/helpers/file/zip_test.rb
ndr_import-8.4.0 test/helpers/file/zip_test.rb
ndr_import-8.3.0 test/helpers/file/zip_test.rb
ndr_import-8.2.0 test/helpers/file/zip_test.rb
ndr_import-8.1.0 test/helpers/file/zip_test.rb
ndr_import-8.0.0 test/helpers/file/zip_test.rb
ndr_import-7.0.0 test/helpers/file/zip_test.rb
ndr_import-6.4.1 test/helpers/file/zip_test.rb
ndr_import-6.4.0 test/helpers/file/zip_test.rb
ndr_import-6.3.0 test/helpers/file/zip_test.rb
ndr_import-6.2.0 test/helpers/file/zip_test.rb
ndr_import-6.1.1 test/helpers/file/zip_test.rb
ndr_import-6.1.0 test/helpers/file/zip_test.rb
ndr_import-6.0.0 test/helpers/file/zip_test.rb
ndr_import-5.1.0 test/helpers/file/zip_test.rb
ndr_import-5.0.0 test/helpers/file/zip_test.rb
ndr_import-4.1.4 test/helpers/file/zip_test.rb
ndr_import-4.1.3 test/helpers/file/zip_test.rb
ndr_import-4.1.2 test/helpers/file/zip_test.rb
ndr_import-4.1.1 test/helpers/file/zip_test.rb