Skip to content

Commit be79388

Browse files
authored
CM-38309 - Cover optional System Git Executable with more tests (#237)
1 parent 996d405 commit be79388

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

cycode/cli/utils/git_proxy.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,29 @@ def get_git_proxy(git_module: Optional[types.ModuleType]) -> _AbstractGitProxy:
7373
return _GitProxy() if git_module else _DummyGitProxy()
7474

7575

76-
git_proxy = get_git_proxy(git)
76+
class GitProxyManager(_AbstractGitProxy):
77+
"""We are using this manager for easy unit testing and mocking of the git module."""
78+
79+
def __init__(self) -> None:
80+
self._git_proxy = get_git_proxy(git)
81+
82+
def _set_dummy_git_proxy(self) -> None:
83+
self._git_proxy = _DummyGitProxy()
84+
85+
def _set_git_proxy(self) -> None:
86+
self._git_proxy = _GitProxy()
87+
88+
def get_repo(self, path: Optional['PathLike'] = None, *args, **kwargs) -> 'Repo':
89+
return self._git_proxy.get_repo(path, *args, **kwargs)
90+
91+
def get_null_tree(self) -> object:
92+
return self._git_proxy.get_null_tree()
93+
94+
def get_invalid_git_repository_error(self) -> Type[BaseException]:
95+
return self._git_proxy.get_invalid_git_repository_error()
96+
97+
def get_git_command_error(self) -> Type[BaseException]:
98+
return self._git_proxy.get_git_command_error()
99+
100+
101+
git_proxy = GitProxyManager()

tests/cli/commands/test_main_command.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from click.testing import CliRunner
88

99
from cycode.cli.commands.main_cli import main_cli
10+
from cycode.cli.utils.git_proxy import git_proxy
1011
from tests.conftest import CLI_ENV_VARS, TEST_FILES_PATH, ZIP_CONTENT_PATH
1112
from tests.cyclient.mocked_responses.scan_client import mock_scan_responses
1213
from tests.cyclient.test_scan_client import get_zipped_file_scan_response, get_zipped_file_scan_url
@@ -47,3 +48,39 @@ def test_passing_output_option(output: str, scan_client: 'ScanClient', api_token
4748
assert 'scan_id' in output
4849
else:
4950
assert 'Scan ID' in result.output
51+
52+
53+
@responses.activate
54+
def test_optional_git_with_path_scan(scan_client: 'ScanClient', api_token_response: responses.Response) -> None:
55+
mock_scan_responses(responses, 'secret', scan_client, uuid4(), ZIP_CONTENT_PATH)
56+
responses.add(get_zipped_file_scan_response(get_zipped_file_scan_url('secret', scan_client), ZIP_CONTENT_PATH))
57+
responses.add(api_token_response)
58+
59+
# fake env without Git executable
60+
git_proxy._set_dummy_git_proxy()
61+
62+
args = ['--output', 'json', 'scan', 'path', str(_PATH_TO_SCAN)]
63+
result = CliRunner().invoke(main_cli, args, env=CLI_ENV_VARS)
64+
65+
# do NOT expect error about not found Git executable
66+
assert 'GIT_PYTHON_GIT_EXECUTABLE' not in result.output
67+
68+
# reset the git proxy
69+
git_proxy._set_git_proxy()
70+
71+
72+
@responses.activate
73+
def test_required_git_with_path_repository(scan_client: 'ScanClient', api_token_response: responses.Response) -> None:
74+
responses.add(api_token_response)
75+
76+
# fake env without Git executable
77+
git_proxy._set_dummy_git_proxy()
78+
79+
args = ['--output', 'json', 'scan', 'repository', str(_PATH_TO_SCAN)]
80+
result = CliRunner().invoke(main_cli, args, env=CLI_ENV_VARS)
81+
82+
# expect error about not found Git executable
83+
assert 'GIT_PYTHON_GIT_EXECUTABLE' in result.output
84+
85+
# reset the git proxy
86+
git_proxy._set_git_proxy()

0 commit comments

Comments
 (0)