Skip to content

Commit

Permalink
- F better name
Browse files Browse the repository at this point in the history
Co-Authored-By: jmasonlee <[email protected]>
Co-Authored-By: Gregor Riegler <[email protected]>
Co-Authored-By: blade290 <[email protected]>
Co-Authored-By: Nitsan Avni <[email protected]>
Co-Authored-By: bhargavgundu <[email protected]>
  • Loading branch information
6 people committed May 19, 2024
1 parent cb4bc02 commit 1b17eaf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
43 changes: 21 additions & 22 deletions approvaltests/inline/inline_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":

Expand All @@ -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()

17 changes: 6 additions & 11 deletions approvaltests/namer/inline_python_reporter.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_inline_approvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1b17eaf

Please sign in to comment.