1- import responses
21import re
32import sys
3+ from io import StringIO
4+
5+ import responses
46
57# The mock package is only available from Python 3.3 onwards. Thank you, Python.
68if sys .version_info >= (3 , 3 ):
1113from unittest import TestCase
1214from teamscale_client .teamscale_client_config import TeamscaleClientConfig
1315from teamscale_precommit_client import PrecommitClient
16+ from teamscale_precommit_client .precommit_client import DEFAULT_PATH_PREFIX
1417from teamscale_client .utils import to_json
1518
1619URL = 'http://localhost:8080'
1922USERNAME = 'johndoe'
2023ACCESS_TOKEN = 'secret'
2124REPO_PATH = 'path/to/repo/'
22- ANALYZED_FILE = REPO_PATH + 'file.ext'
25+ ANALYZED_FILE_NAME = 'file.ext'
26+ ANALYZED_FILE_PATH = REPO_PATH + ANALYZED_FILE_NAME
27+ DELETED_FILE_NAME = 'deletedFile.ext'
2328CURRENT_BRANCH = 'my_feature_branch'
2429
2530
@@ -131,22 +136,56 @@ def test_get_added_and_existing_findings_in_changes_for_changes(self):
131136 self .assert_findings_ids (self .precommit_client .findings_in_changed_code , [])
132137 self .assert_findings_ids (self .precommit_client .existing_findings , [4 , 5 , 6 , 7 ])
133138
139+ @responses .activate
140+ def test_adding_path_prefix (self ):
141+ path_prefix = 'prefix'
142+ self .precommit_client = self ._get_precommit_client (self ._get_changed_file (),
143+ [DELETED_FILE_NAME ], path_prefix = path_prefix )
144+ self .mock_precommit_findings_churn (added_findings = [1 ], path_prefix = path_prefix + '/' )
145+ self .precommit_client .run ()
146+
147+ changed_file_path_with_prefix = path_prefix + '/' + ANALYZED_FILE_NAME
148+ deleted_file_path_with_prefix = path_prefix + '/' + DELETED_FILE_NAME
149+ precommit_request = next (call .request for call in responses .calls if call .request .method == 'PUT' )
150+
151+ # Check if the path prefix was applied and sent correctly in the request's body
152+ self .assertIn (changed_file_path_with_prefix , precommit_request .body )
153+ self .assertIn (deleted_file_path_with_prefix , precommit_request .body )
154+
155+ @responses .activate
156+ def test_stripping_path_prefix (self ):
157+ path_prefix = 'prefix'
158+ self .precommit_client = self ._get_precommit_client (self ._get_changed_file (),
159+ [DELETED_FILE_NAME ], path_prefix = path_prefix )
160+ self .mock_precommit_findings_churn (added_findings = [1 ], path_prefix = path_prefix + '/' )
161+
162+ captured_output = StringIO ()
163+ sys .stdout = captured_output
164+
165+ self .precommit_client .run ()
166+
167+ # Check if the path prefix was applied and sent correctly in the request's body
168+ self .assertNotIn (path_prefix , captured_output .getvalue ())
169+
134170 @staticmethod
135- def mock_precommit_findings_churn (added_findings = None , findings_in_changed_code = None , removed_findings = None ):
171+ def mock_precommit_findings_churn (added_findings = None , findings_in_changed_code = None , removed_findings = None ,
172+ path_prefix = DEFAULT_PATH_PREFIX ):
136173 """Mocks returning the findings churn for the given added, removed, and findings in changed code.
137174 Findings can be provided as list of integers each of which represents a finding instance."""
138- precommit_response = to_json (PrecommitClientTest ._get_findings_churn (added_findings , findings_in_changed_code ,
139- removed_findings ))
175+ precommit_response = to_json (
176+ PrecommitClientTest ._get_findings_churn (path_prefix , added_findings , findings_in_changed_code ,
177+ removed_findings ))
140178 responses .add (responses .PUT , PrecommitClientTest .get_project_service_mock ('pre-commit' ), body = SUCCESS ,
141179 status = 200 )
142180 responses .add (responses .GET , PrecommitClientTest .get_project_service_mock ('pre-commit' ),
143181 body = precommit_response , status = 200 , content_type = "application/json" , )
144182
145183 @staticmethod
146- def mock_existing_findings (branch , existing_findings = None ):
184+ def mock_existing_findings (branch , existing_findings = None , path_prefix = DEFAULT_PATH_PREFIX ):
147185 """Mocks returning the given existing findings for the provided branch.
148186 Findings can be provided as list of integers each of which represents a finding instance."""
149- existing_findings_from_current_branch = to_json (PrecommitClientTest ._get_findings_as_dicts (existing_findings ))
187+ existing_findings_from_current_branch = to_json (
188+ PrecommitClientTest ._get_findings_as_dicts (existing_findings , path_prefix ))
150189 responses .add (responses .GET , PrecommitClientTest .get_project_service_mock ('findings' , branch ),
151190 body = existing_findings_from_current_branch , status = 200 ,
152191 content_type = "application/json" , )
@@ -162,13 +201,15 @@ def get_project_service_mock(service_id, branch=''):
162201 return re .compile (r'%s/p/%s/%s/.*%s.*' % (URL , PROJECT , service_id , branch ))
163202
164203 @staticmethod
165- def _get_precommit_client (changed_files , deleted_files , fetch_existing_findings = False ,
204+ def _get_precommit_client (changed_files , deleted_files , path_prefix = DEFAULT_PATH_PREFIX ,
205+ fetch_existing_findings = False ,
166206 fetch_existing_findings_in_changes = False ):
167207 """Gets a precommit client some of whose methods are mocked out for testing."""
168208 responses .add (responses .GET , PrecommitClientTest .get_global_service_mock ('service-api-info' ), status = 200 ,
169209 content_type = "application/json" , body = '{"apiVersion": 6}' )
170210 precommit_client = PrecommitClient (PrecommitClientTest ._get_precommit_client_config (),
171- repository_path = REPO_PATH , analyzed_file = ANALYZED_FILE , verify = False ,
211+ repository_path = REPO_PATH , path_prefix = path_prefix ,
212+ analyzed_file = ANALYZED_FILE_PATH , verify = False ,
172213 omit_links_to_findings = True , fetch_existing_findings = fetch_existing_findings ,
173214 fetch_existing_findings_in_changes = fetch_existing_findings_in_changes )
174215 precommit_client ._calculate_modifications = Mock ()
@@ -195,35 +236,42 @@ def _get_no_changed_files():
195236 @staticmethod
196237 def _get_changed_file ():
197238 """Helper that returns a single changed path and content."""
198- return {ANALYZED_FILE : 'def foo():\n pass' }
239+ return {ANALYZED_FILE_NAME : 'def foo():\n pass' }
199240
200241 @staticmethod
201242 def _get_no_deleted_files ():
202243 """Helper that provides an empty list of deleted files."""
203244 return []
204245
205246 @staticmethod
206- def _get_findings_churn (added_findings = None , findings_in_changed_code = None , removed_findings = None ):
247+ def _get_findings_churn (path_prefix , added_findings = None , findings_in_changed_code = None , removed_findings = None ):
207248 """Returns the precommit findings churn as dict for the provided findings number list.
208249 Findings can be provided as list of integers each of which represents a finding instance."""
209- return {"addedFindings" : PrecommitClientTest ._get_findings_as_dicts (added_findings ),
210- "findingsInChangedCode" : PrecommitClientTest ._get_findings_as_dicts (findings_in_changed_code ),
211- 'removedFindings' : PrecommitClientTest ._get_findings_as_dicts (removed_findings )}
250+ return {"addedFindings" : PrecommitClientTest ._get_findings_as_dicts (added_findings , path_prefix ),
251+ "findingsInChangedCode" : PrecommitClientTest ._get_findings_as_dicts (findings_in_changed_code ,
252+ path_prefix ),
253+ 'removedFindings' : PrecommitClientTest ._get_findings_as_dicts (removed_findings , path_prefix )}
212254
213255 @staticmethod
214- def _get_findings_as_dicts (findings_number_list ):
256+ def _get_findings_as_dicts (findings_number_list , path_prefix ):
215257 """Transforms a list of integers each of which represents a finding instance into a list of dicts."""
216258 if not findings_number_list :
217259 return []
218260 findings_dicts = []
219261 for finding_number in findings_number_list :
220262 findings_dict = {'id' : str (finding_number ), 'typeId' : 'id%i' % finding_number ,
221263 'message' : 'message%i' % finding_number , 'assessment' : 'RED' ,
222- 'location' : {'uniformPath' : ANALYZED_FILE , 'rawStartLine' : finding_number }}
264+ 'location' : {'uniformPath' : path_prefix + ANALYZED_FILE_NAME ,
265+ 'rawStartLine' : finding_number }}
223266 findings_dicts .append (findings_dict )
224267 return findings_dicts
225268
226269 def assert_findings_ids (self , actual_findings , expected_ids ):
227270 """Asserts that the given findings have the specified ids."""
228271 actual_findings_ids = [int (actual_finding .finding_id ) for actual_finding in actual_findings ]
229272 self .assertListEqual (actual_findings_ids , expected_ids )
273+
274+ def assert_findings_paths (self , actual_findings , expected_paths ):
275+ """Asserts that the given findings have the specified paths."""
276+ actual_findings_paths = [actual_finding .uniformPath for actual_finding in actual_findings ]
277+ self .assertListEqual (actual_findings_paths , expected_paths )
0 commit comments