Skip to content

Commit 95aed7a

Browse files
Teardown after each test run, don't rely on object finalizers.
1 parent 3ca721e commit 95aed7a

File tree

1 file changed

+14
-36
lines changed

1 file changed

+14
-36
lines changed

test/test_helper.rb

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,54 @@ module Rugged
88
class TestCase < MiniTest::Unit::TestCase
99
# Automatically clean up created fixture repos after each test run
1010
def after_teardown
11-
Rugged::TestCase::FixtureRepo.eager_teardown
11+
Rugged::TestCase::FixtureRepo.teardown
1212
super
1313
end
1414

1515
module FixtureRepo
1616
# Create a new, empty repository.
1717
def self.empty(*args)
1818
path = Dir.mktmpdir("rugged-empty")
19-
Rugged::Repository.init_at(path, *args).tap do |repo|
20-
schedule_cleanup(repo, path)
21-
end
22-
rescue
23-
FileUtils.remove_entry_secure(path)
24-
raise
19+
ensure_cleanup(path)
20+
Rugged::Repository.init_at(path, *args)
2521
end
2622

2723
# Create a repository based on a rugged fixture repo.
2824
def self.from_rugged(name, *args)
2925
path = Dir.mktmpdir("rugged-#{name}")
26+
ensure_cleanup(path)
3027

3128
FileUtils.cp_r(File.join(TestCase::TEST_DIR, "fixtures", name, "."), path)
3229

3330
prepare(path)
3431

3532
Rugged::Repository.new(path, *args).tap do |repo|
36-
schedule_cleanup(repo, path)
3733
rewrite_gitmodules(repo) unless repo.bare?
3834
end
39-
rescue
40-
FileUtils.remove_entry_secure(path)
41-
raise
4235
end
4336

4437
# Create a repository based on a libgit2 fixture repo.
4538
def self.from_libgit2(name, *args)
4639
path = Dir.mktmpdir("rugged-libgit2-#{name}")
40+
ensure_cleanup(path)
4741

4842
FileUtils.cp_r(File.join(TestCase::LIBGIT2_FIXTURE_DIR, name, "."), path)
4943

5044
prepare(path)
5145

5246
Rugged::Repository.new(path, *args).tap do |repo|
53-
schedule_cleanup(repo, path)
5447
rewrite_gitmodules(repo) unless repo.bare?
5548
end
56-
rescue
57-
FileUtils.remove_entry_secure(path)
58-
raise
5949
end
6050

6151
# Create a repository cloned from another Rugged::Repository instance.
6252
def self.clone(repository)
6353
path = Dir.mktmpdir("rugged")
54+
ensure_cleanup(path)
6455

6556
`git clone --quiet -- #{repository.path} #{path}`
6657

67-
Rugged::Repository.new(path).tap do |repo|
68-
schedule_cleanup(repo, path)
69-
end
70-
rescue
71-
FileUtils.remove_entry_secure(path)
72-
raise
58+
Rugged::Repository.new(path)
7359
end
7460

7561
def self.prepare(path)
@@ -104,8 +90,8 @@ def self.rewrite_gitmodules(repo)
10490
# copied repo.
10591
url.strip!
10692
path = Dir.mktmpdir(url)
93+
ensure_cleanup(path)
10794
FileUtils.cp_r(File.join(TestCase::LIBGIT2_FIXTURE_DIR, url, "."), path)
108-
schedule_cleanup(repo, path)
10995

11096
line = "url = #{path}\n"
11197
end
@@ -127,26 +113,18 @@ def self.rewrite_gitmodules(repo)
127113
end
128114
end
129115

130-
def self.finalize_cleanup(path)
131-
proc { FileUtils.remove_entry_secure(path) if File.exist?(path) }
132-
end
133-
134-
# Try to eagerly delete directories containing fixture repos.
135-
def self.eager_teardown
136-
while path = self.directories.pop
137-
FileUtils.remove_entry_secure(path) rescue nil
138-
end
116+
# Delete temp directories that got created
117+
def self.teardown
118+
self.directories.each { |path| FileUtils.remove_entry_secure(path) }
119+
self.directories.clear
139120
end
140121

141122
def self.directories
142123
@directories ||= []
143124
end
144125

145-
# Schedule the given +path+ to be deleted, either when
146-
# +FixtureRepo.eager_teardown+ is called or when the given +repo+
147-
# gets gc'ed.
148-
def self.schedule_cleanup(repo, path)
149-
ObjectSpace.define_finalizer(repo, finalize_cleanup(path))
126+
# Registers the given +path+ to be deleted when #teardown is called.
127+
def self.ensure_cleanup(path)
150128
self.directories << path
151129
end
152130
end

0 commit comments

Comments
 (0)