From 02a9951c629cd87af3113315c59059993ff159bc Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Fri, 29 Jul 2022 17:01:53 +0100 Subject: [PATCH 1/7] Use custom exception handler --- aiidalab_launch/__main__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/aiidalab_launch/__main__.py b/aiidalab_launch/__main__.py index a71c5f7..b83c076 100644 --- a/aiidalab_launch/__main__.py +++ b/aiidalab_launch/__main__.py @@ -8,6 +8,7 @@ import getpass import logging import socket +import sys from pathlib import Path from textwrap import wrap @@ -67,6 +68,11 @@ pass_app_state = click.make_pass_decorator(ApplicationState, ensure=True) +def exception_handler(exception_type, exception, traceback): # noqa: U100 + print(f"Unexpected {exception_type.__name__}: {exception}") + print("Use verbose mode `aiidalab-launch --verbose` to see full stacktrace") + + def with_profile(cmd): def callback(ctx, param, value): # noqa: U100 app_state = ctx.ensure_object(ApplicationState) @@ -100,6 +106,10 @@ def cli(app_state, verbose): err=True, ) + # Hide stack traces by default + if verbose == 0: + sys.excepthook = exception_handler + LOGGER.info(f"Configuration file path: {app_state.config_path}") latest_version = get_latest_version(timeout=0.1) From 004770220620f5212e82ed44249183ab33d107ab Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Fri, 29 Jul 2022 18:11:58 +0100 Subject: [PATCH 2/7] Output to stderr --- aiidalab_launch/__main__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aiidalab_launch/__main__.py b/aiidalab_launch/__main__.py index b83c076..cd9789a 100644 --- a/aiidalab_launch/__main__.py +++ b/aiidalab_launch/__main__.py @@ -69,8 +69,10 @@ def exception_handler(exception_type, exception, traceback): # noqa: U100 - print(f"Unexpected {exception_type.__name__}: {exception}") - print("Use verbose mode `aiidalab-launch --verbose` to see full stacktrace") + click.echo(f"Unexpected {exception_type.__name__}: {exception}", err=True) + click.echo( + "Use verbose mode `aiidalab-launch --verbose` to see full stacktrace", err=True + ) def with_profile(cmd): From e59b45a5264cdf6e0534edad57d17f43c7745bf9 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Fri, 29 Jul 2022 18:13:54 +0100 Subject: [PATCH 3/7] Try test --- tests/test_cli.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 44fe80d..5033c79 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -51,6 +51,23 @@ def test_version_verbose_logging(): assert "Verbose logging is enabled." in result.output.strip() +def test_hidden_stacktrace(): + """ + Arrange/Act: Run `profiles edit invalid` subcommand. + Assert: The output does not contain stacktrace. + """ + runner: CliRunner = CliRunner() + with pytest.raises(ValueError): + result: Result = runner.invoke( + cli.cli, ["profiles", "show", "invalid"], catch_exceptions=False + ) + result: Result = runner.invoke( + cli.cli, + ["profiles", "show", "invalid"], + ) + assert isinstance(result.exception, ValueError) + + def test_list_profiles(): runner: CliRunner = CliRunner() result: Result = runner.invoke(cli.cli, ["profiles", "list"]) From 963893cead86dcccd1d4fa6d72ca6da33fdd0cfa Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Fri, 5 Aug 2022 00:28:28 +0100 Subject: [PATCH 4/7] Adress review --- aiidalab_launch/__main__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aiidalab_launch/__main__.py b/aiidalab_launch/__main__.py index cd9789a..2980d01 100644 --- a/aiidalab_launch/__main__.py +++ b/aiidalab_launch/__main__.py @@ -68,15 +68,15 @@ pass_app_state = click.make_pass_decorator(ApplicationState, ensure=True) -def exception_handler(exception_type, exception, traceback): # noqa: U100 +def exception_handler(exception_type, exception, _): click.echo(f"Unexpected {exception_type.__name__}: {exception}", err=True) click.echo( - "Use verbose mode `aiidalab-launch --verbose` to see full stacktrace", err=True + "Use verbose mode `aiidalab-launch --verbose` to see full stack trace", err=True ) def with_profile(cmd): - def callback(ctx, param, value): # noqa: U100 + def callback(ctx, _, value): app_state = ctx.ensure_object(ApplicationState) name = value or app_state.config.default_profile LOGGER.info(f"Using profile: {name}") @@ -108,7 +108,7 @@ def cli(app_state, verbose): err=True, ) - # Hide stack traces by default + # Hide stack trace by default. if verbose == 0: sys.excepthook = exception_handler From ef21a360940ca1c3127942dd57e5d368d80de923 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Fri, 5 Aug 2022 00:33:16 +0100 Subject: [PATCH 5/7] Update test --- tests/test_cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 5033c79..36c155d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -51,10 +51,10 @@ def test_version_verbose_logging(): assert "Verbose logging is enabled." in result.output.strip() -def test_hidden_stacktrace(): +def test_invalid_profile_name_throws(): """ - Arrange/Act: Run `profiles edit invalid` subcommand. - Assert: The output does not contain stacktrace. + Arrange/Act: Run `profiles show invalid` subcommand. + Assert: The command throws an exception due to invalid profile name. """ runner: CliRunner = CliRunner() with pytest.raises(ValueError): From 9463682d21553900cba29edaf3dc6988d913cc06 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Fri, 5 Aug 2022 01:08:13 +0100 Subject: [PATCH 6/7] Add back noqa --- aiidalab_launch/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aiidalab_launch/__main__.py b/aiidalab_launch/__main__.py index 2980d01..7ac8c24 100644 --- a/aiidalab_launch/__main__.py +++ b/aiidalab_launch/__main__.py @@ -68,7 +68,7 @@ pass_app_state = click.make_pass_decorator(ApplicationState, ensure=True) -def exception_handler(exception_type, exception, _): +def exception_handler(exception_type, exception, traceback): # noqa 100 click.echo(f"Unexpected {exception_type.__name__}: {exception}", err=True) click.echo( "Use verbose mode `aiidalab-launch --verbose` to see full stack trace", err=True @@ -76,7 +76,7 @@ def exception_handler(exception_type, exception, _): def with_profile(cmd): - def callback(ctx, _, value): + def callback(ctx, param, value): # noqa 100 app_state = ctx.ensure_object(ApplicationState) name = value or app_state.config.default_profile LOGGER.info(f"Using profile: {name}") From 23682b6d3f15c64a0d4eb88a48ba52a7c002640f Mon Sep 17 00:00:00 2001 From: Carl Simon Adorf Date: Fri, 5 Aug 2022 10:19:45 +0200 Subject: [PATCH 7/7] Fix the inline linting config. --- aiidalab_launch/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aiidalab_launch/__main__.py b/aiidalab_launch/__main__.py index 7ac8c24..552f013 100644 --- a/aiidalab_launch/__main__.py +++ b/aiidalab_launch/__main__.py @@ -68,7 +68,7 @@ pass_app_state = click.make_pass_decorator(ApplicationState, ensure=True) -def exception_handler(exception_type, exception, traceback): # noqa 100 +def exception_handler(exception_type, exception, traceback): # noqa: U100 click.echo(f"Unexpected {exception_type.__name__}: {exception}", err=True) click.echo( "Use verbose mode `aiidalab-launch --verbose` to see full stack trace", err=True @@ -76,7 +76,7 @@ def exception_handler(exception_type, exception, traceback): # noqa 100 def with_profile(cmd): - def callback(ctx, param, value): # noqa 100 + def callback(ctx, param, value): # noqa: U100 app_state = ctx.ensure_object(ApplicationState) name = value or app_state.config.default_profile LOGGER.info(f"Using profile: {name}")