From 44d46fa0d74c4d0b538bb7f60e01fcda677a3404 Mon Sep 17 00:00:00 2001 From: backwardspy Date: Wed, 5 Oct 2022 16:26:24 +0100 Subject: [PATCH] add max-frames option to limit traceback length --- tests/test_terminal.py | 3 ++- ward/_run.py | 8 ++++++++ ward/_terminal.py | 6 +++++- ward/config.py | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/test_terminal.py b/tests/test_terminal.py index 813f7bdf..2fbdbcfd 100644 --- a/tests/test_terminal.py +++ b/tests/test_terminal.py @@ -295,7 +295,7 @@ def mock_rich_console(): @fixture def writer(console=mock_rich_console): yield TestResultWriter( - console, Suite([]), TestOutputStyle.LIVE, [TestProgressStyle.INLINE], None + console, Suite([]), TestOutputStyle.LIVE, [TestProgressStyle.INLINE], None, 100 ) @@ -393,6 +393,7 @@ def _(console=mock_rich_console): test_output_style=TestOutputStyle.TEST_PER_LINE, progress_styles=[TestProgressStyle.INLINE], config_path=None, + max_frames=100, ) result = result_writer.output_all_test_results(_ for _ in ()) diff --git a/ward/_run.py b/ward/_run.py index 54bd0f97..03595d87 100644 --- a/ward/_run.py +++ b/ward/_run.py @@ -126,6 +126,12 @@ def run(ctx: click.Context): Pass multiple times to enable multiple styles. """, ) +@click.option( + "--max-frames", + type=int, + default=100, + help="The maximum number of stack frames to show in tracebacks.", +) @click.option( "--order", type=click.Choice(["standard", "random"], case_sensitive=False), @@ -170,6 +176,7 @@ def test( fail_limit: Optional[int], test_output_style: str, progress_style: List[str], + max_frames: int, order: str, capture_output: bool, show_slowest: int, @@ -227,6 +234,7 @@ def test( progress_styles=progress_styles, config_path=config_path, show_diff_symbols=show_diff_symbols, + max_frames=max_frames, ) for renderable in print_before: rich_console.print(renderable) diff --git a/ward/_terminal.py b/ward/_terminal.py index 858206fa..26d2c837 100644 --- a/ward/_terminal.py +++ b/ward/_terminal.py @@ -675,6 +675,7 @@ def __init__( test_output_style: TestOutputStyle, progress_styles: List[TestProgressStyle], config_path: Optional[Path], + max_frames: int, show_diff_symbols: bool = False, ): self.console = console @@ -684,6 +685,7 @@ def __init__( self.config_path = config_path self.show_diff_symbols = show_diff_symbols self.terminal_size = get_terminal_size() + self.max_frames = max_frames def output_all_test_results( self, @@ -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=True, max_frames=self.max_frames + ) 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..0580e102 100644 --- a/ward/config.py +++ b/ward/config.py @@ -27,3 +27,4 @@ class Config: hook_module: Tuple[str] progress_style: Tuple[str] plugin_config: Dict[str, Dict[str, Any]] + max_frames: int