summaryrefslogtreecommitdiff
path: root/spec/zip_spec.cr
blob: 7f539ccee74853913921256a99bb83fd30ac3c4d (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require "./spec_helper"

TEST_DIR = File.dirname(__FILE__)
TEST_FILE_PATH = File.join(TEST_DIR, "..", "src", "zip.cr")

describe Zip do
  # TODO: Write tests

  it "works" do
    Zip::VERSION.should eq(Zip::VERSION)
  end

  ###############
  # write tests #
  ###############

  it "creates an empty archive" do
    Zip.write(File.join(TEST_DIR, "test-empty.zip")) do |zip|
      # do nothing
    end
  end

  it "creates an entry from a String" do
    Zip.write(File.join(TEST_DIR, "test-string.zip")) do |zip|
      zip.add("bar.txt", "bar")
    end
  end

  it "creates an entry from a String with no compression" do
    Zip.write(File.join(TEST_DIR, "test-string-none.zip")) do |zip|
      zip.add(
        path:   "bar.txt",
        data:   "bar",
        method: Zip::CompressionMethod::NONE
      )
    end
  end

  it "creates an entry from a MemoryIO" do
    Zip.write(File.join(TEST_DIR, "test-memio.zip")) do |zip|
      zip.add("bar.txt", "bar")
    end
  end

  it "creates an entry from a File" do
    Zip.write(File.join(TEST_DIR, "test-file.zip")) do |zip|
      zip.add_file("test.cr", TEST_FILE_PATH)
    end
  end

  it "creates an archive from a MemoryIO, String, and File" do
    Zip.write(File.join(TEST_DIR, "test-many.zip")) do |zip|
      zip.add("foo.txt", MemoryIO.new("foo"))
      zip.add("bar.txt", "bar")
      zip.add_file("test.cr", TEST_FILE_PATH)
      zip.add_dir("example-dir")
    end
  end

  ##############
  # read tests #
  ##############

  it "reads an archive" do
    Zip.read(File.join(TEST_DIR, "test-string.zip")) do |zip|
      zip.entries.each do |e|
        pp e.path
      end
    end
  end

  it "reads an archive created by an external program" do
    Zip.read(File.join(TEST_DIR, "real.zip")) do |zip|
      zip.each do |e|
        pp e.path
      end
    end
  end

  it "reads an archive created by an external program" do
    Zip.read(File.join(TEST_DIR, "real.zip")) do |zip|
      zip.each do |e|
        e.write("/dev/null")

        # p e.extras.map { |e| { e.code, e.size } }
        # p e.local_extras.map { |e| { e.code, e.size } }
      end
    end
  end

  it "reads all an archive's compressed entries" do
    Zip.read(File.join(TEST_DIR, "test-many.zip")) do |zip|
      puts "file has #{zip.size} entries"

      zip.each do |e|

        if e.dir?
          puts "#{e.path} is a directory"
        else
          io = MemoryIO.new
          # e.write(STDOUT)
          e.write(io)
          io.close
        end
      end
    end
  end
end