2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2021-05-12 05:41:32 -04:00
|
|
|
require_relative 'package/tar_test_case'
|
2008-03-31 18:40:06 -04:00
|
|
|
require 'rubygems/package'
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
class TestGemPackageTarReader < Gem::Package::TarTestCase
|
2008-03-31 18:40:06 -04:00
|
|
|
def test_each_entry
|
2013-09-14 04:59:02 -04:00
|
|
|
tar = tar_dir_header "foo", "bar", 0, Time.now
|
|
|
|
tar << tar_file_header("bar", "baz", 0, 0, Time.now)
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
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
|
2014-05-26 10:36:22 -04:00
|
|
|
ensure
|
|
|
|
io.close!
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rewind
|
|
|
|
content = ('a'..'z').to_a.join(" ")
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
str =
|
|
|
|
tar_file_header("lib/foo", "", 010644, content.size, Time.now) +
|
|
|
|
content + "\0" * (512 - content.size)
|
2008-03-31 18:40:06 -04:00
|
|
|
str << "\0" * 1024
|
|
|
|
|
2014-05-26 10:36:22 -04:00
|
|
|
io = TempIO.new(str)
|
|
|
|
|
|
|
|
Gem::Package::TarReader.new(io) do |tar_reader|
|
2008-03-31 18:40:06 -04:00
|
|
|
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
|
2014-05-26 10:36:22 -04:00
|
|
|
ensure
|
|
|
|
io.close!
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_seek
|
2013-09-14 04:59:02 -04:00
|
|
|
tar = tar_dir_header "foo", "bar", 0, Time.now
|
|
|
|
tar << tar_file_header("bar", "baz", 0, 0, Time.now)
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
io = TempIO.new tar
|
|
|
|
|
|
|
|
Gem::Package::TarReader.new io do |tar_reader|
|
|
|
|
tar_reader.seek 'baz/bar' do |entry|
|
|
|
|
assert_kind_of Gem::Package::TarReader::Entry, entry
|
|
|
|
|
|
|
|
assert_equal 'baz/bar', entry.full_name
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 0, io.pos
|
|
|
|
end
|
2014-05-26 10:36:22 -04:00
|
|
|
ensure
|
|
|
|
io.close!
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_seek_missing
|
2013-09-14 04:59:02 -04:00
|
|
|
tar = tar_dir_header "foo", "bar", 0, Time.now
|
|
|
|
tar << tar_file_header("bar", "baz", 0, 0, Time.now)
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
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
|
2014-05-26 10:36:22 -04:00
|
|
|
ensure
|
|
|
|
io.close!
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|