Skip to content

Commit 1b17eaf

Browse files
isidorejmasonleegregorrieglerblade290nitsanavni
committed
- F better name
Co-Authored-By: jmasonlee <262853+jmasonlee@users.noreply.github.com> Co-Authored-By: Gregor Riegler <rieglerg85@gmail.com> Co-Authored-By: blade290 <43077216+blade290@users.noreply.github.com> Co-Authored-By: Nitsan Avni <nitsanav@gmail.com> Co-Authored-By: bhargavgundu <7643639+bhargavgundu@users.noreply.github.com>
1 parent cb4bc02 commit 1b17eaf

3 files changed

Lines changed: 28 additions & 34 deletions

File tree

approvaltests/inline/inline_options.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,31 @@ class SemiAutomaticInlineOptions(InlineOptions):
2828
def apply(self, options: "Options") -> "Options":
2929
return options.with_reporter(
3030
InlinePythonReporter(ReporterThatAutomaticallyApproves(),
31-
footer=lambda __: DELETE_ME_TO_APPROVE_)
31+
create_footer_function=lambda __: DELETE_ME_TO_APPROVE_)
3232
)
3333

3434
return SemiAutomaticInlineOptions()
3535

3636
@staticmethod
37-
def applesauce():
38-
return InlineOptions()
37+
def semi_automatic_with_previous_approved():
38+
from approvaltests.namer.inline_python_reporter import InlinePythonReporter
39+
from approvaltests.reporters import ReporterThatAutomaticallyApproves
40+
41+
def create_previous_capture_footer(approved_path):
42+
approved_text = Path(approved_path).read_text()
43+
approved_text = approved_text.rsplit("\n", 1)[0]
44+
approved_text = approved_text.rsplit(PREVIOUS_RESULT_, 1)[-1]
45+
previous_result_stuff = lambda: "\n" + PREVIOUS_RESULT_ + approved_text
46+
return DELETE_ME_TO_APPROVE_ + previous_result_stuff()
47+
48+
class PreviousCaptureInlineOptions(InlineOptions):
49+
def apply(self, options: "Options") -> "Options":
50+
return options.with_reporter(
51+
InlinePythonReporter(ReporterThatAutomaticallyApproves(),
52+
create_footer_function=create_previous_capture_footer)
53+
)
54+
55+
return PreviousCaptureInlineOptions()
3956

4057
def apply(self, options: "Options") -> "Options":
4158

@@ -55,22 +72,4 @@ def apply(self, options: "Options") -> "Options":
5572

5673
return ShowCodeInlineOptions() if do_show_code else DoNotShowCodeInlineOptions()
5774

58-
@staticmethod
59-
def previous_capture():
60-
from approvaltests.namer.inline_python_reporter import InlinePythonReporter
61-
from approvaltests.reporters import ReporterThatAutomaticallyApproves
62-
63-
def create_previous_capture_suffix(approved_path):
64-
approved_text = Path(approved_path).read_text()
65-
approved_text = approved_text.rsplit("\n", 1)[0]
66-
approved_text = approved_text.rsplit(PREVIOUS_RESULT_, 1)[-1]
67-
previous_result_stuff = lambda: "\n" + PREVIOUS_RESULT_ + approved_text
68-
return DELETE_ME_TO_APPROVE_ + previous_result_stuff()
69-
class PreviousCaptureInlineOptions(InlineOptions):
70-
def apply(self, options: "Options") -> "Options":
71-
return options.with_reporter(
72-
InlinePythonReporter(ReporterThatAutomaticallyApproves(),
73-
footer=create_previous_capture_suffix)
74-
)
75-
76-
return PreviousCaptureInlineOptions()
75+

approvaltests/namer/inline_python_reporter.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import tempfile
22
from inspect import FrameInfo
33
from pathlib import Path
4+
from typing import Callable
45

56
from approvaltests import Reporter, StackFrameNamer
67
from approvaltests.inline.split_code import SplitCode
78

8-
PREVIOUS_RESULT_ = "vvvvv PREVIOUS RESULT vvvvv\n"
9-
10-
DELETE_ME_TO_APPROVE_ = "\n***** DELETE ME TO APPROVE *****"
11-
12-
139
class InlinePythonReporter(Reporter):
14-
def __init__(self, reporter, footer=None):
10+
def __init__(self, reporter: Reporter, create_footer_function: Callable[[str],str]=None):
1511
self.diffReporter = reporter
16-
self.footer = footer
17-
self.semi_automatic_extra_line = ""
12+
self.footer_function = create_footer_function or (lambda __: "")
13+
self.footer = ""
1814

1915
def report(self, received_path: str, approved_path: str) -> bool:
2016
test_source_file = self.get_test_source_file()
21-
if self.footer:
22-
self.semi_automatic_extra_line = self.footer(approved_path)
17+
self.footer = self.footer_function(approved_path)
2318
received_path = self.create_received_file(received_path, test_source_file)
2419
return self.diffReporter.report(received_path, test_source_file)
2520

@@ -31,7 +26,7 @@ def create_received_file(self, received_path: str, test_source_file: str):
3126
code = Path(test_source_file).read_text()
3227

3328
received_text = (
34-
Path(received_path).read_text()[:-1] + self.semi_automatic_extra_line
29+
Path(received_path).read_text()[:-1] + self.footer
3530
)
3631
method_name = StackFrameNamer.get_test_frame().function
3732
new_code = self.swap(received_text, code, method_name)

tests/test_inline_approvals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_inline_with_preserved_approved_text():
196196
vvvvv PREVIOUS RESULT vvvvv
197197
41
198198
"""
199-
options = Options().inline(InlineOptions.previous_capture())
199+
options = Options().inline(InlineOptions.semi_automatic_with_previous_approved())
200200
try:
201201
verify("42", options=options)
202202
except ApprovalException:

0 commit comments

Comments
 (0)