Skip to content

Commit

Permalink
fix: changed TOML lists are replaced (same behaviour as in YAML files) (
Browse files Browse the repository at this point in the history
  • Loading branch information
danygielow committed May 29, 2023
1 parent 3df4038 commit dbd9ac7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/nitpick/plugins/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from __future__ import annotations

from itertools import chain
from typing import TYPE_CHECKING, Iterator
from typing import TYPE_CHECKING, Iterator, cast

from tomlkit import dumps, parse

from nitpick.blender import BaseDoc, Comparison, TomlDoc, traverse_toml_tree
from nitpick.blender import Comparison, TomlDoc, traverse_toml_tree
from nitpick.plugins import hookimpl
from nitpick.plugins.base import NitpickPlugin
from nitpick.violations import Fuss, SharedViolations, ViolationEnum
Expand Down Expand Up @@ -42,20 +42,34 @@ def enforce_rules(self) -> Iterator[Fuss]:

document = parse(toml_doc.as_string) if self.autofix else None
yield from chain(
self.report(SharedViolations.DIFFERENT_VALUES, document, comparison.diff),
self.report(SharedViolations.MISSING_VALUES, document, comparison.missing),
self.report(SharedViolations.DIFFERENT_VALUES, document, cast(TomlDoc, comparison.diff)),
self.report(
SharedViolations.MISSING_VALUES,
document,
cast(TomlDoc, comparison.missing),
cast(TomlDoc, comparison.replace),
),
)
if self.autofix and self.dirty:
self.file_path.write_text(dumps(document))

def report(self, violation: ViolationEnum, document: TOMLDocument | None, change: BaseDoc | None):
def report(
self,
violation: ViolationEnum,
document: TOMLDocument | None,
change: TomlDoc | None,
replacement: TomlDoc | None = None,
):
"""Report a violation while optionally modifying the TOML document."""
if not change:
if not (change or replacement):
return
if document:
traverse_toml_tree(document, change.as_object)
if self.autofix:
real_change = cast(TomlDoc, replacement or change)
traverse_toml_tree(document, real_change.as_object)
self.dirty = True
yield self.reporter.make_fuss(violation, change.reformatted.strip(), prefix="", fixed=self.autofix)

to_display = cast(TomlDoc, change or replacement)
yield self.reporter.make_fuss(violation, to_display.reformatted.strip(), prefix="", fixed=self.autofix)

@property
def initial_contents(self) -> str:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ def test_suggest_initial_contents(tmp_path):
[section]
key = "value"
number = 10
list = [ "a", "b", "c",]
"""
ProjectMock(tmp_path).style(
f"""
["{filename}".section]
key = "value"
number = 10
list = ["a", "b", "c"]
"""
).api_check_then_fix(
Fuss(
Expand All @@ -58,13 +60,19 @@ def test_missing_different_values_pyproject_toml(tmp_path):
["pyproject.toml".tool]
missing = "value"
["pyproject.toml".config]
list = ["a", "b", "c"]
"""
).pyproject_toml(
"""
[something]
x = 1 # comment for x
yada = "before" # comment for yada yada
abc = "123" # comment for abc
[config]
list = ["a", "b"] # comment for list
"""
).api_check_then_fix(
Fuss(
Expand All @@ -85,6 +93,9 @@ def test_missing_different_values_pyproject_toml(tmp_path):
"""
[tool]
missing = "value"
[config]
list = [ "c",]
""",
),
).assert_file_contents(
Expand All @@ -95,6 +106,9 @@ def test_missing_different_values_pyproject_toml(tmp_path):
yada = "after" # comment for yada yada
abc = "123" # comment for abc
[config]
list = ["a", "b", "c"] # comment for list
[tool]
missing = "value"
""",
Expand All @@ -116,6 +130,7 @@ def test_missing_different_values_any_toml(tmp_path):
["{filename}".section]
key = "new value"
number = 5
list = ["a", "b", "c"]
"""
).api_check_then_fix(
Fuss(
Expand All @@ -136,6 +151,7 @@ def test_missing_different_values_any_toml(tmp_path):
"""
[section]
number = 5
list = [ "a", "b", "c",]
""",
),
).assert_file_contents(
Expand All @@ -145,5 +161,6 @@ def test_missing_different_values_any_toml(tmp_path):
# Line comment
key = "new value"
number = 5
list = ["a", "b", "c"]
""",
)

0 comments on commit dbd9ac7

Please sign in to comment.