Skip to content

Commit 808af79

Browse files
committed
🚒 made sure the test for issue 55 is agnostic to git version
Added the ability to mock git up in test_main methods, and setup of the repository in both mock and temp dir. Signed-off-by: Guyzmo <[email protected]>
1 parent 0c66cfe commit 808af79

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
language: python
2+
# Don't use the Travis Container-Based Infrastructure
3+
sudo: true
24
matrix:
35
include:
46
- os: linux

tests/helpers.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@
1515

1616
from git_repo.repo import RepositoryService, main
1717

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+
1846
class RepositoryMockup(RepositoryService):
1947
name = 'test_name'
2048
command = 'test_command'
@@ -134,7 +162,8 @@ def user(self):
134162
def get_repository(self, *args, **kwarg):
135163
return {}
136164

137-
class GitRepoMainTestCase():
165+
166+
class GitRepoMainTestCase(TestGitPopenMockupMixin):
138167
def setup_method(self, method):
139168
self.log.info('GitRepoMainTestCase.setup_method({})'.format(method))
140169
self.tempdir = TemporaryDirectory()
@@ -148,6 +177,8 @@ def setup_method(self, method):
148177
'lab': 'gitlab',
149178
'bb': 'bitbucket',
150179
}
180+
# setup git command mockup
181+
self.setup_git_popen()
151182

152183
def teardown_method(self, method):
153184
self.log.info('GitRepoMainTestCase.teardown_method({})'.format(method))
@@ -341,25 +372,21 @@ def main_noop(self, repo, rc=1, args={}):
341372
'--path': self.tempdir.name
342373
}, args)), "Non {} result for no-action".format(rc)
343374

344-
class GitRepoTestCase():
375+
376+
class GitRepoTestCase(TestGitPopenMockupMixin):
345377
def setup_method(self, method):
346378
self.log.info('GitRepoTestCase.setup_method({})'.format(method))
347379
# build temporary directory
348380
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
357381
# when initiating service with no repository, the connection is not triggered
358382
self.service = self.get_service()
359-
self.service.repository = self.repository
360383
# setup http api mockup
361384
self.recorder = betamax.Betamax(self.get_requests_session())
362385
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
363390
# have git commands logged
364391
Git.GIT_PYTHON_TRACE = True
365392
FORMAT = '> %(message)s'
@@ -390,22 +417,6 @@ def _make_cassette_name(self):
390417
return '_'.join(['test', self.service.name, test_function_name])
391418
raise Exception("Helpers functions shall be used only within test functions!")
392419

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-
409420
'''assertion helpers'''
410421

411422
def assert_repository_exists(self, namespace, repository):

tests/integration/test_main.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,25 @@ def test_request_list__no_repo_slug__https_dot_git_fix__issue55(self):
333333
from subprocess import call
334334
call(['git', 'init', '-q', self.tempdir.name])
335335
call(['git', '--git-dir={}/.git'.format(self.tempdir.name), 'remote', 'add', 'github', 'https://github.com/guyzmo/.git-repo'])
336-
repo_slug, seen_args = self.main_request_list(rc=0, args={})
337-
assert ('guyzmo', '.git-repo') == repo_slug
338-
assert dict() == seen_args
336+
with self.mockup_git('guyzmo', '.git-repo', 'https://github.com/guyzmo/.git-repo'):
337+
self.set_mock_popen_commands([
338+
('git remote get-url --all github', b'https://github.com/guyzmo/.git-repo\n', b'', 0),
339+
])
340+
repo_slug, seen_args = self.main_request_list(rc=0, args={})
341+
assert ('guyzmo', '.git-repo') == repo_slug
342+
assert dict() == seen_args
339343

340344
def test_request_list__no_repo_slug__git_dot_git_fix__issue55(self):
341345
from subprocess import call
342346
call(['git', 'init', '-q', self.tempdir.name])
343347
call(['git', '--git-dir={}/.git'.format(self.tempdir.name), 'remote', 'add', 'github', '[email protected]:guyzmo/.git-repo'])
344-
repo_slug, seen_args = self.main_request_list(rc=0, args={})
345-
assert ('guyzmo', '.git-repo') == repo_slug
346-
assert dict() == seen_args
348+
with self.mockup_git('guyzmo', '.git-repo', '[email protected]:guyzmo/.git-repo'):
349+
self.set_mock_popen_commands([
350+
('git remote get-url --all github', b'[email protected]:guyzmo/.git-repo', b'', 0),
351+
])
352+
repo_slug, seen_args = self.main_request_list(rc=0, args={})
353+
assert ('guyzmo', '.git-repo') == repo_slug
354+
assert dict() == seen_args
347355

348356
def test_request_fetch__request(self, capsys, caplog):
349357
from subprocess import call

0 commit comments

Comments
 (0)