diff --git a/secureli/container.py b/secureli/container.py index b1d548c8..38d5b1da 100644 --- a/secureli/container.py +++ b/secureli/container.py @@ -140,6 +140,7 @@ class Container(containers.DeclarativeContainer): pii_scanner_service = providers.Factory( PiiScannerService, repo_files=repo_files_repository, + echo=echo, ) updater_service = providers.Factory( diff --git a/secureli/modules/pii_scanner/pii_scanner.py b/secureli/modules/pii_scanner/pii_scanner.py index 181d784d..5faf8cba 100644 --- a/secureli/modules/pii_scanner/pii_scanner.py +++ b/secureli/modules/pii_scanner/pii_scanner.py @@ -12,6 +12,7 @@ from pathlib import Path import pydantic +from secureli.modules.shared.abstractions.echo import EchoAbstraction from secureli.modules.shared.models.scan import ( ScanFailure, ScanMode, @@ -37,8 +38,10 @@ class PiiScannerService: def __init__( self, repo_files: RepoFilesRepository, + echo: EchoAbstraction, ): self.repo_files = repo_files + self.echo = echo def scan_repo( self, @@ -85,7 +88,7 @@ def scan_repo( current_line_num = 0 except Exception as e: - print(f"Error scanning {file_name}: {e}") + self.echo.print(f"Error PII scanning {file_name}: {e}") scan_failures = self._generate_scan_failures(pii_found_files) output = self._generate_scan_output(pii_found, not pii_found) @@ -125,7 +128,7 @@ def _get_files_list( if files: file_paths = list(filter(lambda file: file in file_paths, files)) - elif scan_mode == ScanMode.ALL_FILES: + if scan_mode == ScanMode.ALL_FILES: file_paths = self.repo_files.list_repo_files(folder_path) return list( diff --git a/tests/modules/pii_scanner/test_pii_scanner_service.py b/tests/modules/pii_scanner/test_pii_scanner_service.py index 44034505..36761cb7 100644 --- a/tests/modules/pii_scanner/test_pii_scanner_service.py +++ b/tests/modules/pii_scanner/test_pii_scanner_service.py @@ -1,6 +1,8 @@ import pytest from pytest_mock import MockerFixture -from unittest.mock import MagicMock +from unittest.mock import MagicMock, Mock +import builtins +import contextlib, io from pathlib import Path from secureli.modules.pii_scanner.pii_scanner import PiiScannerService from secureli.modules.shared.models.scan import ScanMode @@ -17,6 +19,12 @@ def mock_repo_files_repository() -> MagicMock: return mock_repo_files_repository +@pytest.fixture() +def mock_echo() -> MagicMock: + mock_echo = MagicMock() + return mock_echo + + @pytest.fixture() def mock_open_fn(mocker: MockerFixture) -> MagicMock: # The below data wouldn't ACTUALLY count as PII, but using fake PII here would prevent this code @@ -40,8 +48,10 @@ def mock_re(mocker: MockerFixture) -> MagicMock: @pytest.fixture() -def pii_scanner_service(mock_repo_files_repository: MagicMock) -> PiiScannerService: - return PiiScannerService(mock_repo_files_repository) +def pii_scanner_service( + mock_repo_files_repository: MagicMock, mock_echo: MagicMock +) -> PiiScannerService: + return PiiScannerService(mock_repo_files_repository, mock_echo) def test_that_pii_scanner_service_finds_potential_pii( @@ -74,6 +84,7 @@ def test_that_pii_scanner_service_ignores_excluded_file_extensions( pii_scanner_service: PiiScannerService, mock_repo_files_repository: MagicMock, mock_open_fn: MagicMock, + mock_re: MagicMock, ): mock_repo_files_repository.list_staged_files.return_value = ["fake_file_path.md"] @@ -101,3 +112,18 @@ def test_that_pii_scanner_service_only_scans_specific_files_if_provided( assert scan_result.successful == False assert len(scan_result.failures) == 1 assert scan_result.failures[0].file == specified_file + + +def test_that_pii_scanner_prints_when_exceptions_encountered( + pii_scanner_service: PiiScannerService, + mock_open_fn: MagicMock, + mock_echo: MagicMock, +): + mock_open_fn.side_effect = Exception("Oh no") + pii_scanner_service.scan_repo( + test_folder_path, + ScanMode.STAGED_ONLY, + ) + + mock_echo.print.assert_called_once() + assert "Error PII scanning" in mock_echo.print.call_args.args[0]