66
77import os
88import sys
9- from git import Repo , Remote , GitCommandError
9+ from git import Repo , Remote , GitCommandError , Git
1010from unittest import TestCase
1111import tempfile
1212import shutil
@@ -149,6 +149,7 @@ def case(self, rw_repo, rw_remote_repo)
149149 This setup allows you to test push and pull scenarios and hooks nicely.
150150
151151 See working dir info in with_rw_repo
152+ :note: We attempt to launch our own invocation of git-daemon, which will be shutdown at the end of the test.
152153 """
153154 assert isinstance (working_tree_ref , basestring ), "Decorator requires ref name for working tree checkout"
154155
@@ -186,17 +187,30 @@ def remote_repo_creator(self):
186187
187188 d_remote .config_writer .set ('url' , remote_repo_url )
188189
190+ temp_dir = os .path .dirname (_mktemp ())
191+ # On windows, this will fail ... we deal with failures anyway and default to telling the user to do it
192+ try :
193+ gd = Git ().daemon (temp_dir , as_process = True )
194+ except Exception as err :
195+ gd = None
196+ # end
197+
189198 # try to list remotes to diagnoes whether the server is up
190199 try :
191200 rw_repo .git .ls_remote (d_remote )
192201 except GitCommandError , e :
202+ # We assume in good faith that we didn't start the daemon - but make sure we kill it anyway
203+ # Of course we expect it to work here already, but maybe there are timing constraints
204+ # on some platforms ?
205+ if gd is not None :
206+ os .kill (gd .proc .pid , 15 )
193207 print str (e )
194208 if os .name == 'nt' :
195209 raise AssertionError (
196- 'git-daemon needs to run this test, but windows does not have one. Otherwise, run: git-daemon "%s"' % os . path . dirname ( _mktemp ()) )
210+ 'git-daemon needs to run this test, but windows does not have one. Otherwise, run: git-daemon "%s"' % temp_dir )
197211 else :
198212 raise AssertionError (
199- 'Please start a git-daemon to run this test, execute: git-daemon "%s"' % os . path . dirname ( _mktemp ()) )
213+ 'Please start a git-daemon to run this test, execute: git-daemon "%s"' % temp_dir )
200214 # END make assertion
201215 # END catch ls remote error
202216
@@ -206,11 +220,16 @@ def remote_repo_creator(self):
206220 try :
207221 return func (self , rw_repo , rw_remote_repo )
208222 finally :
223+ # gd.proc.kill() ... no idea why that doesn't work
224+ os .kill (gd .proc .pid , 15 )
225+
209226 os .chdir (prev_cwd )
210227 rw_repo .git .clear_cache ()
211228 rw_remote_repo .git .clear_cache ()
212229 shutil .rmtree (repo_dir , onerror = _rmtree_onerror )
213230 shutil .rmtree (remote_repo_dir , onerror = _rmtree_onerror )
231+
232+ gd .proc .wait ()
214233 # END cleanup
215234 # END bare repo creator
216235 remote_repo_creator .__name__ = func .__name__
0 commit comments