Skip to content

Commit

Permalink
feat: add file config options (#137)
Browse files Browse the repository at this point in the history
* feat: now exit non-zero if files are changed in-place

* feat: allow setting check, diff, in-place in config files

* test: add tests for new config file options

* feat: allow setting exclude list in config files

* test: add tests for exclude config file option

* docs: update usage documentation
  • Loading branch information
weibullguy authored Dec 17, 2022
1 parent b033bc9 commit f7d50ae
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
docformatter
============

.. |CI| image:: https://img.shields.io/github/actions/workflow/status/PyCQA/docformatter/workflows/ci.yml?branch=main
.. |CI| image:: https://img.shields.io/github/actions/workflow/status/PyCQA/docformatter/ci.yml?branch=master
:target: https://github.com/PyCQA/docformatter/actions/workflows/ci.yml
.. |COVERALLS| image:: https://img.shields.io/coveralls/github/PyCQA/docformatter
:target: https://coveralls.io/github/PyCQA/docformatter
Expand Down
4 changes: 2 additions & 2 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ help output provides a summary of these options:
-i, --in-place make changes to files instead of printing diffs
-c, --check only check and report incorrectly formatted files
-r, --recursive drill down directories recursively
-e, --exclude exclude directories and files by names
-e, --exclude in recursive mode, exclude directories and files by names
--wrap-summaries length
wrap long summary lines at this length; set
Expand Down Expand Up @@ -93,7 +93,7 @@ Possible exit codes from ``docformatter``:

- **1** - if any error encountered
- **2** - if it was interrupted
- **3** - if any file needs to be formatted (in ``--check`` mode)
- **3** - if any file needs to be formatted (in ``--check`` or ``--in-place`` mode)

Use as a PyCharm File Watcher
-----------------------------
Expand Down
6 changes: 5 additions & 1 deletion src/docformatter/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,21 @@ def do_parse_arguments(self) -> None:
"-i",
"--in-place",
action="store_true",
default=self.flargs_dct.get("in-place", "false").lower() == "true",
help="make changes to files instead of printing diffs",
)
changes.add_argument(
"-c",
"--check",
action="store_true",
default=self.flargs_dct.get("check", "false").lower() == "true",
help="only check and report incorrectly formatted files",
)
self.parser.add_argument(
"-d",
"--diff",
action="store_true",
default=self.flargs_dct.get("diff", "false").lower() == "true",
help="when used with `--check` or `--in-place`, also what changes "
"would be made",
)
Expand All @@ -121,7 +124,8 @@ def do_parse_arguments(self) -> None:
"-e",
"--exclude",
nargs="*",
help="exclude directories and files by names",
default=self.flargs_dct.get("exclude", None),
help="in recursive mode, exclude directories and files by names",
)
self.parser.add_argument(
"--wrap-summaries",
Expand Down
2 changes: 1 addition & 1 deletion src/docformatter/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ def _do_format_file(self, filename):
show_diff = self.args.diff

if source != formatted_source:
ret = FormatResult.check_failed
if self.args.check:
print(unicode(filename), file=self.stderror)
ret = FormatResult.check_failed
elif self.args.in_place:
with self.encodor.do_open_with_encoding(
filename,
Expand Down
16 changes: 13 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,20 @@ def temporary_file(contents, file_directory=".", file_prefix=""):
os.remove(f.name)

@pytest.fixture(scope="function")
def temporary_config(config, file_directory="/tmp",
file_name="pyproject.toml"):
def temporary_pyproject_toml(config, config_file_directory="/tmp",):
"""Write contents to temporary configuration and yield it."""
f = open(f"{file_directory}/{file_name}", "wb")
f = open(f"{config_file_directory}/pyproject.toml", "wb")
try:
f.write(config.encode())
f.close()
yield f.name
finally:
os.remove(f.name)

@pytest.fixture(scope="function")
def temporary_setup_cfg(config, config_file_directory="/tmp",):
"""Write contents to temporary configuration and yield it."""
f = open(f"{config_file_directory}/setup.cfg", "wb")
try:
f.write(config.encode())
f.close()
Expand Down
80 changes: 80 additions & 0 deletions tests/test_configuration_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,83 @@ def test_low_length_range_greater_than_high_length_range(self, capsys):
"First value of --docstring-length should be less than or equal "
"to the second" in err
)

@pytest.mark.unit
@pytest.mark.parametrize(
"config",
[
"""\
[tool.docformatter]
check = true
diff = true
recursive = true
exclude = ["src/", "tests/"]
"""
],
)
def test_exclude_from_pyproject_toml(self,temporary_pyproject_toml,config,):
"""Read exclude list from pyproject.toml.
See issue #120.
"""
argb = [
"/path/to/docformatter",
"-c",
"--config",
"/tmp/pyproject.toml",
"",
]

uut = Configurater(argb)
uut.do_parse_arguments()

assert uut.args.check
assert not uut.args.in_place
assert uut.args_lst == argb
assert uut.config_file == "/tmp/pyproject.toml"
assert uut.flargs_dct == {
"recursive": "True",
"check": "True",
"diff": "True",
"exclude": ["src/", "tests/"]
}

@pytest.mark.unit
@pytest.mark.parametrize(
"config",
[
"""\
[docformatter]
check = true
diff = true
recursive = true
exclude = ["src/", "tests/"]
"""
],
)
def test_exclude_from_setup_cfg(self,temporary_setup_cfg,config,):
"""Read exclude list from setup.cfg.
See issue #120.
"""
argb = [
"/path/to/docformatter",
"-c",
"--config",
"/tmp/setup.cfg",
"",
]

uut = Configurater(argb)
uut.do_parse_arguments()

assert uut.args.check
assert not uut.args.in_place
assert uut.args_lst == argb
assert uut.config_file == "/tmp/setup.cfg"
assert uut.flargs_dct == {
"recursive": "true",
"check": "true",
"diff": "true",
"exclude": '["src/", "tests/"]'
}
Loading

0 comments on commit f7d50ae

Please sign in to comment.