55# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66from __future__ import print_function
77
8- import os
9- from unittest import TestCase
10- import time
11- import tempfile
8+ from functools import wraps
129import io
1310import logging
11+ import os
12+ import tempfile
13+ import textwrap
14+ import time
15+ from unittest import TestCase
1416
15- from functools import wraps
16-
17- from git .util import rmtree
1817from 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
21+
2022
21- osp = os . path .dirname
23+ ospd = osp .dirname
2224
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 " )
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 " )
2527
2628__all__ = (
27- 'fixture_path' , 'fixture' , 'absolute_project_path' , ' StringProcessAdapter' ,
29+ 'fixture_path' , 'fixture' , 'StringProcessAdapter' ,
2830 'with_rw_directory' , 'with_rw_repo' , 'with_rw_and_rw_remote_repo' , 'TestBase' , 'TestCase' ,
2931 'GIT_REPO' , 'GIT_DAEMON_PORT'
3032)
3537
3638
3739def fixture_path (name ):
38- test_dir = osp (osp (__file__ ))
39- return os .path .join (test_dir , "fixtures" , name )
40+ return osp .join (ospd (ospd (__file__ )), 'fixtures' , name )
4041
4142
4243def fixture (name ):
4344 with open (fixture_path (name ), 'rb' ) as fd :
4445 return fd .read ()
4546
46-
47- def absolute_project_path ():
48- return os .path .abspath (os .path .join (osp (__file__ ), ".." , ".." ))
49-
5047#} END routines
5148
5249#{ Adapters
@@ -165,26 +162,31 @@ def repo_creator(self):
165162 return argument_passer
166163
167164
168- def launch_git_daemon (temp_dir , ip , port ):
165+ def launch_git_daemon (base_path , ip , port ):
169166 from git import Git
170167 if is_win :
171168 ## On MINGW-git, daemon exists in .\Git\mingw64\libexec\git-core\,
172169 # but if invoked as 'git daemon', it detaches from parent `git` cmd,
173170 # and then CANNOT DIE!
174171 # So, invoke it as a single command.
175- ## Cygwin-git has no daemon.
172+ ## Cygwin-git has no daemon. But it can use MINGW's.
176173 #
177- daemon_cmd = ['git-daemon' , temp_dir ,
174+ daemon_cmd = ['git-daemon' ,
178175 '--enable=receive-pack' ,
179176 '--listen=%s' % ip ,
180- '--port=%s' % port ]
177+ '--port=%s' % port ,
178+ '--base-path=%s' % base_path ,
179+ base_path ]
181180 gd = Git ().execute (daemon_cmd , as_process = True )
182181 else :
183- gd = Git ().daemon (temp_dir ,
182+ gd = Git ().daemon (base_path ,
184183 enable = 'receive-pack' ,
185184 listen = ip ,
186185 port = port ,
186+ base_path = base_path ,
187187 as_process = True )
188+ # yes, I know ... fortunately, this is always going to work if sleep time is just large enough
189+ time .sleep (0.5 )
188190 return gd
189191
190192
@@ -212,7 +214,8 @@ def case(self, rw_repo, rw_remote_repo)
212214 See working dir info in with_rw_repo
213215 :note: We attempt to launch our own invocation of git-daemon, which will be shutdown at the end of the test.
214216 """
215- from git import Remote , GitCommandError
217+ from git import Git , Remote , GitCommandError
218+
216219 assert isinstance (working_tree_ref , string_types ), "Decorator requires ref name for working tree checkout"
217220
218221 def argument_passer (func ):
@@ -240,23 +243,36 @@ def remote_repo_creator(self):
240243 pass
241244 crw .set (section , "receivepack" , True )
242245
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
246+ # Initialize the remote - first do it as local remote and pull, then
247+ # we change the url to point to the daemon.
246248 d_remote = Remote .create (rw_repo , "daemon_origin" , remote_repo_dir )
247249 d_remote .fetch ()
248- remote_repo_url = "git://localhost:%s%s" % (GIT_DAEMON_PORT , remote_repo_dir )
249250
251+ base_path , rel_repo_dir = osp .split (remote_repo_dir )
252+
253+ remote_repo_url = "git://localhost:%s/%s" % (GIT_DAEMON_PORT , rel_repo_dir )
250254 with d_remote .config_writer as cw :
251255 cw .set ('url' , remote_repo_url )
252256
253- temp_dir = osp (_mktemp ())
254- gd = launch_git_daemon (temp_dir , '127.0.0.1' , GIT_DAEMON_PORT )
255257 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-
258+ gd = launch_git_daemon (Git .polish_url (base_path ), '127.0.0.1' , GIT_DAEMON_PORT )
259+ except Exception as ex :
260+ if is_win :
261+ msg = textwrap .dedent ("""
262+ The `git-daemon.exe` must be in PATH.
263+ For MINGW, look into .\Git\mingw64\libexec\git-core\), but problems with paths might appear.
264+ CYGWIN has no daemon, but if one exists, it gets along fine (has also paths problems)
265+ Anyhow, alternatively try starting `git-daemon` manually:""" )
266+ else :
267+ msg = "Please try starting `git-daemon` manually:"
268+ msg += textwrap .dedent ("""
269+ git daemon --enable=receive-pack --base-path=%s %s
270+ You can also run the daemon on a different port by passing --port=<port>"
271+ and setting the environment variable GIT_PYTHON_TEST_GIT_DAEMON_PORT to <port>
272+ """ % (base_path , base_path ))
273+ raise AssertionError (ex , msg )
274+ # END make assertion
275+ else :
260276 # try to list remotes to diagnoes whether the server is up
261277 try :
262278 rw_repo .git .ls_remote (d_remote )
@@ -283,9 +299,9 @@ def remote_repo_creator(self):
283299 git daemon --enable=receive-pack '%s'
284300 You can also run the daemon on a different port by passing --port=<port>"
285301 and setting the environment variable GIT_PYTHON_TEST_GIT_DAEMON_PORT to <port>
286- """ % temp_dir )
302+ """ % base_path )
287303 from unittest import SkipTest
288- raise SkipTest (msg ) if is_win else AssertionError (msg )
304+ raise SkipTest (msg ) if HIDE_WINDOWS_KNOWN_ERRORS else AssertionError (e , msg )
289305 # END make assertion
290306 # END catch ls remote error
291307
@@ -354,7 +370,7 @@ class TestBase(TestCase):
354370
355371 def _small_repo_url (self ):
356372 """:return" a path to a small, clonable repository"""
357- return os . path .join (self .rorepo .working_tree_dir , 'git/ext/gitdb/gitdb/ext/smmap' )
373+ return osp .join (self .rorepo .working_tree_dir , 'git/ext/gitdb/gitdb/ext/smmap' )
358374
359375 @classmethod
360376 def setUpClass (cls ):
@@ -378,7 +394,7 @@ def _make_file(self, rela_path, data, repo=None):
378394 with the given data. Returns absolute path to created file.
379395 """
380396 repo = repo or self .rorepo
381- abs_path = os . path .join (repo .working_tree_dir , rela_path )
397+ abs_path = osp .join (repo .working_tree_dir , rela_path )
382398 with open (abs_path , "w" ) as fp :
383399 fp .write (data )
384400 return abs_path
0 commit comments