Skip to content

Commit

Permalink
Merge pull request #160 from maresb/toml-options
Browse files Browse the repository at this point in the history
Add --indent and --trailing-commas arguments for toml
  • Loading branch information
macisamuele authored May 22, 2023
2 parents 8ced619 + 3883278 commit 5f56679
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
25 changes: 21 additions & 4 deletions language_formatters_pre_commit_hooks/pretty_format_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,25 @@ def pretty_format_toml(argv: typing.Optional[typing.List[str]] = None) -> int:
dest="autofix",
help="Automatically fixes encountered not-pretty-formatted files",
)

parser.add_argument(
"--indent",
type=int,
default="2",
help="The number of spaces to be used as delimiter for indentation level (Default: %(default)s)",
)
parser.add_argument("filenames", nargs="*", help="Filenames to fix")
parser.add_argument(
"--trailing-commas",
action="store_true",
dest="trailing_commas",
help="Add trailing commas to inline arrays",
)
parser.add_argument(
"--no-sort",
action="store_true",
dest="no_sort",
help="Don't sort keys",
)
args = parser.parse_args(argv)

status = 0
Expand All @@ -39,11 +56,11 @@ def pretty_format_toml(argv: typing.Optional[typing.List[str]] = None) -> int:
inline=True,
block=True,
),
sort_config=SortConfiguration(tables=True),
sort_config=SortConfiguration(tables=not args.no_sort, table_keys=not args.no_sort),
format_config=FormattingConfiguration(
spaces_before_inline_comment=2,
spaces_indent_inline_array=2,
trailing_comma_inline_array=False,
spaces_indent_inline_array=args.indent,
trailing_comma_inline_array=args.trailing_commas,
),
).sorted()

Expand Down
4 changes: 4 additions & 0 deletions test-data/pretty_format_toml/indent2-pretty-formatted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies = [
"numpy",
"scikit-learn"
]
4 changes: 4 additions & 0 deletions test-data/pretty_format_toml/indent4-pretty-formatted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies = [
"numpy",
"scikit-learn"
]
4 changes: 4 additions & 0 deletions test-data/pretty_format_toml/no-sort-pretty-formatted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[project]
name = "example"
version = "1.0.0"
description = "Example"
4 changes: 4 additions & 0 deletions test-data/pretty_format_toml/sorted-pretty-formatted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[project]
description = "Example"
name = "example"
version = "1.0.0"
15 changes: 15 additions & 0 deletions tests/pretty_format_toml_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ def test_pretty_format_toml(filename, expected_retval):
assert pretty_format_toml([filename]) == expected_retval


@pytest.mark.parametrize(
("filename", "args", "expected_retval"),
(
("indent2-pretty-formatted.toml", [], 0),
("indent2-pretty-formatted.toml", ["--indent=4"], 1),
("indent4-pretty-formatted.toml", [], 1),
("indent4-pretty-formatted.toml", ["--indent=4"], 0),
("no-sort-pretty-formatted.toml", ["--no-sort"], 0),
("no-sort-pretty-formatted.toml", [], 1),
),
)
def test_pretty_format_toml_custom_cli_arguments(filename, args, expected_retval):
assert pretty_format_toml([filename] + args) == expected_retval


def test_pretty_format_toml_autofix(tmpdir):
run_autofix_test(
tmpdir,
Expand Down

0 comments on commit 5f56679

Please sign in to comment.