summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_package_tar_reader.rb
blob: fa2a3c4745182e2d515033900fd947e622b54b7b (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true
require_relative "package/tar_test_case"
require "rubygems/package"

class TestGemPackageTarReader < Gem::Package::TarTestCase
  def test_each_entry
    tar = tar_dir_header "foo", "bar", 0, Time.now
    tar << tar_file_header("bar", "baz", 0, 0, Time.now)

    io = TempIO.new tar

    entries = 0

    Gem::Package::TarReader.new io do |tar_reader|
      tar_reader.each_entry do |entry|
        assert_kind_of Gem::Package::TarReader::Entry, entry

        entries += 1
      end
    end

    assert_equal 2, entries
  ensure
    io.close!
  end

  def test_rewind
    content = ("a".."z").to_a.join(" ")

    str =
      tar_file_header("lib/foo", "", 010644, content.size, Time.now) +
      content + "\0" * (512 - content.size)
    str << "\0" * 1024

    io = TempIO.new(str)

    Gem::Package::TarReader.new(io) do |tar_reader|
      3.times do
        tar_reader.rewind
        i = 0
        tar_reader.each_entry do |entry|
          assert_equal(content, entry.read)
          i += 1
        end
        assert_equal(1, i)
      end
    end
  ensure
    io.close!
  end

  def test_seek
    tar = tar_dir_header "foo", "bar", 0, Time.now
    tar << tar_file_header("bar", "baz", 0, 0, Time.now)

    io = TempIO.new tar

    Gem::Package::TarReader.new io do |tar_reader|
      retval = tar_reader.seek "baz/bar" do |entry|
        assert_kind_of Gem::Package::TarReader::Entry, entry

        assert_equal "baz/bar", entry.full_name
        entry.read
      end

      assert_equal "", retval
      assert_equal 0, io.pos
    end
  ensure
    io.close!
  end

  def test_seek_missing
    tar = tar_dir_header "foo", "bar", 0, Time.now
    tar << tar_file_header("bar", "baz", 0, 0, Time.now)

    io = TempIO.new tar

    Gem::Package::TarReader.new io do |tar_reader|
      tar_reader.seek "nonexistent" do |_entry|
        flunk "entry missing but entry-found block was run"
      end

      assert_equal 0, io.pos
    end
  ensure
    io.close!
  end

  def test_read_in_gem_data
    gem_tar = util_gem_data_tar do |tar|
      tar.add_file "lib/code.rb", 0444 do |io|
        io.write "# lib/code.rb"
      end
    end

    count = 0
    Gem::Package::TarReader.new(gem_tar).each do |entry|
      next unless entry.full_name == "data.tar.gz"

      Zlib::GzipReader.wrap entry do |gzio|
        Gem::Package::TarReader.new(gzio).each do |contents_entry|
          assert_equal "# lib/code.rb", contents_entry.read
          count += 1
        end
      end
    end

    assert_equal 1, count, "should have found one file"
  end

  def test_seek_in_gem_data
    gem_tar = util_gem_data_tar do |tar|
      tar.add_file "lib/code.rb", 0444 do |io|
        io.write "# lib/code.rb"
      end
      tar.add_file "lib/foo.rb", 0444 do |io|
        io.write "# lib/foo.rb"
      end
    end

    count = 0
    Gem::Package::TarReader.new(gem_tar).seek("data.tar.gz") do |entry|
      Zlib::GzipReader.wrap entry do |gzio|
        Gem::Package::TarReader.new(gzio).seek("lib/foo.rb") do |contents_entry|
          assert_equal "# lib/foo.rb", contents_entry.read
          count += 1
        end
      end
    end

    assert_equal 1, count, "should have found one file"
  end
end