Skip to content

Commit db3bc2f

Browse files
authored
Check file paths passed on as tool findings (#637)
do not continue condemodder if any flag path is not found
1 parent 96313a4 commit db3bc2f

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/codemodder/codemodder.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,26 @@ def run(original_args) -> int:
159159
logger.info("codemodder: python/%s", __version__)
160160
logger.info("command: %s %s", Path(sys.argv[0]).name, " ".join(original_args))
161161

162-
# TODO: this should be dict[str, list[Path]]
163-
tool_result_files_map: DefaultDict[str, list[str]] = detect_sarif_tools(
164-
[Path(name) for name in argv.sarif or []]
165-
)
162+
try:
163+
# TODO: this should be dict[str, list[Path]]
164+
tool_result_files_map: DefaultDict[str, list[str]] = detect_sarif_tools(
165+
[Path(name) for name in argv.sarif or []]
166+
)
167+
except FileNotFoundError as err:
168+
logger.error(err)
169+
return 1
170+
166171
tool_result_files_map["sonar"].extend(argv.sonar_issues_json or [])
167172
tool_result_files_map["sonar"].extend(argv.sonar_hotspots_json or [])
168173
tool_result_files_map["defectdojo"] = argv.defectdojo_findings_json or []
169174

175+
for file_name in itertools.chain(*tool_result_files_map.values()):
176+
if not os.path.exists(file_name):
177+
logger.error(
178+
f"FileNotFoundError: [Errno 2] No such file or directory: '{file_name}'"
179+
)
180+
return 1
181+
170182
repo_manager = PythonRepoManager(Path(argv.directory))
171183

172184
try:

tests/test_codemodder.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,27 @@ def test_diff_newline_edge_case(self):
394394
+CSRF_TRUSTED_ORIGINS = ["http://127.0.0.1:8000","http://0.0.0.0:8000","http://172.16.189.10"]
395395
+SESSION_COOKIE_SECURE = True"""
396396
)
397+
398+
@pytest.mark.parametrize(
399+
"flag",
400+
[
401+
"sarif",
402+
"sonar-issues-json",
403+
"sonar-hotspots-json",
404+
"defectdojo-findings-json",
405+
],
406+
)
407+
@mock.patch("codemodder.codetf.CodeTF.write_report")
408+
def test_bad_sarif_path(self, mock_report, caplog, flag):
409+
args = [
410+
"tests/samples",
411+
"--output",
412+
"here.txt",
413+
"--codemod-include=url-sandbox",
414+
f"--{flag}=bad.json",
415+
]
416+
417+
exit_code = run(args)
418+
assert exit_code == 1
419+
assert "No such file or directory: 'bad.json'" in caplog.text
420+
mock_report.assert_not_called()

0 commit comments

Comments
 (0)