diff --git a/approvaltests/inline/inline_options.py b/approvaltests/inline/inline_options.py index 4158f49d..dfacbbed 100644 --- a/approvaltests/inline/inline_options.py +++ b/approvaltests/inline/inline_options.py @@ -28,14 +28,31 @@ class SemiAutomaticInlineOptions(InlineOptions): def apply(self, options: "Options") -> "Options": return options.with_reporter( InlinePythonReporter(ReporterThatAutomaticallyApproves(), - footer=lambda __: DELETE_ME_TO_APPROVE_) + create_footer_function=lambda __: DELETE_ME_TO_APPROVE_) ) return SemiAutomaticInlineOptions() @staticmethod - def applesauce(): - return InlineOptions() + def semi_automatic_with_previous_approved(): + from approvaltests.namer.inline_python_reporter import InlinePythonReporter + from approvaltests.reporters import ReporterThatAutomaticallyApproves + + def create_previous_capture_footer(approved_path): + approved_text = Path(approved_path).read_text() + approved_text = approved_text.rsplit("\n", 1)[0] + approved_text = approved_text.rsplit(PREVIOUS_RESULT_, 1)[-1] + previous_result_stuff = lambda: "\n" + PREVIOUS_RESULT_ + approved_text + return DELETE_ME_TO_APPROVE_ + previous_result_stuff() + + class PreviousCaptureInlineOptions(InlineOptions): + def apply(self, options: "Options") -> "Options": + return options.with_reporter( + InlinePythonReporter(ReporterThatAutomaticallyApproves(), + create_footer_function=create_previous_capture_footer) + ) + + return PreviousCaptureInlineOptions() def apply(self, options: "Options") -> "Options": @@ -55,22 +72,4 @@ def apply(self, options: "Options") -> "Options": return ShowCodeInlineOptions() if do_show_code else DoNotShowCodeInlineOptions() - @staticmethod - def previous_capture(): - from approvaltests.namer.inline_python_reporter import InlinePythonReporter - from approvaltests.reporters import ReporterThatAutomaticallyApproves - - def create_previous_capture_suffix(approved_path): - approved_text = Path(approved_path).read_text() - approved_text = approved_text.rsplit("\n", 1)[0] - approved_text = approved_text.rsplit(PREVIOUS_RESULT_, 1)[-1] - previous_result_stuff = lambda: "\n" + PREVIOUS_RESULT_ + approved_text - return DELETE_ME_TO_APPROVE_ + previous_result_stuff() - class PreviousCaptureInlineOptions(InlineOptions): - def apply(self, options: "Options") -> "Options": - return options.with_reporter( - InlinePythonReporter(ReporterThatAutomaticallyApproves(), - footer=create_previous_capture_suffix) - ) - - return PreviousCaptureInlineOptions() + diff --git a/approvaltests/namer/inline_python_reporter.py b/approvaltests/namer/inline_python_reporter.py index eae6fedf..7c3ccd1d 100644 --- a/approvaltests/namer/inline_python_reporter.py +++ b/approvaltests/namer/inline_python_reporter.py @@ -1,25 +1,20 @@ import tempfile from inspect import FrameInfo from pathlib import Path +from typing import Callable from approvaltests import Reporter, StackFrameNamer from approvaltests.inline.split_code import SplitCode -PREVIOUS_RESULT_ = "vvvvv PREVIOUS RESULT vvvvv\n" - -DELETE_ME_TO_APPROVE_ = "\n***** DELETE ME TO APPROVE *****" - - class InlinePythonReporter(Reporter): - def __init__(self, reporter, footer=None): + def __init__(self, reporter: Reporter, create_footer_function: Callable[[str],str]=None): self.diffReporter = reporter - self.footer = footer - self.semi_automatic_extra_line = "" + self.footer_function = create_footer_function or (lambda __: "") + self.footer = "" def report(self, received_path: str, approved_path: str) -> bool: test_source_file = self.get_test_source_file() - if self.footer: - self.semi_automatic_extra_line = self.footer(approved_path) + self.footer = self.footer_function(approved_path) received_path = self.create_received_file(received_path, test_source_file) return self.diffReporter.report(received_path, test_source_file) @@ -31,7 +26,7 @@ def create_received_file(self, received_path: str, test_source_file: str): code = Path(test_source_file).read_text() received_text = ( - Path(received_path).read_text()[:-1] + self.semi_automatic_extra_line + Path(received_path).read_text()[:-1] + self.footer ) method_name = StackFrameNamer.get_test_frame().function new_code = self.swap(received_text, code, method_name) diff --git a/tests/test_inline_approvals.py b/tests/test_inline_approvals.py index 2823108c..7f2d9d3f 100644 --- a/tests/test_inline_approvals.py +++ b/tests/test_inline_approvals.py @@ -196,7 +196,7 @@ def test_inline_with_preserved_approved_text(): vvvvv PREVIOUS RESULT vvvvv 41 """ - options = Options().inline(InlineOptions.previous_capture()) + options = Options().inline(InlineOptions.semi_automatic_with_previous_approved()) try: verify("42", options=options) except ApprovalException: