Skip to content

Commit

Permalink
dev(hansbug): simplify keyboard interrupt information
Browse files Browse the repository at this point in the history
  • Loading branch information
HansBug committed Sep 17, 2022
1 parent 3ec229e commit 1b99a46
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 9 additions & 0 deletions igm/entry/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def print_exception(err: BaseException, print: Optional[Callable] = None):
print(f'{type(err).__name__}: {err.args}')


class KeyboardInterrupted(ClickWarningException):
exit_code = 0x7

def __init__(self, msg=None):
ClickWarningException.__init__(self, msg or 'Interrupted.')


def command_wrap():
def _decorator(func):
@wraps(func)
Expand All @@ -54,6 +61,8 @@ def _new_func(*args, **kwargs):
return func(*args, **kwargs)
except ClickException:
raise
except KeyboardInterrupt:
raise KeyboardInterrupted
except BaseException as err:
click.secho('Unexpected error found when running IGM CLI!', fg='red', file=sys.stderr)
print_exception(err, partial(click.secho, fg='red', file=sys.stderr))
Expand Down
13 changes: 9 additions & 4 deletions templates/test/template/igmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def e3():
raise TypeError('type', 'error', 233)


def ki():
raise KeyboardInterrupt


igm_project(
name={{ (user.name | str + '-demo') | potc }},
name={{(user.name | str + '-demo') | potc}},
version='0.3.2',
template_name={{ template.name | potc }},
template_version={{ template.version | potc }},
created_at={{ py.time.time() | trepr | potc }},
template_name={{template.name | potc}},
template_version={{template.version | potc}},
created_at={{py.time.time() | trepr | potc}},
scripts={
None: cpy('main.py'),
'install': cpip('install', '-r', 'requirements.txt'),
Expand All @@ -52,5 +56,6 @@ def e3():
'e1': e1,
'e2': e2,
'e3': e3,
'ki': ki,
}
)
6 changes: 6 additions & 0 deletions test/entry/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ def test_test_print_error(self, test_project_dir):
assert result.stdout.strip() == 'Call function \'e3\'.'
assert result.stderr.strip().splitlines(keepends=False)[-1] == 'TypeError: (\'type\', \'error\', 233)'

result = simulate_entry(get_cli_entry(), ['igm', 'run', 'ki'])
assert result.exitcode == 0x7
assert result.error is None
assert result.stdout.strip() == 'Call function \'ki\'.'
assert result.stderr.strip() == 'Interrupted.'

def test_fake_without_default(self, project_without_default_dir):
result = simulate_entry(get_cli_entry(), ['igm', 'run'])
assert result.exitcode == 0x31
Expand Down

0 comments on commit 1b99a46

Please sign in to comment.