|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from cycode.cli.consts import IAC_SCAN_TYPE, SAST_SCAN_TYPE, SCA_SCAN_TYPE, SECRET_SCAN_TYPE |
| 5 | +from cycode.cli.printers.utils.detection_data import get_detection_file_path |
| 6 | + |
| 7 | + |
| 8 | +def _make_detection(**details: str) -> MagicMock: |
| 9 | + detection = MagicMock() |
| 10 | + detection.detection_details = dict(details) |
| 11 | + return detection |
| 12 | + |
| 13 | + |
| 14 | +def test_get_detection_file_path_sca_uses_file_path() -> None: |
| 15 | + detection = _make_detection(file_name='package.json', file_path='/repo/path/package.json') |
| 16 | + result = get_detection_file_path(SCA_SCAN_TYPE, detection) |
| 17 | + assert result == Path('/repo/path/package.json') |
| 18 | + |
| 19 | + |
| 20 | +def test_get_detection_file_path_iac_uses_file_path() -> None: |
| 21 | + detection = _make_detection(file_name='main.tf', file_path='/repo/infra/main.tf') |
| 22 | + result = get_detection_file_path(IAC_SCAN_TYPE, detection) |
| 23 | + assert result == Path('/repo/infra/main.tf') |
| 24 | + |
| 25 | + |
| 26 | +def test_get_detection_file_path_sca_fallback_empty() -> None: |
| 27 | + detection = _make_detection() |
| 28 | + result = get_detection_file_path(SCA_SCAN_TYPE, detection) |
| 29 | + assert result == Path('') |
| 30 | + |
| 31 | + |
| 32 | +def test_get_detection_file_path_secret() -> None: |
| 33 | + detection = _make_detection(file_path='/repo/src', file_name='.env') |
| 34 | + result = get_detection_file_path(SECRET_SCAN_TYPE, detection) |
| 35 | + assert result == Path('/repo/src/.env') |
| 36 | + |
| 37 | + |
| 38 | +def test_get_detection_file_path_sast() -> None: |
| 39 | + detection = _make_detection(file_path='repo/src/app.py') |
| 40 | + result = get_detection_file_path(SAST_SCAN_TYPE, detection) |
| 41 | + assert result == Path('/repo/src/app.py') |
0 commit comments