Skip to content

Commit 79f542d

Browse files
authored
Code coverage improvements (#28)
* Code coverage - Git Clients * Code coverage - dry-run * Code coverage - pop process * Test directory
1 parent 03912a7 commit 79f542d

5 files changed

Lines changed: 101 additions & 4 deletions

File tree

sonar-project.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ sonar.organization=axel3rd-github
33
sonar.projectName=Git Platforms Synchro
44
sonar.python.coverage.reportPaths=coverage.xml
55
sonar.python.version=3.11,3.12,3.13,3.14
6+
sonar.tests=tests
67
sonar.coverage.exclusions=**/tests/subtest_git_clients_dependencies_import.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"slug":"spring-ai-examples","id":14,"name":"spring-ai-examples","hierarchyId":"4b75de9e2055a505cad1","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"MYORG","id":1,"name":"MyOrg","public":false,"type":"NORMAL","links":{"self":[{"href":"http://localhost:7990/projects/MYORG"}]}},"public":false,"archived":false,"links":{"clone":[{"href":"http://localhost:7990/scm/myorg/spring-ai-examples.git","name":"http-value"},{"href":"ssh://git@localhost:7999/myorg/spring-ai-examples.git","name":"ssh"}],"self":[{"href":"http://localhost:7990/projects/MYORG/repos/spring-ai-examples/browse"}]}}

tests/test_git_clients.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
import re
22
from pytest_httpserver import HTTPServer
33
from pytest import LogCaptureFixture, raises
4-
from modules.git_clients import GitClientFactory
4+
from modules.git_clients import GitClientFactory, GitClient, check_input, check_input_empty
55
from tests.test_utils import get_url_root, expect_request
66
from requests import exceptions
7+
from github import GithubException
8+
from gitlab import GitlabError
9+
10+
11+
def test_interface():
12+
client = GitClient()
13+
client.get_login_or_token()
14+
client.get_password()
15+
client.get_url()
16+
client.get_repos('org')
17+
client.has_repo('org', 'repo')
18+
client.get_branches('org', 'repo')
19+
client.get_tags('org', 'repo')
20+
client.get_repo_description('org', 'repo')
21+
client.get_repo_clone_url('org', 'repo')
22+
client.create_repo('org', 'repo')
23+
client.create_repo('org', 'repo', 'description')
24+
25+
26+
def test_check_inputs():
27+
with raises(ValueError, match='not empty'):
28+
check_input_empty('value', 'not empty')
29+
with raises(ValueError, match='empty'):
30+
check_input('', 'empty')
31+
with raises(ValueError, match='empty'):
32+
check_input(None, 'empty') # noqa: python:S5655
733

834

935
def test_type_undefined(caplog: LogCaptureFixture):
@@ -77,6 +103,21 @@ def test_github_empty_branches_tags(httpserver: HTTPServer, caplog: LogCaptureFi
77103
assert 0 == len(github.get_tags('spring-projects', 'spring-petclinic'))
78104

79105

106+
def test_github_errors_has_repo(httpserver: HTTPServer, caplog: LogCaptureFixture):
107+
expect_request(httpserver, 'github', '/users/spring-projects')
108+
httpserver.expect_request('/repos/spring-projects/spring-petclinic').respond_with_data(status=442)
109+
github = GitClientFactory.create_client(get_url_root(httpserver), 'github', 'ghu_xxxx')
110+
with raises(GithubException, match='442'):
111+
github.has_repo('spring-projects', 'spring-petclinic')
112+
113+
114+
def test_github_errors_create_repo(httpserver: HTTPServer, caplog: LogCaptureFixture):
115+
httpserver.expect_request('/orgs/spring-projects').respond_with_data(status=442)
116+
github = GitClientFactory.create_client(get_url_root(httpserver), 'github', 'ghu_xxxx')
117+
with raises(GithubException, match='442'):
118+
github.create_repo('spring-projects', 'spring-petclinic', 'fake')
119+
120+
80121
def test_gitea_proxy(httpserver: HTTPServer, caplog: LogCaptureFixture):
81122
gitea = GitClientFactory.create_client('https://fake.url.dev', 'gitea', 'foo', 'bar', proxy=get_url_root(httpserver))
82123

@@ -259,6 +300,13 @@ def test_bitbucket_empty_branches_tags(httpserver: HTTPServer, caplog: LogCaptur
259300
assert 0 == len(bitbucket.get_tags('MyOrg', 'spring-ai-examples'))
260301

261302

303+
def test_bitbucket_bad_clone_url_http(httpserver: HTTPServer, caplog: LogCaptureFixture):
304+
expect_request(httpserver, 'bitbucket', '/rest/api/1.0/projects/MyOrg/repos/spring-ai-examples-no-clone-url-http')
305+
bitbucket = GitClientFactory.create_client(get_url_root(httpserver), 'bitbucket', 'fake_token')
306+
with raises(ValueError, match='Cannot not found http clone link'):
307+
bitbucket.get_repo_clone_url('MyOrg', 'spring-ai-examples-no-clone-url-http')
308+
309+
262310
def test_gitlab_proxy(httpserver: HTTPServer, caplog: LogCaptureFixture):
263311
gitlab = GitClientFactory.create_client('https://fake.url.dev', 'gitlab', 'fake_token', proxy=get_url_root(httpserver))
264312

@@ -327,6 +375,13 @@ def test_gitlab_empty_branches_tags(httpserver: HTTPServer, caplog: LogCaptureFi
327375
assert 0 == len(gitlab.get_tags('axel3rd', 'spring-petclinic'))
328376

329377

378+
def test_gitlab_errors_has_repo(httpserver: HTTPServer, caplog: LogCaptureFixture):
379+
httpserver.expect_request('/api/v4/projects/axel3rd/spring-petclinic').respond_with_data(status=442)
380+
gitlab = GitClientFactory.create_client(get_url_root(httpserver), 'gitlab', 'glpat-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
381+
with raises(GitlabError, match='442'):
382+
gitlab.has_repo('axel3rd', 'spring-petclinic')
383+
384+
330385
def test_gerrit_proxy(httpserver: HTTPServer, caplog: LogCaptureFixture):
331386
gerrit = GitClientFactory.create_client('https://fake.url.dev', 'gerrit', 'foo', 'bar', proxy=get_url_root(httpserver))
332387

tests/test_git_clients_dependencies_import.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def test_no_bitbucket(request):
1717
exec_test_method(request.module.__file__, request.node.name)
1818

1919

20+
@pytest.mark.skipif(os.name == 'nt', reason='Inifinite loop on Windows')
21+
def test_no_gerrit(request):
22+
exec_test_method(request.module.__file__, request.node.name)
23+
24+
2025
@pytest.mark.skipif(os.name == 'nt', reason='Inifinite loop on Windows')
2126
def test_no_gitlab(request):
2227
exec_test_method(request.module.__file__, request.node.name)

tests/test_git_platforms_synchro.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def test_from_github_to_gitea_mirror_exist(httpserver: HTTPServer, caplog: LogCa
260260
assert 'The requested URL returned error: 542' in caplog.text
261261

262262

263-
def test_from_github_to_gitea_sync(httpserver: HTTPServer, caplog: LogCaptureFixture):
263+
def test_from_github_to_gitea_sync_real(httpserver: HTTPServer, caplog: LogCaptureFixture):
264264
# httpserver doesn't support KeepAlive, so we need to mock the git clone
265265
# as already existing bare directory (reuse mechanism) and mock the git
266266
# push failure
@@ -277,16 +277,37 @@ def test_from_github_to_gitea_sync(httpserver: HTTPServer, caplog: LogCaptureFix
277277
# Gitea with same repo
278278
prepare_gitea_with_spring_projects(httpserver, update_commit=True)
279279

280+
# Remove 'from' credentials to validate pop process
281+
test_args = get_test_args_github_to_gitea(httpserver)
282+
test_args.remove('--from-login')
283+
test_args.remove('foo')
284+
test_args.remove('--from-password')
285+
test_args.remove('bar')
286+
280287
with raises(GitCommandError):
281-
with patch.object(sys, 'argv', get_test_args_github_to_gitea(httpserver)):
288+
with patch.object(sys, 'argv', test_args):
282289
git_platforms_synchro.main()
283290

284291
assert 'Reusing existing cloned repo ' + get_url_root(httpserver) + '/spring-projects/spring-petclinic.git' in caplog.text
285292
assert 'Synchronize branch...' in caplog.text
286293
assert 'The requested URL returned error: 542' in caplog.text
287294

288295

289-
def test_from_github_to_gitea_tags_only(httpserver: HTTPServer, caplog: LogCaptureFixture):
296+
def test_from_github_to_gitea_sync_dry_run(httpserver: HTTPServer, caplog: LogCaptureFixture):
297+
# GitHub with spring-projects
298+
prepare_github_with_spring_projects(httpserver)
299+
300+
# Gitea with same repo
301+
prepare_gitea_with_spring_projects(httpserver, update_commit=True)
302+
303+
with patch.object(sys, 'argv', get_test_args_github_to_gitea(httpserver) + ['--dry-run']):
304+
git_platforms_synchro.main()
305+
306+
assert 'Synchronize branch...' in caplog.text
307+
assert ' Dry-run mode, skipping branch synchronization.' in caplog.text
308+
309+
310+
def test_from_github_to_gitea_tags_only_real(httpserver: HTTPServer, caplog: LogCaptureFixture):
290311
# httpserver doesn't support KeepAlive, so we need to mock the git clone
291312
# as already existing bare directory (reuse mechanism) and mock the git
292313
# push failure
@@ -313,6 +334,20 @@ def test_from_github_to_gitea_tags_only(httpserver: HTTPServer, caplog: LogCaptu
313334
assert 'The requested URL returned error: 542' in caplog.text
314335

315336

337+
def test_from_github_to_gitea_tags_only_dry_run(httpserver: HTTPServer, caplog: LogCaptureFixture):
338+
# GitHub with spring-projects
339+
prepare_github_with_spring_projects(httpserver)
340+
341+
# Gitea with same repo
342+
prepare_gitea_with_spring_projects(httpserver, prepare_tags=False)
343+
httpserver.expect_request('/api/v1/repos/MyOrg/spring-petclinic/tags').respond_with_data('[]')
344+
345+
with patch.object(sys, 'argv', get_test_args_github_to_gitea(httpserver) + ['--dry-run']):
346+
git_platforms_synchro.main()
347+
348+
assert 'Dry-run mode, skipping tags synchronization.' in caplog.text
349+
350+
316351
def test_from_github_to_gitea_all_already_sync(httpserver: HTTPServer, caplog: LogCaptureFixture):
317352
# GitHub with spring-projects
318353
prepare_github_with_spring_projects(httpserver)

0 commit comments

Comments
 (0)