Skip to content

Commit

Permalink
replace toml package with tomlkit (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikOwen authored Oct 20, 2021
1 parent 4f06f89 commit fbc7460
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
8 changes: 3 additions & 5 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import os
import pathlib
import sys

import toml
import tomlkit

sys.path.insert(0, os.path.abspath("."))

Expand All @@ -35,9 +34,8 @@
copyright_year = "%s-%s" % (copyright_year, now.year)
copyright = "%s, GoDaddy.com, LLC" % copyright_year

version = toml.load(str(DOCS_PATH.parent / "pyproject.toml"))["tool"]["poetry"][
"version"
]
with open(str(DOCS_PATH.parent / "pyproject.toml"), encoding="utf-8") as filename:
version = tomlkit.loads(filename.read())["tool"]["poetry"]["version"] # type: ignore
release = version

# -- General configuration ---------------------------------------------------
Expand Down
56 changes: 42 additions & 14 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sphinx = {version = "^3.2", optional = true}
sphinx-click = {version = "^2.5.0", optional = true}
sphinx-rtd-theme = {version = "^0.5.0", optional = true}
sphinxcontrib-spelling = {version = "^5.4.0", optional = true}
toml = "^0.10"
tomlkit = "^0.7.2"

[tool.poetry.dev-dependencies]
black = {version = "21.5b2", allow-prereleases = true, markers = "platform_python_implementation == 'CPython'"}
Expand All @@ -61,7 +61,6 @@ pytest-sugar = "^0.9.4"
tox = "^3.21.4"
vulture = "^2.3"
types-requests = "^2.25.2"
types-toml = "^0.1.3"
types-click = "^7.1.2"

[tool.poetry.extras]
Expand Down
9 changes: 5 additions & 4 deletions tartufo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)

import click
import toml
import tomlkit

from tartufo import types, util
from tartufo.types import ConfigException, Rule
Expand Down Expand Up @@ -85,10 +85,11 @@ def load_config_from_path(
full_path = config_path / possibility
if full_path.exists():
try:
toml_file = toml.load(full_path)
config = toml_file.get("tool", {}).get("tartufo", {})
with open(full_path, encoding="utf8") as file:
toml_file = tomlkit.loads(file.read())
config = toml_file.get("tool", {}).get("tartufo", {}) # type: ignore
break
except (toml.TomlDecodeError, OSError) as exc:
except (tomlkit.exceptions.ParseError, OSError) as exc:
raise types.ConfigException(f"Error reading configuration file: {exc}")
if not config and traverse and config_path.parent != config_path:
return load_config_from_path(config_path.parent, filename, traverse)
Expand Down
11 changes: 6 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from unittest import mock

import click
import toml
import tomlkit
from click.testing import CliRunner

from tartufo import config, types
Expand Down Expand Up @@ -185,13 +185,14 @@ def test_specified_file_gets_read(self):
)
self.assertEqual(config_path, self.data_dir / "config" / "other_config.toml")

@mock.patch("toml.load")
@mock.patch("tomlkit.loads")
def test_config_exception_is_raised_if_trouble_reading_file(
self, mock_toml: mock.MagicMock
):
mock_toml.side_effect = toml.TomlDecodeError("Bad TOML!", "foo", 42)
mock_toml.side_effect = tomlkit.exceptions.ParseError(21, 42)
with self.assertRaisesRegex(
types.ConfigException, "Error reading configuration file: Bad TOML!"
types.ConfigException,
"TOML parse error at line 21 col 42",
):
config.load_config_from_path(self.data_dir)

Expand All @@ -206,7 +207,7 @@ def test_parent_directory_not_checked_if_traverse_is_false(self):
self.data_dir / "config", "pyproject.toml", False
)

@mock.patch("toml.load")
@mock.patch("tomlkit.loads")
def test_config_keys_are_normalized(self, mock_load: mock.MagicMock):
mock_load.return_value = {"tool": {"tartufo": {"--repo-path": "."}}}
(_, data) = config.load_config_from_path(self.data_dir)
Expand Down

0 comments on commit fbc7460

Please sign in to comment.