Skip to content

Commit

Permalink
Fix Container bug for AoT edge case, add test file
Browse files Browse the repository at this point in the history
  • Loading branch information
pappasam committed Aug 14, 2019
1 parent c7c2c84 commit 77a8f57
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ A command line utility to sort and format your toml files. Requires Python 3.6 o
## Installation

```bash
# With pip
pip install toml-sort

# With poetry
poetry add toml-sort
```

## Motivation
Expand Down Expand Up @@ -46,6 +50,60 @@ Read from stdin, write to file on disk

cat input.toml | toml-sort -o output.toml

## Example

The following example shows the input, and output, from the CLI with default options.

### Unformatted, unsorted input

```toml
# My great TOML example

title = "The example"

[[a-section.hello]]
ports = [ 8001, 8001, 8002 ]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?



[b-section]
date = "2018"
name = "Richard Stallman"

[[a-section.hello]]
ports = [ 80 ]
dob = 1920-05-27T07:32:00Z # Another date!

[a-section]
date = "2019"
name = "Samuel Roeca"
```

### Formatted, sorted output

```toml
# My great TOML example

title = "The example"

[a-section]
date = "2019"
name = "Samuel Roeca"

[[a-section.hello]]
ports = [ 8001, 8001, 8002 ]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

[[a-section.hello]]
ports = [ 80 ]
dob = 1920-05-27T07:32:00Z # Another date!

[b-section]
date = "2018"
name = "Richard Stallman"
```

## Local Development

Local development for this project is quite simple.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ line-length = 79

[tool.poetry]
name = "toml-sort"
version = "0.13.0"
version = "0.14.0"
description = "Toml sorting library"
authors = ["Sam Roeca <[email protected]>"]
readme = "README.md"
Expand Down
19 changes: 19 additions & 0 deletions tests/examples/defaults/weird.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# My great TOML example

title = "The example"

[a-section]
date = "2019"
name = "Samuel Roeca"

[[a-section.hello]]
ports = [ 8001, 8001, 8002 ]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

[[a-section.hello]]
ports = [ 80 ]
dob = 1920-05-27T07:32:00Z # Another date!

[b-section]
date = "2018"
name = "Richard Stallman"
21 changes: 21 additions & 0 deletions tests/examples/weird.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# My great TOML example

title = "The example"

[[a-section.hello]]
ports = [ 8001, 8001, 8002 ]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?



[b-section]
date = "2018"
name = "Richard Stallman"

[[a-section.hello]]
ports = [ 80 ]
dob = 1920-05-27T07:32:00Z # Another date!

[a-section]
date = "2019"
name = "Samuel Roeca"
5 changes: 4 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

@pytest.mark.parametrize(
"path_unsorted,path_sorted",
[("from-toml-lang.toml", "defaults/from-toml-lang.toml")],
[
("from-toml-lang.toml", "defaults/from-toml-lang.toml"),
("weird.toml", "defaults/weird.toml"),
],
)
def test_cli_defaults(path_unsorted: str, path_sorted: str) -> None:
"""Test the basic cli behavior with default arguments
Expand Down
13 changes: 9 additions & 4 deletions toml_sort/tomlsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import re
import itertools
from typing import Iterable, Tuple, Set
from typing import Iterable, Tuple, Set, Union

import tomlkit
from tomlkit.api import aot, ws, item
from tomlkit.api import aot, ws, item, table
from tomlkit.toml_document import TOMLDocument
from tomlkit.container import Container
from tomlkit.items import Item, Table, AoT, Comment, Whitespace
Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(

def sorted_children_table(
self, parent: Table
) -> Iterable[Tuple[str, Item]]:
) -> Iterable[Tuple[str, Union[Item, Container]]]:
"""Get the sorted children of a table
NOTE: non-tables are wrapped in an item to ensure that they are, in
Expand All @@ -69,7 +69,7 @@ def sorted_children_table(
tables = (
(key, parent[key])
for key in parent
if isinstance(parent[key], (Table, AoT))
if isinstance(parent[key], (Table, AoT, Container))
)
non_tables = (
(key, item(parent[key], parent))
Expand All @@ -87,6 +87,11 @@ def sorted_children_table(

def toml_elements_sorted(self, original: Item) -> Item:
"""Returns a sorted item, recursing collections to their base"""
if isinstance(original, Container):
new_table = table()
for key, value in self.sorted_children_table(original):
new_table[key] = self.toml_elements_sorted(value)
return new_table
if isinstance(original, Table):
original.trivia.indent = "\n"
new_table = Table(
Expand Down

0 comments on commit 77a8f57

Please sign in to comment.