Skip to content

Commit

Permalink
Load sorter choice from project config
Browse files Browse the repository at this point in the history
ghstack-source-id: 03940aab1ad9b6cb6e0ca062f9b32604303faa68
Pull Request resolved: #193
  • Loading branch information
amyreese committed Feb 22, 2024
1 parent 00e2200 commit 0af70c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ufmt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tomlkit
from trailrunner import project_root

from .types import Formatter, UfmtConfig
from .types import Formatter, Sorter, UfmtConfig

LOG = logging.getLogger(__name__)

Expand All @@ -35,6 +35,7 @@ def load_config(path: Optional[Path] = None, root: Optional[Path] = None) -> Ufm
raise ValueError(f"{config_path}: excludes must be a list of strings")

formatter = Formatter(config.pop("formatter", UfmtConfig.formatter))
sorter = Sorter(config.pop("sorter", Sorter.usort))

if config:
LOG.warning("%s: unknown values ignored: %r", config_path, sorted(config))
Expand All @@ -44,6 +45,7 @@ def load_config(path: Optional[Path] = None, root: Optional[Path] = None) -> Ufm
pyproject_path=config_path,
excludes=excludes,
formatter=formatter,
sorter=sorter,
)

return UfmtConfig()
12 changes: 12 additions & 0 deletions ufmt/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ def test_invalid_config(self, log_mock):
):
load_config(self.td / "fake.py")

with self.subTest("unsupported sorter"):
self.pyproject.write_text(
dedent(
"""
[tool.ufmt]
sorter = "garbage"
"""
)
)
with self.assertRaisesRegex(ValueError, "'garbage' is not a valid Sorter"):
load_config(self.td / "fake.py")

@patch("ufmt.config.LOG")
def test_config_excludes(self, log_mock):
with self.subTest("missing"):
Expand Down

0 comments on commit 0af70c3

Please sign in to comment.