Skip to content

Commit cf74b91

Browse files
authored
Simplify how temp files are used when testing Git::Base#archive (#621)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent a8bfb9d commit cf74b91

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

tests/units/test_archive.rb

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,88 @@
33
require 'test_helper'
44

55
class TestArchive < Test::Unit::TestCase
6-
76
def setup
87
clone_working_repo
98
@git = Git.open(@wdir)
10-
@tempfiles = []
11-
end
12-
13-
def teardown
14-
@tempfiles.clear
159
end
1610

1711
def tempfile
18-
tempfile_object = Tempfile.new('archive-test')
19-
@tempfiles << tempfile_object # prevent deletion until teardown
20-
tempfile_object.close # close to avoid locking from git processes
21-
tempfile_object.path
12+
Dir::Tmpname.create('test-archive') { }
2213
end
2314

2415
def test_archive
2516
f = @git.archive('v2.6', tempfile)
2617
assert(File.exist?(f))
18+
File.delete(f)
19+
end
2720

21+
def test_archive_object
2822
f = @git.object('v2.6').archive(tempfile) # writes to given file
2923
assert(File.exist?(f))
24+
File.delete(f)
25+
end
3026

27+
def test_archive_object_with_no_filename
3128
f = @git.object('v2.6').archive # returns path to temp file
3229
assert(File.exist?(f))
30+
File.delete(f)
31+
end
3332

33+
def test_archive_to_tar
3434
f = @git.object('v2.6').archive(nil, :format => 'tar') # returns path to temp file
3535
assert(File.exist?(f))
3636

37-
lines = Minitar::Input.open(f).each.to_a.map(&:full_name)
37+
lines = []
38+
Minitar::Input.open(f) do |tar_reader|
39+
lines = tar_reader.to_a.map(&:full_name)
40+
end
41+
File.delete(f)
42+
3843
assert_match(%r{ex_dir/}, lines[1])
3944
assert_match(/ex_dir\/ex\.txt/, lines[2])
4045
assert_match(/example\.txt/, lines[3])
46+
end
4147

48+
def test_archive_to_zip
4249
f = @git.object('v2.6').archive(tempfile, :format => 'zip')
4350
assert(File.file?(f))
51+
File.delete(f)
52+
end
4453

54+
def test_archive_to_tgz
4555
f = @git.object('v2.6').archive(tempfile, :format => 'tgz', :prefix => 'test/')
4656
assert(File.exist?(f))
4757

48-
lines = Minitar::Input.open(Zlib::GzipReader.new(File.open(f, 'rb'))).each.to_a.map(&:full_name)
58+
lines = []
59+
File.open(f, 'rb') do |file_reader|
60+
Zlib::GzipReader.open(file_reader) do |gz_reader|
61+
Minitar::Input.open(gz_reader) do |tar_reader|
62+
lines = tar_reader.to_a.map(&:full_name)
63+
end
64+
end
65+
end
66+
File.delete(f)
67+
4968
assert_match(%r{test/}, lines[1])
5069
assert_match(%r{test/ex_dir/ex\.txt}, lines[3])
70+
end
5171

72+
def test_archive_with_prefix_and_path
5273
f = @git.object('v2.6').archive(tempfile, :format => 'tar', :prefix => 'test/', :path => 'ex_dir/')
5374
assert(File.exist?(f))
5475

55-
lines = Minitar::Input.open(f).each.to_a.map(&:full_name)
76+
tar_file = Minitar::Input.open(f)
77+
lines = tar_file.each.to_a.map(&:full_name)
78+
tar_file.close
79+
File.delete(f)
80+
5681
assert_match(%r{test/}, lines[1])
5782
assert_match(%r{test/ex_dir/ex\.txt}, lines[3])
83+
end
5884

85+
def test_archive_branch
5986
f = @git.remote('working').branch('master').archive(tempfile, :format => 'tgz')
6087
assert(File.exist?(f))
88+
File.delete(f)
6189
end
62-
6390
end

0 commit comments

Comments
 (0)