|  | 
|  | 1 | +import click | 
|  | 2 | +import pytest | 
|  | 3 | +from click import ClickException | 
|  | 4 | +from git import InvalidGitRepositoryError | 
|  | 5 | + | 
|  | 6 | +from cli.code_scanner import _handle_exception # noqa | 
|  | 7 | +from cli.exceptions import custom_exceptions | 
|  | 8 | + | 
|  | 9 | + | 
|  | 10 | +@pytest.fixture() | 
|  | 11 | +def ctx(): | 
|  | 12 | +    return click.Context(click.Command('path'), obj={'verbose': False, 'output': 'text'}) | 
|  | 13 | + | 
|  | 14 | + | 
|  | 15 | +@pytest.mark.parametrize('exception, expected_soft_fail', [ | 
|  | 16 | +    (custom_exceptions.CycodeError(400, 'msg'), True), | 
|  | 17 | +    (custom_exceptions.ScanAsyncError('msg'), True), | 
|  | 18 | +    (custom_exceptions.HttpUnauthorizedError('msg'), True), | 
|  | 19 | +    (custom_exceptions.ZipTooLargeError(1000), True), | 
|  | 20 | +    (InvalidGitRepositoryError(), None), | 
|  | 21 | +]) | 
|  | 22 | +def test_handle_exception_soft_fail(ctx: click.Context, exception: custom_exceptions.CycodeError, expected_soft_fail: bool): | 
|  | 23 | +    with ctx: | 
|  | 24 | +        _handle_exception(ctx, exception) | 
|  | 25 | + | 
|  | 26 | +        assert ctx.obj.get('did_fail') is True | 
|  | 27 | +        assert ctx.obj.get('soft_fail') is expected_soft_fail | 
|  | 28 | + | 
|  | 29 | + | 
|  | 30 | +def test_handle_exception_unhandled_error(ctx: click.Context): | 
|  | 31 | +    with ctx: | 
|  | 32 | +        with pytest.raises(ClickException): | 
|  | 33 | +            _handle_exception(ctx, ValueError('test')) | 
|  | 34 | + | 
|  | 35 | +            assert ctx.obj.get('did_fail') is True | 
|  | 36 | +            assert ctx.obj.get('soft_fail') is None | 
|  | 37 | + | 
|  | 38 | + | 
|  | 39 | +def test_handle_exception_click_error(ctx: click.Context): | 
|  | 40 | +    with ctx: | 
|  | 41 | +        with pytest.raises(ClickException): | 
|  | 42 | +            _handle_exception(ctx, click.ClickException('test')) | 
|  | 43 | + | 
|  | 44 | +            assert ctx.obj.get('did_fail') is True | 
|  | 45 | +            assert ctx.obj.get('soft_fail') is None | 
|  | 46 | + | 
|  | 47 | + | 
|  | 48 | +def test_handle_exception_verbose(monkeypatch): | 
|  | 49 | +    ctx = click.Context(click.Command('path'), obj={'verbose': True, 'output': 'text'}) | 
|  | 50 | + | 
|  | 51 | +    def mock_secho(msg, *_, **__): | 
|  | 52 | +        assert 'Error:' in msg | 
|  | 53 | + | 
|  | 54 | +    monkeypatch.setattr(click, 'secho', mock_secho) | 
|  | 55 | + | 
|  | 56 | +    with ctx: | 
|  | 57 | +        with pytest.raises(ClickException): | 
|  | 58 | +            _handle_exception(ctx, ValueError('test')) | 
0 commit comments