5
5
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6
6
from __future__ import print_function
7
7
8
- import os
9
- from unittest import TestCase
10
- import time
11
- import tempfile
8
+ from functools import wraps
12
9
import io
13
10
import logging
11
+ import os
12
+ import tempfile
13
+ import textwrap
14
+ import time
15
+ from unittest import TestCase
14
16
15
- from functools import wraps
16
-
17
- from git .util import rmtree
18
17
from git .compat import string_types , is_win
19
- import textwrap
18
+ from git .util import rmtree , HIDE_WINDOWS_KNOWN_ERRORS
19
+
20
+ import os .path as osp
20
21
21
- osp = os .path .dirname
22
22
23
- GIT_REPO = os .environ .get ("GIT_PYTHON_TEST_GIT_REPO_BASE" , osp (osp (osp (osp (__file__ )))))
24
- GIT_DAEMON_PORT = os .environ .get ("GIT_PYTHON_TEST_GIT_DAEMON_PORT" , "9418" )
23
+ ospd = osp .dirname
24
+
25
+ GIT_REPO = os .environ .get ("GIT_PYTHON_TEST_GIT_REPO_BASE" , ospd (ospd (ospd (ospd (__file__ )))))
26
+ GIT_DAEMON_PORT = os .environ .get ("GIT_PYTHON_TEST_GIT_DAEMON_PORT" , "19418" )
25
27
26
28
__all__ = (
27
29
'fixture_path' , 'fixture' , 'absolute_project_path' , 'StringProcessAdapter' ,
35
37
36
38
37
39
def fixture_path (name ):
38
- test_dir = osp ( osp (__file__ ))
39
- return os . path .join (test_dir , "fixtures" , name )
40
+ test_dir = ospd ( ospd (__file__ ))
41
+ return osp .join (test_dir , "fixtures" , name )
40
42
41
43
42
44
def fixture (name ):
@@ -45,7 +47,7 @@ def fixture(name):
45
47
46
48
47
49
def absolute_project_path ():
48
- return os . path . abspath (os . path .join (osp (__file__ ), ".." , ".." ))
50
+ return osp . abspath (osp .join (osp (__file__ ), ".." , ".." ))
49
51
50
52
#} END routines
51
53
@@ -165,26 +167,31 @@ def repo_creator(self):
165
167
return argument_passer
166
168
167
169
168
- def launch_git_daemon (temp_dir , ip , port ):
170
+ def launch_git_daemon (base_path , ip , port ):
169
171
from git import Git
170
172
if is_win :
171
173
## On MINGW-git, daemon exists in .\Git\mingw64\libexec\git-core\,
172
174
# but if invoked as 'git daemon', it detaches from parent `git` cmd,
173
175
# and then CANNOT DIE!
174
176
# So, invoke it as a single command.
175
- ## Cygwin-git has no daemon.
177
+ ## Cygwin-git has no daemon. But it can use MINGW's.
176
178
#
177
- daemon_cmd = ['git-daemon' , temp_dir ,
179
+ daemon_cmd = ['git-daemon' ,
178
180
'--enable=receive-pack' ,
179
181
'--listen=%s' % ip ,
180
- '--port=%s' % port ]
182
+ '--port=%s' % port ,
183
+ '--base-path=%s' % base_path ,
184
+ base_path ]
181
185
gd = Git ().execute (daemon_cmd , as_process = True )
182
186
else :
183
- gd = Git ().daemon (temp_dir ,
187
+ gd = Git ().daemon (base_path ,
184
188
enable = 'receive-pack' ,
185
189
listen = ip ,
186
190
port = port ,
191
+ base_path = base_path ,
187
192
as_process = True )
193
+ # yes, I know ... fortunately, this is always going to work if sleep time is just large enough
194
+ time .sleep (0.5 )
188
195
return gd
189
196
190
197
@@ -212,7 +219,8 @@ def case(self, rw_repo, rw_remote_repo)
212
219
See working dir info in with_rw_repo
213
220
:note: We attempt to launch our own invocation of git-daemon, which will be shutdown at the end of the test.
214
221
"""
215
- from git import Remote , GitCommandError
222
+ from git import Git , Remote , GitCommandError
223
+
216
224
assert isinstance (working_tree_ref , string_types ), "Decorator requires ref name for working tree checkout"
217
225
218
226
def argument_passer (func ):
@@ -240,23 +248,36 @@ def remote_repo_creator(self):
240
248
pass
241
249
crw .set (section , "receivepack" , True )
242
250
243
- # initialize the remote - first do it as local remote and pull, then
244
- # we change the url to point to the daemon. The daemon should be started
245
- # by the user, not by us
251
+ # Initialize the remote - first do it as local remote and pull, then
252
+ # we change the url to point to the daemon.
246
253
d_remote = Remote .create (rw_repo , "daemon_origin" , remote_repo_dir )
247
254
d_remote .fetch ()
248
- remote_repo_url = "git://localhost:%s%s" % (GIT_DAEMON_PORT , remote_repo_dir )
249
255
256
+ base_path , rel_repo_dir = osp .split (remote_repo_dir )
257
+
258
+ remote_repo_url = "git://localhost:%s/%s" % (GIT_DAEMON_PORT , rel_repo_dir )
250
259
with d_remote .config_writer as cw :
251
260
cw .set ('url' , remote_repo_url )
252
261
253
- temp_dir = osp (_mktemp ())
254
- gd = launch_git_daemon (temp_dir , '127.0.0.1' , GIT_DAEMON_PORT )
255
262
try :
256
- # yes, I know ... fortunately, this is always going to work if sleep time is just large enough
257
- time .sleep (0.5 )
258
- # end
259
-
263
+ gd = launch_git_daemon (Git .polish_url (base_path ), '127.0.0.1' , GIT_DAEMON_PORT )
264
+ except Exception as ex :
265
+ if is_win :
266
+ msg = textwrap .dedent ("""
267
+ The `git-daemon.exe` must be in PATH.
268
+ For MINGW, look into .\Git\mingw64\libexec\git-core\), but problems with paths might appear.
269
+ CYGWIN has no daemon, but if one exists, it gets along fine (has also paths problems)
270
+ Anyhow, alternatively try starting `git-daemon` manually:""" )
271
+ else :
272
+ msg = "Please try starting `git-daemon` manually:"
273
+ msg += textwrap .dedent ("""
274
+ git daemon --enable=receive-pack --base-path=%s %s
275
+ You can also run the daemon on a different port by passing --port=<port>"
276
+ and setting the environment variable GIT_PYTHON_TEST_GIT_DAEMON_PORT to <port>
277
+ """ % (base_path , base_path ))
278
+ raise AssertionError (ex , msg )
279
+ # END make assertion
280
+ else :
260
281
# try to list remotes to diagnoes whether the server is up
261
282
try :
262
283
rw_repo .git .ls_remote (d_remote )
@@ -283,9 +304,9 @@ def remote_repo_creator(self):
283
304
git daemon --enable=receive-pack '%s'
284
305
You can also run the daemon on a different port by passing --port=<port>"
285
306
and setting the environment variable GIT_PYTHON_TEST_GIT_DAEMON_PORT to <port>
286
- """ % temp_dir )
307
+ """ % base_path )
287
308
from unittest import SkipTest
288
- raise SkipTest (msg ) if is_win else AssertionError (msg )
309
+ raise SkipTest (msg ) if HIDE_WINDOWS_KNOWN_ERRORS else AssertionError (e , msg )
289
310
# END make assertion
290
311
# END catch ls remote error
291
312
@@ -354,7 +375,7 @@ class TestBase(TestCase):
354
375
355
376
def _small_repo_url (self ):
356
377
""":return" a path to a small, clonable repository"""
357
- return os . path .join (self .rorepo .working_tree_dir , 'git/ext/gitdb/gitdb/ext/smmap' )
378
+ return osp .join (self .rorepo .working_tree_dir , 'git/ext/gitdb/gitdb/ext/smmap' )
358
379
359
380
@classmethod
360
381
def setUpClass (cls ):
@@ -378,7 +399,7 @@ def _make_file(self, rela_path, data, repo=None):
378
399
with the given data. Returns absolute path to created file.
379
400
"""
380
401
repo = repo or self .rorepo
381
- abs_path = os . path .join (repo .working_tree_dir , rela_path )
402
+ abs_path = osp .join (repo .working_tree_dir , rela_path )
382
403
with open (abs_path , "w" ) as fp :
383
404
fp .write (data )
384
405
return abs_path
0 commit comments