Skip to content

Commit

Permalink
Add: Introduce new dataclass for storing the autohooks settings
Browse files Browse the repository at this point in the history
The settigs contain the mode and the list of activated plugin modules.
The class allows to write the mode and plugins to a toml file like the
pyproject.toml.
  • Loading branch information
bjoernricks committed Aug 9, 2022
1 parent 045d9ac commit 534b9e4
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
37 changes: 37 additions & 0 deletions autohooks/settings.py
Expand Up @@ -15,7 +15,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Iterable

import tomlkit


class Mode(Enum):
Expand Down Expand Up @@ -51,3 +56,35 @@ def from_string(modestring: str) -> "Mode":

def __str__(self) -> str:
return self.name.lower() # pylint: disable=no-member


@dataclass
class AutohooksSettings:
mode: Mode = Mode.UNDEFINED
pre_commit: Iterable[str] = field(default_factory=list)

def write(self, filename: Path) -> None:
"""
Write the current AutohooksSettings to a TOML file
If the TOML file already exists only the [tool.autohooks] section is
overridden.
"""
if filename.exists():
toml_doc = tomlkit.loads(filename.read_text())
else:
toml_doc = tomlkit.document()

if "tool" not in toml_doc:
toml_doc["tool"] = tomlkit.table(is_super_table=True)
if "autohooks" not in toml_doc["tool"]:
toml_doc["tool"]["autohooks"] = tomlkit.table()

config_dict = {
"mode": str(self.mode.get_effective_mode()),
"pre-commit": self.pre_commit,
}

toml_doc["tool"]["autohooks"].update(config_dict)

filename.write_text(tomlkit.dumps(toml_doc), encoding="utf8")
84 changes: 83 additions & 1 deletion tests/test_settings.py
Expand Up @@ -15,9 +15,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=invalid-name

import unittest

from autohooks.settings import Mode
from autohooks.settings import AutohooksSettings, Mode
from tests import tempdir, testfile


class ModeTestCase(unittest.TestCase):
Expand Down Expand Up @@ -77,5 +80,84 @@ def test_string_conversion(self):
self.assertEqual(str(Mode.UNDEFINED), "undefined")


pyproject_toml_1 = """
[tool.autohooks]
pre-commit = ['foo', 'bar']
plugins.foo.bar = 'ipsum'
plugins.foo.lorem = 'dolor'
[tool.autohooks.plugins.bar]
foo = 'bar'
"""


class AutohooksSettingsTestCase(unittest.TestCase):
def test_create_empty(self):
settings = AutohooksSettings()
self.assertEqual(settings.mode, Mode.UNDEFINED)
self.assertEqual(settings.pre_commit, [])

def test_create(self):
settings = AutohooksSettings(mode=Mode.POETRY, pre_commit=["a", "b"])

self.assertEqual(settings.mode, Mode.POETRY)
self.assertEqual(settings.pre_commit, ["a", "b"])

def test_save_settings_new_empty(self):
settings = AutohooksSettings()
expected = """[tool.autohooks]
mode = "pythonpath"
pre-commit = []
"""
with tempdir() as tmp_path:
toml = tmp_path / "test.toml"
settings.write(toml)

self.assertEqual(settings.mode, Mode.UNDEFINED)
self.assertEqual(settings.pre_commit, [])

self.assertEqual(toml.read_text(encoding="utf8"), expected)

def test_save_empty_override(self):
settings = AutohooksSettings()
expected = """
[tool.autohooks]
pre-commit = []
mode = "pythonpath"
plugins.foo.bar = 'ipsum'
plugins.foo.lorem = 'dolor'
[tool.autohooks.plugins.bar]
foo = 'bar'
"""
with testfile(pyproject_toml_1) as toml:
settings.write(toml)

self.assertEqual(settings.mode, Mode.UNDEFINED)
self.assertEqual(settings.pre_commit, [])

self.assertEqual(toml.read_text(encoding="utf8"), expected)

def test_save_override(self):
settings = AutohooksSettings(mode=Mode.POETRY, pre_commit=["a", "b"])
expected = """
[tool.autohooks]
pre-commit = ["a", "b"]
mode = "poetry"
plugins.foo.bar = 'ipsum'
plugins.foo.lorem = 'dolor'
[tool.autohooks.plugins.bar]
foo = 'bar'
"""
with testfile(pyproject_toml_1) as toml:
settings.write(toml)

self.assertEqual(settings.mode, Mode.POETRY)
self.assertEqual(settings.pre_commit, ["a", "b"])

self.assertEqual(toml.read_text(encoding="utf8"), expected)


if __name__ == "__main__":
unittest.main()

0 comments on commit 534b9e4

Please sign in to comment.