15
15
16
16
from git_repo .repo import RepositoryService , main
17
17
18
+
19
+ class TestGitPopenMockupMixin :
20
+ def setup_git_popen (self ):
21
+ # repository mockup (in a temporary place)
22
+ self .repository = Repo .init (self .tempdir .name )
23
+ # setup git command mockup
24
+ self .Popen = MockPopen ()
25
+ self .Popen .mock .Popen_instance .stdin = None
26
+ self .Popen .mock .Popen_instance .wait = lambda * a , ** k : self .Popen .wait ()
27
+ self .Popen .mock .Popen_instance .__enter__ = lambda self : self
28
+ self .Popen .mock .Popen_instance .__exit__ = lambda self , * a , ** k : None
29
+
30
+ def set_mock_popen_commands (self , cmd_list ):
31
+ for cmd , out , err , rc in cmd_list :
32
+ self .Popen .set_command (cmd , out , err , returncode = rc )
33
+
34
+ def mockup_git (self , namespace , repository , url = None ):
35
+ # disable refspec check
36
+ from git import remote
37
+ remote .Remote ._assert_refspec = lambda self : None
38
+ # write FETCH_HEAD ref
39
+ with open (os .path .join (self .repository .git_dir , 'FETCH_HEAD' ), 'w' ) as f :
40
+ url = url or "{}:{}/{}" .format (self .service .fqdn , namespace , repository )
41
+ f .write ("749656b8b3b282d11a4221bb84e48291ca23ecc6" \
42
+ " branch 'master' of {}" .format (url ))
43
+ return Replace ('git.cmd.Popen' , self .Popen )
44
+
45
+
18
46
class RepositoryMockup (RepositoryService ):
19
47
name = 'test_name'
20
48
command = 'test_command'
@@ -134,7 +162,8 @@ def user(self):
134
162
def get_repository (self , * args , ** kwarg ):
135
163
return {}
136
164
137
- class GitRepoMainTestCase ():
165
+
166
+ class GitRepoMainTestCase (TestGitPopenMockupMixin ):
138
167
def setup_method (self , method ):
139
168
self .log .info ('GitRepoMainTestCase.setup_method({})' .format (method ))
140
169
self .tempdir = TemporaryDirectory ()
@@ -148,6 +177,8 @@ def setup_method(self, method):
148
177
'lab' : 'gitlab' ,
149
178
'bb' : 'bitbucket' ,
150
179
}
180
+ # setup git command mockup
181
+ self .setup_git_popen ()
151
182
152
183
def teardown_method (self , method ):
153
184
self .log .info ('GitRepoMainTestCase.teardown_method({})' .format (method ))
@@ -341,25 +372,21 @@ def main_noop(self, repo, rc=1, args={}):
341
372
'--path' : self .tempdir .name
342
373
}, args )), "Non {} result for no-action" .format (rc )
343
374
344
- class GitRepoTestCase ():
375
+
376
+ class GitRepoTestCase (TestGitPopenMockupMixin ):
345
377
def setup_method (self , method ):
346
378
self .log .info ('GitRepoTestCase.setup_method({})' .format (method ))
347
379
# build temporary directory
348
380
self .tempdir = TemporaryDirectory ()
349
- # repository mockup (in a temporary place)
350
- self .repository = Repo .init (self .tempdir .name )
351
- # setup git command mockup
352
- self .Popen = MockPopen ()
353
- self .Popen .mock .Popen_instance .stdin = None
354
- self .Popen .mock .Popen_instance .wait = lambda * a , ** k : self .Popen .wait ()
355
- self .Popen .mock .Popen_instance .__enter__ = lambda self : self
356
- self .Popen .mock .Popen_instance .__exit__ = lambda self , * a , ** k : None
357
381
# when initiating service with no repository, the connection is not triggered
358
382
self .service = self .get_service ()
359
- self .service .repository = self .repository
360
383
# setup http api mockup
361
384
self .recorder = betamax .Betamax (self .get_requests_session ())
362
385
self .get_requests_session ().headers ['Accept-Encoding' ] = 'identity'
386
+ # setup git command mockup
387
+ self .setup_git_popen ()
388
+ # when initiating service with no repository, the connection is not triggered
389
+ self .service .repository = self .repository
363
390
# have git commands logged
364
391
Git .GIT_PYTHON_TRACE = True
365
392
FORMAT = '> %(message)s'
@@ -390,22 +417,6 @@ def _make_cassette_name(self):
390
417
return '_' .join (['test' , self .service .name , test_function_name ])
391
418
raise Exception ("Helpers functions shall be used only within test functions!" )
392
419
393
- '''popen helper'''
394
-
395
- def set_mock_popen_commands (self , cmd_list ):
396
- for cmd , out , err , rc in cmd_list :
397
- self .Popen .set_command (cmd , out , err , returncode = rc )
398
-
399
- def mockup_git (self , namespace , repository ):
400
- # disable refspec check
401
- from git import remote
402
- remote .Remote ._assert_refspec = lambda self : None
403
- # write FETCH_HEAD ref
404
- with open (os .path .join (self .repository .git_dir , 'FETCH_HEAD' ), 'w' ) as f :
405
- f .write ("749656b8b3b282d11a4221bb84e48291ca23ecc6" \
406
- " branch 'master' of git@{}/{}/{}" .format (self .service .fqdn , namespace , repository ))
407
- return Replace ('git.cmd.Popen' , self .Popen )
408
-
409
420
'''assertion helpers'''
410
421
411
422
def assert_repository_exists (self , namespace , repository ):
0 commit comments