Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cfbs/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,13 @@ def pretty_check_string(s, sorting_rules=None):

def pretty_file(filename, sorting_rules=None):
with open(filename) as f:
data = pretty_string(f.read(), sorting_rules)
with open(filename, "w") as f:
f.write(data)
f.write("\n")
old_data = f.read()
new_data = pretty_string(old_data, sorting_rules) + "\n"
if old_data != new_data:
with open(filename, "w") as f:
f.write(new_data)
return True
return False


def pretty_string(s, sorting_rules=None):
Expand Down
13 changes: 11 additions & 2 deletions tests/test_pretty.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import OrderedDict
from cfbs.pretty import pretty, pretty_check_string, pretty_string
from cfbs.utils import item_index
from cfbs.pretty import pretty, pretty_check_string, pretty_string, pretty_file
from cfbs.utils import item_index, mkdir, read_file


def test_pretty():
Expand Down Expand Up @@ -205,6 +205,15 @@ def test_pretty_string():
assert pretty_string(test) == expected


def test_pretty_file():
mkdir("tests/tmp/", exist_ok=True)
with open("tests/tmp/test_pretty_file.json", "w") as f:
f.write(" {} \n")
assert pretty_file("tests/tmp/test_pretty_file.json") == True
assert read_file("tests/tmp/test_pretty_file.json") == "{}\n"
assert pretty_file("tests/tmp/test_pretty_file.json") == False


def test_pretty_check_string():
assert pretty_check_string(' "Hello" ') == False
assert pretty_check_string('"Hello"') == True
Expand Down