Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/docformatter/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _do_get_list_option(self, option: str) -> Union[List[str], None]:
return [str(name) for name in names]
return value.split()

def do_parse_arguments(self) -> None:
def do_parse_arguments(self) -> None: # noqa: PLR0915
"""Parse configuration file and command line arguments."""
changes = self.parser.add_mutually_exclusive_group()
changes.add_argument(
Expand Down Expand Up @@ -190,8 +190,10 @@ def do_parse_arguments(self) -> None:
"(default: False)",
)

_cli_in_place = False
if self.args_lst is not None:
self.args = self.parser.parse_known_args(self.args_lst[1:])[0]
_cli_in_place = self.args.in_place
if not (self.args.in_place or self.args.check):
self.parser.set_defaults(
in_place=str(self.flargs.get("in-place", False)).lower() == "true",
Expand Down Expand Up @@ -351,6 +353,10 @@ def do_parse_arguments(self) -> None:

if self.args_lst is not None:
self.args = self.parser.parse_args(self.args_lst[1:])
# Standard in has no file to change, so drop a configured in-place
# mode. An explicit --in-place still errors.
if "-" in self.args.files and not _cli_in_place:
self.args.in_place = False

if self.args.line_range:
if self.args.line_range[0] <= 0:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_configuration_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
"""Module for testing docformatter's Configurater class."""

# Standard Library Imports
import io
import subprocess
import sys

# Third Party Imports
import pytest

# docformatter Package Imports
from docformatter import __main__ as main
from docformatter.configuration import Configurater

WRAP_72 = 72
Expand Down Expand Up @@ -904,6 +906,46 @@ def test_cli_mode_overrides_configured_mode(tmp_path, configured_mode, cli_mode)
assert config.args.check == (cli_mode == "--check")


@pytest.mark.unit
def test_configured_in_place_formats_standard_in(tmp_path, monkeypatch):
"""Standard in is formatted when only the config file sets in-place."""
(tmp_path / "setup.cfg").write_text(
"[docformatter]\nin-place = true\n", encoding="utf-8"
)
monkeypatch.chdir(tmp_path)
standard_out = io.StringIO()

result = main._main(
argv=["docformatter", "-"],
standard_out=standard_out,
standard_error=io.StringIO(),
standard_in=io.StringIO('def f():\n """ Hello. """\n'),
)

assert result is None
assert standard_out.getvalue() == 'def f():\n """Hello."""\n'


@pytest.mark.unit
def test_explicit_in_place_with_standard_in_is_an_error(tmp_path, monkeypatch, capsys):
"""An explicit --in-place with standard in is still an error."""
(tmp_path / "setup.cfg").write_text(
"[docformatter]\nin-place = true\n", encoding="utf-8"
)
monkeypatch.chdir(tmp_path)

with pytest.raises(SystemExit) as excinfo:
main._main(
argv=["docformatter", "--in-place", "-"],
standard_out=io.StringIO(),
standard_error=io.StringIO(),
standard_in=io.StringIO('def f():\n """ Hello. """\n'),
)

assert excinfo.value.code == 2
assert "--in-place cannot be used with standard input" in capsys.readouterr().err


class TestConfigArgument:
"""Class for testing the two --config spellings accepted by argparse."""

Expand Down
Loading