Skip to content

Commit

Permalink
- B inline reporter use reporter from options
Browse files Browse the repository at this point in the history
Co-Authored-By: Nitsan Avni <[email protected]>
Co-Authored-By: 4dsherwood <[email protected]>
Co-Authored-By: jmasonlee <[email protected]>
Co-Authored-By: blade290 <[email protected]>
  • Loading branch information
5 people committed Nov 26, 2023
1 parent 92ecf7c commit e788ea2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion approvaltests/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ def namer(self) -> Namer:
def inline(self, show_code: bool = True) -> "Options":
from approvaltests.namer.inline_comparator import InlineComparator

return InlineComparator().register(self, show_code=show_code)
return InlineComparator().register(self, show_code)
2 changes: 1 addition & 1 deletion approvaltests/namer/inline_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def get_caller_method(self, caller_frame) -> Callable:
def register(self, options: "Options", show_code: bool):
options2 = options.with_namer(self)
if show_code:
options2 = options2.with_reporter(InlinePythonReporter())
options2 = options2.with_reporter(InlinePythonReporter(options.reporter))
return options2
14 changes: 6 additions & 8 deletions approvaltests/namer/inline_python_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
from inspect import FrameInfo
from pathlib import Path

from approvaltests import Reporter, DiffReporter, StackFrameNamer
from approvaltests import Reporter, StackFrameNamer
from approvaltests.inline.split_code import SplitCode


class InlinePythonReporter(Reporter):
def __init__(self):
def __init__(self, reporter):
self.test_source_file = self.get_test_source_file()
self.diffReporter = DiffReporter()
self.diffReporter = reporter

def report(self, received_path: str, approved_path: str) -> bool:
# create a temp file of the based on the source,
received_path = self.create_received_file(received_path)
self.diffReporter.report(received_path, self.test_source_file)

Expand All @@ -21,13 +20,12 @@ def get_test_source_file(self):
return test_stack_frame.filename

def create_received_file(self, received_path: str):
file = tempfile.NamedTemporaryFile(suffix=".received.txt", delete=False).name
code = Path(self.test_source_file).read_text()
received_text = Path(received_path).read_text()[:-1]
method_name = StackFrameNamer.get_test_frame().function
code = self.swap(received_text, code, method_name)

Path(file).write_text(code)
new_code = self.swap(received_text, code, method_name)
file = tempfile.NamedTemporaryFile(suffix=".received.txt", delete=False).name
Path(file).write_text(new_code)
return file

def swap(self, 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 @@ -11,7 +11,7 @@
Reporter,
verify,
)
from approvaltests.reporters import MultiReporter
from approvaltests.reporters import MultiReporter, ReporterThatAutomaticallyApproves


def get_approved_via_doc_string():
Expand Down

0 comments on commit e788ea2

Please sign in to comment.