From ef53acb28b7d8d7b5d7c94ac2c4b3f785254a3c4 Mon Sep 17 00:00:00 2001 From: Ramon Giovane <40267373+RamonGiovane@users.noreply.github.com> Date: Fri, 10 Feb 2023 01:33:38 +0000 Subject: [PATCH 1/3] Fix .pre-commit-config.yaml isort version that broke rencently --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d5162806..772f5f65 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -36,7 +36,7 @@ repos: hooks: - id: black - repo: https://github.com/PyCQA/isort - rev: 5.10.1 + rev: 5.12.0 hooks: - id: isort - repo: https://github.com/asottile/yesqa From ae729d52cf094303f979ed074acbb8c2d236f119 Mon Sep 17 00:00:00 2001 From: Ramon Giovane <40267373+RamonGiovane@users.noreply.github.com> Date: Fri, 10 Feb 2023 01:59:03 +0000 Subject: [PATCH 2/3] Option to hide local variables values in the test result output --- ward/_run.py | 7 +++++++ ward/_terminal.py | 6 +++++- ward/config.py | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ward/_run.py b/ward/_run.py index 54bd0f97..ab3c3719 100644 --- a/ward/_run.py +++ b/ward/_run.py @@ -151,6 +151,11 @@ def run(ctx: click.Context): help="Record and display duration of n longest running tests", default=0, ) +@click.option( + "--show-locals/--hide-locals", + help="Print all tests without executing them", + default=True, +) @click.option( "--dry-run/--no-dry-run", help="Print all tests without executing them", @@ -174,6 +179,7 @@ def test( capture_output: bool, show_slowest: int, show_diff_symbols: bool, + show_locals: bool, dry_run: bool, hook_module: Tuple[str], ): @@ -227,6 +233,7 @@ def test( progress_styles=progress_styles, config_path=config_path, show_diff_symbols=show_diff_symbols, + show_locals=show_locals, ) for renderable in print_before: rich_console.print(renderable) diff --git a/ward/_terminal.py b/ward/_terminal.py index 858206fa..37a18c90 100644 --- a/ward/_terminal.py +++ b/ward/_terminal.py @@ -676,6 +676,7 @@ def __init__( progress_styles: List[TestProgressStyle], config_path: Optional[Path], show_diff_symbols: bool = False, + show_locals: bool = True, ): self.console = console self.suite = suite @@ -683,6 +684,7 @@ def __init__( self.progress_styles = progress_styles self.config_path = config_path self.show_diff_symbols = show_diff_symbols + self.show_locals = show_locals self.terminal_size = get_terminal_size() def output_all_test_results( @@ -951,7 +953,9 @@ def print_traceback(self, err): # The first frame contains library internal code which is not # relevant to end users, so skip over it. trace = trace.tb_next - tb = Traceback.from_exception(err.__class__, err, trace, show_locals=True) + tb = Traceback.from_exception( + err.__class__, err, trace, show_locals=self.show_locals + ) self.console.print(Padding(tb, pad=(0, 2, 1, 2))) else: self.console.print(str(err)) diff --git a/ward/config.py b/ward/config.py index 9a3f5ec9..fa687407 100644 --- a/ward/config.py +++ b/ward/config.py @@ -23,6 +23,7 @@ class Config: capture_output: bool show_slowest: int show_diff_symbols: bool + show_locals: bool dry_run: bool hook_module: Tuple[str] progress_style: Tuple[str] From c97e9935415c7d5f7df74f459b987031283869d8 Mon Sep 17 00:00:00 2001 From: Ramon Giovane <40267373+RamonGiovane@users.noreply.github.com> Date: Mon, 13 Feb 2023 02:11:19 +0000 Subject: [PATCH 3/3] Cover missing parts of TestResultWriter.print_traceback in tests --- tests/test_terminal.py | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/test_terminal.py b/tests/test_terminal.py index 813f7bdf..70f7734f 100644 --- a/tests/test_terminal.py +++ b/tests/test_terminal.py @@ -299,6 +299,18 @@ def writer(console=mock_rich_console): ) +@fixture +def writer_hiding_locals(console=mock_rich_console): + yield TestResultWriter( + console, + Suite([]), + TestOutputStyle.LIVE, + [TestProgressStyle.INLINE], + None, + show_locals=False, + ) + + for left, right in [ ("abc", "abd"), (123, 124), @@ -398,3 +410,46 @@ def _(console=mock_rich_console): result = result_writer.output_all_test_results(_ for _ in ()) assert result == [] assert not console.print.called + + +@test("TestResultWriter.print_traceback with stacktrace showing locals") +def _(writer=writer): + def internal_func3(**params): + return 1 / 0 + + def internal_func2(name): + age = 18 + internal_func3(name=name, age=age) + + def internal_func1(): + name = "John" + return internal_func2(name=name) + + try: + internal_func1() + except ZeroDivisionError as ex: + writer.print_traceback(ex) + + +@test("TestResultWriter.print_traceback with stacktrace and hiding locals") +def _(writer=writer_hiding_locals): + def internal_func3(**params): + return 1 / 0 + + def internal_func2(name): + age = 18 + internal_func3(name=name, age=age) + + def internal_func1(): + name = "John" + return internal_func2(name=name) + + try: + internal_func1() + except ZeroDivisionError as ex: + writer.print_traceback(ex) + + +@test("TestResultWriter.print_traceback without stacktrace") +def _(writer=writer): + writer.print_traceback(Exception("Some error"))