File: gzip_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (97 lines) | stat: -rw-r--r-- 2,775 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
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
require "../../spec_helper"
require "compress/gzip"

private SAMPLE_TIME     = Time.utc(2016, 1, 2)
private SAMPLE_OS       = 4_u8
private SAMPLE_EXTRA    = Bytes[1, 2, 3]
private SAMPLE_NAME     = "foo.txt"
private SAMPLE_COMMENT  = "some comment"
private SAMPLE_CONTENTS = "hello world\nfoo bar"

private def new_sample_io
  io = IO::Memory.new

  Compress::Gzip::Writer.open(io) do |gzip|
    header = gzip.header
    header.modification_time = SAMPLE_TIME
    header.os = SAMPLE_OS
    header.extra = SAMPLE_EXTRA
    header.name = SAMPLE_NAME
    header.comment = SAMPLE_COMMENT

    io.bytesize.should eq(0)
    gzip.flush
    io.bytesize.should_not eq(0)

    gzip.print SAMPLE_CONTENTS
  end

  io.rewind
end

describe Compress::Gzip do
  it "writes and reads to memory" do
    io = new_sample_io

    Compress::Gzip::Reader.open(io) do |gzip|
      header = gzip.header.not_nil!
      header.modification_time.should eq(SAMPLE_TIME)
      header.os.should eq(SAMPLE_OS)
      header.extra.should eq(SAMPLE_EXTRA)
      header.name.should eq(SAMPLE_NAME)
      header.comment.should eq(SAMPLE_COMMENT)

      # Reading zero bytes is OK
      gzip.read(Bytes.empty).should eq(0)

      gzip.gets_to_end.should eq(SAMPLE_CONTENTS)
    end
  end

  it "rewinds" do
    io = new_sample_io

    gzip = Compress::Gzip::Reader.new(io)
    gzip.gets.should eq(SAMPLE_CONTENTS.lines.first)

    gzip.rewind
    gzip.gets_to_end.should eq(SAMPLE_CONTENTS)
  end

  it "reads file with extra fields from file system" do
    File.open(datapath("test.gz")) do |file|
      Compress::Gzip::Reader.open(file) do |gzip|
        header = gzip.header.not_nil!
        header.modification_time.to_utc.should eq(Time.utc(2012, 9, 4, 22, 6, 5))
        header.os.should eq(3_u8)
        header.extra.should eq(Bytes[1, 2, 3, 4, 5])
        header.name.should eq("test.txt")
        header.comment.should eq("happy birthday")
        gzip.gets_to_end.should eq("One\nTwo")
      end
    end
  end

  it "writes and reads file with extra fields" do
    io = IO::Memory.new
    Compress::Gzip::Writer.open(io) do |gzip|
      header = gzip.header
      header.modification_time = Time.utc(2012, 9, 4, 22, 6, 5)
      header.os = 3_u8
      header.extra = Bytes[1, 2, 3, 4, 5]
      header.name = "test.txt"
      header.comment = "happy birthday"
      gzip << "One\nTwo"
    end
    io.rewind
    Compress::Gzip::Reader.open(io) do |gzip|
      header = gzip.header.not_nil!
      header.modification_time.to_utc.should eq(Time.utc(2012, 9, 4, 22, 6, 5))
      header.os.should eq(3_u8)
      header.extra.should eq(Bytes[1, 2, 3, 4, 5])
      header.name.should eq("test.txt")
      header.comment.should eq("happy birthday")
      gzip.gets_to_end.should eq("One\nTwo")
    end
  end
end