From 5d785c948fd749776cbe714573996c323ff22db5 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 17 May 2022 14:34:44 -0400 Subject: [PATCH] Normalize arguments loaded from pyproject.toml (#75) (#85) Co-authored-by: Sorin Sbarnea --- src/doc8/main.py | 9 +++++++- src/doc8/tests/test_main.py | 42 ++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/doc8/main.py b/src/doc8/main.py index 6d8c3c7..b355362 100644 --- a/src/doc8/main.py +++ b/src/doc8/main.py @@ -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 diff --git a/src/doc8/tests/test_main.py b/src/doc8/tests/test_main.py index 1766f57..f52322c 100644 --- a/src/doc8/tests/test_main.py +++ b/src/doc8/tests/test_main.py @@ -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 @@ -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)