Skip to content

Commit c4ea9f5

Browse files
isidorejmasonleegregorrieglerblade290nitsanavni
committed
- F wip capture_previous inline approvals
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 170ae66 commit c4ea9f5

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

approvaltests/inline/inline_options.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,18 @@ def apply(self, options: "Options") -> "Options":
4949
return options
5050

5151
return ShowCodeInlineOptions() if do_show_code else DoNotShowCodeInlineOptions()
52+
53+
@staticmethod
54+
def previous_capture():
55+
from approvaltests.namer.inline_python_reporter import InlinePythonReporter
56+
from approvaltests.reporters import ReporterThatAutomaticallyApproves
57+
58+
class SemiAutomaticInlineOptions(InlineOptions):
59+
def apply(self, options: "Options") -> "Options":
60+
return options.with_reporter(
61+
InlinePythonReporter(
62+
ReporterThatAutomaticallyApproves(), add_approval_line=False, previous_result=True
63+
)
64+
)
65+
66+
return SemiAutomaticInlineOptions()

approvaltests/namer/inline_python_reporter.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@
55
from approvaltests import Reporter, StackFrameNamer
66
from approvaltests.inline.split_code import SplitCode
77

8+
DELETE_ME_TO_APPROVE_ = "\n***** DELETE ME TO APPROVE *****"
9+
810

911
class InlinePythonReporter(Reporter):
10-
def __init__(self, reporter, add_approval_line=False):
12+
def __init__(self, reporter, add_approval_line=False, previous_result=None):
1113
self.diffReporter = reporter
1214
self.semi_automatic_extra_line = (
13-
"\n***** DELETE ME TO APPROVE *****" if add_approval_line else ""
15+
DELETE_ME_TO_APPROVE_ if add_approval_line else ""
1416
)
17+
self.previous_result = previous_result
1518

1619
def report(self, received_path: str, approved_path: str) -> bool:
1720
test_source_file = self.get_test_source_file()
21+
if self.previous_result:
22+
previous_result_stuff = lambda: "\n" + "vvvvv PREVIOUS RESULT vvvvv" + "\n" + "41"
23+
self.semi_automatic_extra_line = DELETE_ME_TO_APPROVE_ + previous_result_stuff()
1824
received_path = self.create_received_file(received_path, test_source_file)
1925
return self.diffReporter.report(received_path, test_source_file)
2026

@@ -24,6 +30,7 @@ def get_test_source_file(self):
2430

2531
def create_received_file(self, received_path: str, test_source_file: str):
2632
code = Path(test_source_file).read_text()
33+
2734
received_text = (
2835
Path(received_path).read_text()[:-1] + self.semi_automatic_extra_line
2936
)

tests/test_inline_approvals.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,30 @@ def test_inline_with_additional_reporter():
188188
"hello\nworld",
189189
options=(Options().inline().add_reporter(ReportWithBeyondCompare())),
190190
)
191+
192+
def test_inline_with_preserved_approved_text():
193+
"""
194+
42
195+
***** DELETE ME TO APPROVE *****
196+
vvvvv PREVIOUS RESULT vvvvv
197+
41
198+
"""
199+
this_is_the_docstring_we_want = remove_indentation_from("""
200+
42
201+
***** DELETE ME TO APPROVE *****
202+
vvvvv PREVIOUS RESULT vvvvv
203+
41
204+
""")
205+
206+
207+
# 1. Run a failing inline approval with a new configuration
208+
# 2. Capture whatever we've overwriting the docstring with
209+
# 3. Capture the last docstring so we can write it to previous result
210+
# 4. Check that the overwriting string is equal to the docstring we want
211+
options = Options().inline(InlineOptions.previous_capture())
212+
try:
213+
verify("42", options=options)
214+
except ApprovalException:
215+
pass
216+
# assert that the docstring is equal to the string we want
217+
assert get_approved_via_doc_string() == this_is_the_docstring_we_want

0 commit comments

Comments
 (0)