Skip to content

Commit d7e5104

Browse files
committed
CM-53944-Simplified the extension exclude logic and added tests
1 parent fba41bb commit d7e5104

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

cycode/cli/files_collector/file_excluder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _is_file_prefix_supported(self, scan_type: str, file_path: str) -> bool:
7474
if scannable_prefixes:
7575
path = Path(file_path)
7676
file_name = path.name.lower()
77-
return file_name.startswith(scannable_prefixes)
77+
return file_name in scannable_prefixes
7878
return False
7979

8080
def _is_file_extension_supported(self, scan_type: str, filename: str) -> bool:
@@ -104,8 +104,8 @@ def _is_relevant_file_to_scan_common(self, scan_type: str, filename: str) -> boo
104104
)
105105
return False
106106

107-
if (not self._is_file_extension_supported(scan_type, filename)
108-
and not self._is_file_prefix_supported(scan_type, filename)):
107+
if not (self._is_file_extension_supported(scan_type, filename)
108+
or self._is_file_prefix_supported(scan_type, filename)):
109109
logger.debug(
110110
'The document is irrelevant because its extension is not supported, %s',
111111
{'scan_type': scan_type, 'filename': filename},

tests/cli/files_collector/test_file_excluder.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from cycode.cli import consts
44
from cycode.cli.files_collector.file_excluder import _is_file_relevant_for_sca_scan
5-
5+
from cycode.cli.files_collector.file_excluder import Excluder
66

77
class TestIsFileRelevantForScaScan:
88
"""Test the SCA path exclusion logic."""
@@ -38,6 +38,21 @@ def test_files_with_excluded_names_in_filename_should_be_included(self) -> None:
3838
assert _is_file_relevant_for_sca_scan('utils/pycache_cleaner.py') is True
3939
assert _is_file_relevant_for_sca_scan('config/gradle_config.xml') is True
4040

41+
def test_files_with_excluded_extensions_in_should_be_included(self) -> None:
42+
"""Test that files containing excluded directory names in their filename are NOT excluded."""
43+
# These should be INCLUDED because the excluded terms are in the filename, not directory path
44+
excluder = Excluder()
45+
assert excluder._is_relevant_file_to_scan_common('iac','project/cfg/Dockerfile') is True
46+
assert excluder._is_relevant_file_to_scan_common('iac','project/cfg/build.tf') is True
47+
assert excluder._is_relevant_file_to_scan_common('iac', 'project/cfg/build.tf.json') is True
48+
assert excluder._is_relevant_file_to_scan_common('iac', 'project/cfg/config.json') is True
49+
assert excluder._is_relevant_file_to_scan_common('iac', 'project/cfg/config.yaml') is True
50+
assert excluder._is_relevant_file_to_scan_common('iac', 'project/cfg/config.yml') is True
51+
assert excluder._is_relevant_file_to_scan_common('iac','project/cfg/build') is False
52+
assert excluder._is_relevant_file_to_scan_common('iac', 'project/cfg/build') is False
53+
assert excluder._is_relevant_file_to_scan_common('iac', 'project/cfg/Dockerfile.txt') is False
54+
assert excluder._is_relevant_file_to_scan_common('iac', 'project/cfg/config.ini') is False
55+
4156
def test_files_in_regular_directories_should_be_included(self) -> None:
4257
"""Test that files in regular directories (not excluded) are included."""
4358

0 commit comments

Comments
 (0)