2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
7 require File.join(File.expand_path(File.dirname(__FILE__)),
8 'gem_package_tar_test_case')
9 require 'rubygems/package/tar_writer'
11 class TestTarWriter < TarTestCase
18 @tar_writer = Gem::Package::TarWriter.new @io
22 @tar_writer.close unless @tar_writer.closed?
28 @tar_writer.add_file 'x', 0644 do |f| f.write 'a' * 10 end
30 assert_headers_equal(tar_file_header('x', '', 0644, 10),
32 assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
33 assert_equal 1024, @io.pos
36 def test_add_file_simple
37 @tar_writer.add_file_simple 'x', 0644, 10 do |io| io.write "a" * 10 end
39 assert_headers_equal(tar_file_header('x', '', 0644, 10),
42 assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
43 assert_equal 1024, @io.pos
46 def test_add_file_simple_padding
47 @tar_writer.add_file_simple 'x', 0, 100
49 assert_headers_equal tar_file_header('x', '', 0, 100),
52 assert_equal "\0" * 512, @io.string[512, 512]
55 def test_add_file_simple_data
56 @tar_writer.add_file_simple("lib/foo/bar", 0, 10) { |f| f.write @data }
59 assert_equal @data + ("\0" * (512-@data.size)),
63 def test_add_file_simple_size
64 assert_raise Gem::Package::TarWriter::FileOverflow do
65 @tar_writer.add_file_simple("lib/foo/bar", 0, 10) do |io|
71 def test_add_file_unseekable
72 assert_raise Gem::Package::NonSeekableIO do
73 Gem::Package::TarWriter.new(Object.new).add_file 'x', 0
80 assert_equal "\0" * 1024, @io.string
82 e = assert_raise IOError do
85 assert_equal 'closed Gem::Package::TarWriter', e.message
87 e = assert_raise IOError do
90 assert_equal 'closed Gem::Package::TarWriter', e.message
92 e = assert_raise IOError do
93 @tar_writer.add_file 'x', 0
95 assert_equal 'closed Gem::Package::TarWriter', e.message
97 e = assert_raise IOError do
98 @tar_writer.add_file_simple 'x', 0, 0
100 assert_equal 'closed Gem::Package::TarWriter', e.message
102 e = assert_raise IOError do
103 @tar_writer.mkdir 'x', 0
105 assert_equal 'closed Gem::Package::TarWriter', e.message
109 @tar_writer.mkdir 'foo', 0644
111 assert_headers_equal tar_dir_header('foo', '', 0644),
113 assert_equal 512, @io.pos
117 assert_equal ['b' * 100, 'a' * 155],
118 @tar_writer.split_name("#{'a' * 155}/#{'b' * 100}")
120 assert_equal ["#{'qwer/' * 19}bla", 'a' * 151],
121 @tar_writer.split_name("#{'a' * 151}/#{'qwer/' * 19}bla")
124 def test_split_name_too_long_name
125 name = File.join 'a', 'b' * 100
126 assert_equal ['b' * 100, 'a'], @tar_writer.split_name(name)
128 assert_raise Gem::Package::TooLongFileName do
129 name = File.join 'a', 'b' * 101
130 @tar_writer.split_name name
134 def test_split_name_too_long_prefix
135 name = File.join 'a' * 155, 'b'
136 assert_equal ['b', 'a' * 155], @tar_writer.split_name(name)
138 assert_raise Gem::Package::TooLongFileName do
139 name = File.join 'a' * 156, 'b'
140 @tar_writer.split_name name
144 def test_split_name_too_long_total
145 assert_raise Gem::Package::TooLongFileName do
146 @tar_writer.split_name 'a' * 257