@@ -8,68 +8,54 @@ module Rugged
8
8
class TestCase < MiniTest ::Unit ::TestCase
9
9
# Automatically clean up created fixture repos after each test run
10
10
def after_teardown
11
- Rugged ::TestCase ::FixtureRepo . eager_teardown
11
+ Rugged ::TestCase ::FixtureRepo . teardown
12
12
super
13
13
end
14
14
15
15
module FixtureRepo
16
16
# Create a new, empty repository.
17
17
def self . empty ( *args )
18
18
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 )
25
21
end
26
22
27
23
# Create a repository based on a rugged fixture repo.
28
24
def self . from_rugged ( name , *args )
29
25
path = Dir . mktmpdir ( "rugged-#{ name } " )
26
+ ensure_cleanup ( path )
30
27
31
28
FileUtils . cp_r ( File . join ( TestCase ::TEST_DIR , "fixtures" , name , "." ) , path )
32
29
33
30
prepare ( path )
34
31
35
32
Rugged ::Repository . new ( path , *args ) . tap do |repo |
36
- schedule_cleanup ( repo , path )
37
33
rewrite_gitmodules ( repo ) unless repo . bare?
38
34
end
39
- rescue
40
- FileUtils . remove_entry_secure ( path )
41
- raise
42
35
end
43
36
44
37
# Create a repository based on a libgit2 fixture repo.
45
38
def self . from_libgit2 ( name , *args )
46
39
path = Dir . mktmpdir ( "rugged-libgit2-#{ name } " )
40
+ ensure_cleanup ( path )
47
41
48
42
FileUtils . cp_r ( File . join ( TestCase ::LIBGIT2_FIXTURE_DIR , name , "." ) , path )
49
43
50
44
prepare ( path )
51
45
52
46
Rugged ::Repository . new ( path , *args ) . tap do |repo |
53
- schedule_cleanup ( repo , path )
54
47
rewrite_gitmodules ( repo ) unless repo . bare?
55
48
end
56
- rescue
57
- FileUtils . remove_entry_secure ( path )
58
- raise
59
49
end
60
50
61
51
# Create a repository cloned from another Rugged::Repository instance.
62
52
def self . clone ( repository )
63
53
path = Dir . mktmpdir ( "rugged" )
54
+ ensure_cleanup ( path )
64
55
65
56
`git clone --quiet -- #{ repository . path } #{ path } `
66
57
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 )
73
59
end
74
60
75
61
def self . prepare ( path )
@@ -104,8 +90,8 @@ def self.rewrite_gitmodules(repo)
104
90
# copied repo.
105
91
url . strip!
106
92
path = Dir . mktmpdir ( url )
93
+ ensure_cleanup ( path )
107
94
FileUtils . cp_r ( File . join ( TestCase ::LIBGIT2_FIXTURE_DIR , url , "." ) , path )
108
- schedule_cleanup ( repo , path )
109
95
110
96
line = "url = #{ path } \n "
111
97
end
@@ -127,26 +113,18 @@ def self.rewrite_gitmodules(repo)
127
113
end
128
114
end
129
115
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
139
120
end
140
121
141
122
def self . directories
142
123
@directories ||= [ ]
143
124
end
144
125
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 )
150
128
self . directories << path
151
129
end
152
130
end
0 commit comments