Skip to content

Commit

Permalink
- F BREAKING CHANGE Options.inline() takes InlineOptions
Browse files Browse the repository at this point in the history
Co-Authored-By: Nitsan Avni <nitsanav@gmail.com>
Co-Authored-By: 4dsherwood <4dsherwood@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 28, 2024
1 parent bcba3ba commit 6f9dc65
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 31 deletions.
9 changes: 8 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ option 1: we can use the automatic approver
not there is a bug in the automatic approver
option 2: Llewellyns idea

make semi automatic
original plan modify auto reporter
instead we will make this work
then Come back and impement this in an API
and that will depend on what the implementation looks like
Add an extra line for this specific case

POssible FEATURE
Create an API to add a REPORTER.
Create an API to add a REPORTER.[test_inline_approvals.py](tests%2Ftest_inline_approvals.p[ok.approved.txt](ok.approved.txt)y)
now we have a combined reporter and then ?? only the last one works?

Inline Approvals Bugs
Expand Down
6 changes: 4 additions & 2 deletions approvaltests/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from approvaltests.core.comparator import Comparator
from approvaltests.file_approver import FileComparator
from approvaltests.core.namer import Namer
from approvaltests.inline.inline_options import InlineOptions
from approvaltests.scrubbers import combine_scrubbers


Expand Down Expand Up @@ -82,7 +83,8 @@ def namer(self) -> Namer:
namer.set_extension(self.for_file.file_extention)
return namer

def inline(self, show_code: bool = True) -> "Options":
def inline(self, inline_options: InlineOptions = None) -> "Options":
from approvaltests.namer.inline_comparator import InlineComparator
print(f'inline_options = {inline_options.__class__.__name__}')
return InlineComparator().register(self, inline_options)

return InlineComparator().register(self, show_code)
44 changes: 44 additions & 0 deletions approvaltests/inline/inline_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


class InlineOptions:

@staticmethod
def automatic():
from approvaltests.namer.inline_python_reporter import InlinePythonReporter
from approvaltests.reporters import ReporterThatAutomaticallyApproves

class AutomaticInlineOptions(InlineOptions):
def apply(self, options: "Options") -> "Options":
return options.with_reporter(InlinePythonReporter(ReporterThatAutomaticallyApproves()))
return AutomaticInlineOptions()

@staticmethod
def semi_automatic():
from approvaltests.namer.inline_python_reporter import InlinePythonReporter
from approvaltests.reporters import ReporterThatAutomaticallyApproves

class SemiAutomaticInlineOptions(InlineOptions):
def apply(self, options: "Options") -> "Options":
return options.with_reporter(InlinePythonReporter(ReporterThatAutomaticallyApproves(), add_approval_line=True))
return SemiAutomaticInlineOptions()

@staticmethod
def applesauce():
return InlineOptions()
def apply(self, options: "Options") -> "Options":

return options

@staticmethod
def show_code(do_show_code: bool = True):
from approvaltests.namer.inline_python_reporter import InlinePythonReporter

class ShowCodeInlineOptions(InlineOptions):
def apply(self, options: "Options") -> "Options":
return options.with_reporter(InlinePythonReporter(options.reporter))
class DoNotShowCodeInlineOptions(InlineOptions):
def apply(self, options: "Options") -> "Options":
return options

return ShowCodeInlineOptions() if do_show_code else DoNotShowCodeInlineOptions()

11 changes: 8 additions & 3 deletions approvaltests/namer/inline_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from approval_utilities.utilities.multiline_string_utils import remove_indentation_from
from approval_utilities.utilities.stack_frame_utilities import get_class_name_for_frame
from approvaltests import Namer, StackFrameNamer
from approvaltests.inline.inline_options import InlineOptions
from approvaltests.namer.inline_python_reporter import InlinePythonReporter
from approvaltests.reporters import ReporterThatAutomaticallyApproves


class InlineComparator(Namer):
Expand Down Expand Up @@ -41,8 +43,11 @@ def get_caller_method(caller_frame) -> Callable:
caller_function_object = clazz.__dict__.get(caller_function_name)
return caller_function_object

def register(self, options: "Options", show_code: bool):
#return InlineComparator().register(self, inline_options)
def register(self, options: "Options", inline_options: InlineOptions = None):
inline_options = InlineOptions() if inline_options is None else inline_options
options2 = options.with_namer(self)
if show_code:
options2 = options2.with_reporter(InlinePythonReporter(options.reporter))
print(f'inline_options = {inline_options.__class__.__name__}')
options2 = inline_options.apply(options2)

return options2
5 changes: 3 additions & 2 deletions approvaltests/namer/inline_python_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@


class InlinePythonReporter(Reporter):
def __init__(self, reporter):
def __init__(self, reporter, add_approval_line=False):
self.diffReporter = reporter
self.semi_automatic_extra_line = "\n***** DELETE ME TO APPROVE *****" if add_approval_line else ""

def report(self, received_path: str, approved_path: str) -> bool:
test_source_file = self.get_test_source_file()
Expand All @@ -21,7 +22,7 @@ def get_test_source_file(self):

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]
received_text = Path(received_path).read_text()[:-1] + self.semi_automatic_extra_line
method_name = StackFrameNamer.get_test_frame().function
new_code = self.swap(received_text, code, method_name)
file = tempfile.NamedTemporaryFile(suffix=".received.txt", delete=False).name
Expand Down
70 changes: 47 additions & 23 deletions tests/test_inline_approvals.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import unittest
from inspect import FrameInfo
from pathlib import Path
from typing import Callable, Any

import pytest

from approval_utilities.utilities.clipboard_utilities import copy_to_clipboard
from approval_utilities.utilities.multiline_string_utils import remove_indentation_from
from approvaltests import (
StackFrameNamer,
assert_equal_with_reporter,
Options,
Reporter,
verify,
verify_all,
verify_all_combinations_with_labeled_input,
ApprovalException,
)
from approvaltests.inline.inline_options import InlineOptions
from approvaltests.inline.parse_docstring import parse_docstring
from approvaltests.reporters import MultiReporter, ReportWithBeyondCompare
from approvaltests.reporters.report_quietly import ReportQuietly


Expand Down Expand Up @@ -76,28 +72,16 @@ def test_docstrings():
"""
# verify_inline(greetting())
# verify(greetting(), options=Options().inline(show_code= False))
verify(greeting(), options=Options().inline(show_code=True))
verify(greeting(), options=Options().inline())


def greeting():
return "hello\nworld"


class InlineReporter(Reporter):
# TODO: Start here - Make this report create a temp file of the fixed source,
# and compare it with the existing source.
# if there are mulitple failures, they each get there on reporter
def report(self, received_path: str, approved_path: str) -> bool:
received = Path(received_path).read_text()
copy_to_clipboard(f"'''\n{received}\n'''")


def verify_inline(actual):
options = Options()
options = options.with_reporter(MultiReporter(options.reporter, InlineReporter()))
if actual[-1] != "\n":
actual += "\n"
assert_equal_with_reporter(get_approved_via_doc_string(), actual, options=options)



def test_docstring_parsing():
Expand Down Expand Up @@ -157,7 +141,7 @@ def test_with_labeled_input_inline(self) -> None:
lambda a, b: a + b,
arg1=(1, 3),
arg2=(2, 4),
options=Options().inline(show_code=False),
options=Options().inline(),
)


Expand All @@ -170,7 +154,7 @@ def test_preceding_whitespace():
"""
4 whitespaces
"""
verify(get_preceding_whitespace(), options=Options().inline(show_code=True))
verify(get_preceding_whitespace(), options=Options().inline())


def test_trailing_whitespace():
Expand All @@ -179,7 +163,7 @@ def test_trailing_whitespace():
"""
# Note: Pycharm will remove the trailing whitespaces, to disable this go to:
# File -> Settings -> Editor -> General -> On Save -> [ ] Remove trailing spaces
verify("4 trailing whitespaces ", options=Options().inline(show_code=False))
verify("4 trailing whitespaces ", options=Options().inline())

# fmt: on

Expand All @@ -195,5 +179,45 @@ def test_bug_blank_lines():
"""
verify(
"\n\ntest bug with blank lines\n\n\n\n",
options=Options().inline(show_code=True),
options=Options().inline()
)





def test_semi_automatic_inline_reporter():
"""
1
2
"""
#verify("1\n2\n5", options=Options().inline(InlineOptions.semi_automatic()))
#verify("1\n2\n5", options=Options().inline(InlineOptions.automatic()))
# verify("1\n2\n5", options=Options().inline(InlineOptions.applesauce())) # when it is false
#verify("1\n2\n5", options=Options().inline(InlineOptions.show_code()))
verify("1\n2\n5", options=Options().inline(InlineOptions.show_code()))

# show_code
# keep show_code becase we only want to change one thing at time

# reports contents only
# report contents without showing code
# compare full source code
# report on
# inline only on test resutls
#report docstring only only report the docstring
# report results only
# report results and docstring
#use diffcompare in traditional way
# use diffcompare in inline way
#only report the results
# report results only
# report results without surrounding code
# report results no surrounding code
# results only
# no surrounding code
# diffcompare in inline way
# ---
# showcode = false
#

0 comments on commit 6f9dc65

Please sign in to comment.