Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow showing the diff when using --in-place or --check with --diff #128

Merged
merged 1 commit into from Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/docformatter/configuration.py
Expand Up @@ -102,6 +102,13 @@ def do_parse_arguments(self) -> None:
action="store_true",
help="only check and report incorrectly formatted files",
)
self.parser.add_argument(
weibullguy marked this conversation as resolved.
Show resolved Hide resolved
"-d",
"--diff",
action="store_true",
help="when used with `--check` or `--in-place`, also what changes "
"would be made",
)
self.parser.add_argument(
"-r",
"--recursive",
Expand Down
13 changes: 9 additions & 4 deletions src/docformatter/format.py
Expand Up @@ -155,8 +155,6 @@ def do_format_files(self):
try:
result = self._do_format_file(filename)
outcomes[result] += 1
if result == FormatResult.check_failed:
print(unicode(filename), file=self.stderror)
except IOError as exception:
outcomes[FormatResult.error] += 1
print(unicode(exception), file=self.stderror)
Expand Down Expand Up @@ -190,16 +188,23 @@ def _do_format_file(self, filename):
source = input_file.read()
formatted_source = self._do_format_code(source)

ret = FormatResult.ok
show_diff = self.args.diff

if source != formatted_source:
if self.args.check:
return FormatResult.check_failed
print(unicode(filename), file=self.stderror)
ret = FormatResult.check_failed
elif self.args.in_place:
with self.encodor.do_open_with_encoding(
filename,
mode="w",
) as output_file:
output_file.write(formatted_source)
else:
show_diff = True

if show_diff:
# Standard Library Imports
import difflib

Expand All @@ -212,7 +217,7 @@ def _do_format_file(self, filename):
)
self.stdout.write("\n".join(list(diff) + [""]))

return FormatResult.ok
return ret

def _do_format_code(self, source):
"""Return source code with docstrings formatted.
Expand Down
47 changes: 40 additions & 7 deletions tests/test_docformatter.py
Expand Up @@ -97,11 +97,18 @@ def foo():
'''
],
)
def test_in_place(self, temporary_file, contents):
@pytest.mark.parametrize(
"diff", [True, False], ids=["show-diff", "no-diff"]
)
def test_in_place(self, temporary_file, contents, diff):
"""Should make changes and save back to file."""
output_file = io.StringIO()
args = ["my_fake_program", "--in-place", temporary_file]
if diff:
args.append("--diff")

main._main(
argv=["my_fake_program", "--in-place", temporary_file],
argv=args,
standard_out=output_file,
standard_error=None,
standard_in=None,
Expand All @@ -115,6 +122,11 @@ def foo():
== f.read()
)

if diff:
assert "def foo" in output_file.getvalue()
else:
assert "def foo" not in output_file

@pytest.mark.system
@pytest.mark.parametrize(
"contents",
Expand Down Expand Up @@ -167,12 +179,21 @@ def test_io_error_exit_code(self):
"contents",
["""Totally fine docstring, do not report anything."""],
)
def test_check_mode_correct_docstring(self, temporary_file, contents):
@pytest.mark.parametrize(
"diff", [True, False], ids=["show-diff", "no-diff"]
)
def test_check_mode_correct_docstring(
self, temporary_file, contents, diff
):
""""""
stdout = io.StringIO()
stderr = io.StringIO()
args = ["my_fake_program", "--check", temporary_file]
if diff:
args.append("--diff")

ret_code = main._main(
argv=["my_fake_program", "--check", temporary_file],
argv=args,
standard_out=stdout,
standard_error=stderr,
standard_in=None,
Expand All @@ -193,18 +214,30 @@ def test_check_mode_correct_docstring(self, temporary_file, contents):
'''
],
)
def test_check_mode_incorrect_docstring(self, temporary_file, contents):
@pytest.mark.parametrize(
"diff", [True, False], ids=["show-diff", "no-diff"]
)
def test_check_mode_incorrect_docstring(
self, temporary_file, contents, diff
):
""""""
stdout = io.StringIO()
stderr = io.StringIO()
args = ["my_fake_program", "--check", temporary_file]
if diff:
args.append("--diff")

ret_code = main._main(
argv=["my_fake_program", "--check", temporary_file],
argv=args,
standard_out=stdout,
standard_error=stderr,
standard_in=None,
)
assert ret_code == 3 # FormatResult.check_failed
assert stdout.getvalue() == ""
if diff:
assert "Print my path" in stdout.getvalue()
else:
assert stdout.getvalue() == ""
assert stderr.getvalue().strip() == temporary_file


Expand Down