|
4 | 4 | from click.testing import CliRunner |
5 | 5 | from twyn import cli |
6 | 6 | from twyn.base.constants import AvailableLoggingLevels |
| 7 | +from twyn.base.exceptions import TwynError |
7 | 8 | from twyn.trusted_packages.cache_handler import CacheEntry, CacheHandler |
8 | 9 | from twyn.trusted_packages.trusted_packages import TyposquatCheckResult |
9 | 10 |
|
@@ -216,3 +217,35 @@ def test_dependency_file_name_has_to_be_recognized(self): |
216 | 217 | assert isinstance(result.exception, SystemExit) |
217 | 218 | assert result.exit_code == 2 |
218 | 219 | assert "Dependency file name not supported." in result.output |
| 220 | + |
| 221 | + @patch("twyn.cli.check_dependencies") |
| 222 | + def test_base_twyn_error_is_caught_and_wrapped_in_cli_error(self, mock_check_dependencies, caplog): |
| 223 | + """Test that BaseTwynError is caught and wrapped in CliError.""" |
| 224 | + runner = CliRunner() |
| 225 | + |
| 226 | + # Mock check_dependencies to raise a BaseTwynError |
| 227 | + test_error = TwynError("Test base error") |
| 228 | + test_error.message = "Test base error message" |
| 229 | + mock_check_dependencies.side_effect = test_error |
| 230 | + |
| 231 | + result = runner.invoke(cli.run, ["--dependency", "requests"]) |
| 232 | + |
| 233 | + assert result.exit_code == 1 |
| 234 | + assert isinstance(result.exception, SystemExit) |
| 235 | + # Check that the error message was logged |
| 236 | + assert "Test base error message" in caplog.text |
| 237 | + |
| 238 | + @patch("twyn.cli.check_dependencies") |
| 239 | + def test_unhandled_exception_is_caught_and_wrapped_in_cli_error(self, mock_check_dependencies, caplog): |
| 240 | + """Test that unhandled exceptions are caught and wrapped in CliError.""" |
| 241 | + runner = CliRunner() |
| 242 | + |
| 243 | + # Mock check_dependencies to raise a generic exception |
| 244 | + mock_check_dependencies.side_effect = ValueError("Unexpected error") |
| 245 | + |
| 246 | + result = runner.invoke(cli.run, ["--dependency", "requests"]) |
| 247 | + |
| 248 | + assert result.exit_code == 1 |
| 249 | + assert isinstance(result.exception, SystemExit) |
| 250 | + # Check that the generic error message was logged |
| 251 | + assert "Unhandled exception occured." in caplog.text |
0 commit comments