Skip to content

Commit

Permalink
Normalize arguments loaded from pyproject.toml (#75) (#85)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
ejd and ssbarnea authored May 17, 2022
1 parent 4e40150 commit 5d785c9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/doc8/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@ def from_ini(fp):

def from_toml(fp):
with open(fp, "rb") as f:
cfg = tomli.load(f).get("tool", {}).get("doc8", {})
parsed = tomli.load(f).get("tool", {}).get("doc8", {})

cfg = {}
for key, value in parsed.items():
if key == "ignore-path-errors":
value = parse_ignore_path_errors(value)
cfg[key.replace("-", "_")] = value

return cfg


Expand Down
42 changes: 41 additions & 1 deletion src/doc8/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import shutil
import sys

from doc8.main import main, doc8
from doc8.main import main, doc8, from_toml


# Location to create test files
Expand Down Expand Up @@ -426,3 +426,43 @@ def test_args__version__overrides_default(self):
state = main()
self.assertEqual(state, 0)
mock_scan.assert_not_called()


CONFIG_TOML = """\
[tool.doc8]
allow-long-titles = true
ignore-path-errors = ["foo.rst;D001;D002", "bar.rst;D002"]
default-extension = ".rst"
extension = [".rst", ".rST", ".txt", ".TXT"]
ignore-path = ["baz.rst", "boff.rst"]
ignore = ["D002", "D005"]
max-line-length = 80
file-encoding = "utf8"
sphinx = false"""


class TestConfig(unittest.TestCase):
"""
Test that configuration file is loaded correctly
"""

def test_config__from_toml(self):
with TmpFs() as tmpfs:
tmpfs.create_file("pyproject.toml", CONFIG_TOML)
cfg = from_toml(os.path.join(tmpfs.path, "pyproject.toml"))

self.assertEqual(cfg["allow_long_titles"], True)
self.assertEqual(
cfg["ignore_path_errors"],
{
"foo.rst": {"D001", "D002"},
"bar.rst": {"D002"},
},
)
self.assertEqual(cfg["default_extension"], ".rst")
self.assertEqual(cfg["extension"], [".rst", ".rST", ".txt", ".TXT"])
self.assertEqual(cfg["ignore_path"], ["baz.rst", "boff.rst"])
self.assertEqual(cfg["ignore"], ["D002", "D005"])
self.assertEqual(cfg["max_line_length"], 80)
self.assertEqual(cfg["file_encoding"], "utf8")
self.assertEqual(cfg["sphinx"], False)

0 comments on commit 5d785c9

Please sign in to comment.