Skip to content

Commit

Permalink
Merge pull request #23 from larsewi/pretty-check
Browse files Browse the repository at this point in the history
Implemented `cfbs pretty --check`
  • Loading branch information
olehermanse authored Oct 7, 2021
2 parents d067b2a + a5b3ce6 commit 7ebfafd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cfbs/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

from cfbs.main import main

main()
rc = main()
sys.exit(rc)
16 changes: 13 additions & 3 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
sh,
)

from cfbs.pretty import pretty_file, pretty
from cfbs.pretty import pretty_check_file, pretty_file, pretty
from cfbs.index import Index


Expand All @@ -42,18 +42,28 @@ def put_definition(data: dict):
f.write(pretty(data))


def pretty_command(filenames: list) -> int:
def pretty_command(filenames: list, check) -> int:
if not filenames:
user_error("Filenames missing for cfbs pretty command")

num_files = 0
for f in filenames:
if not f or not f.endswith(".json"):
user_error(
f"cfbs pretty command can only be used with .json files, not '{os.path.basename(f)}'"
)
try:
pretty_file(f)
if check:
if not pretty_check_file(f):
num_files += 1
print("Would reformat %s" % f)
else:
pretty_file(f)
except FileNotFoundError:
user_error(f"File '{f}' not found")
if check:
print("Would reformat %d file(s)" % num_files)
return 1 if num_files > 0 else 0
return 0


Expand Down
5 changes: 4 additions & 1 deletion cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def get_args():
"--force", help="Force rebuild / redownload", action="store_true"
)
parser.add_argument("--index", help="Specify alternate index", type=str)
parser.add_argument(
"--check", help="Check if file(s) would be reformatted", action="store_true"
)

args = parser.parse_args()
return args
Expand Down Expand Up @@ -74,7 +77,7 @@ def main() -> int:
if args.command == "search":
return commands.search_command(args.args, index=args.index)
if args.command == "pretty":
return commands.pretty_command(args.args)
return commands.pretty_command(args.args, args.check)

if not is_cfbs_repo():
user_error("This is not a cfbs repo, to get started, type: cfbs init")
Expand Down
12 changes: 12 additions & 0 deletions cfbs/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
from collections import OrderedDict


def pretty_check_file(filename):
with open(filename) as f:
s = f.read()
o = json.loads(s, object_pairs_hook=OrderedDict)
return s == pretty(o) + "\n"


def pretty_check_string(s):
o = json.loads(s, object_pairs_hook=OrderedDict)
return s == pretty(o)


def pretty_file(filename):
with open(filename) as f:
data = f.read()
Expand Down
17 changes: 16 additions & 1 deletion test/test_pretty.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import OrderedDict
from cfbs.pretty import pretty, pretty_string
from cfbs.pretty import pretty, pretty_check_string, pretty_string


def test_pretty():
Expand Down Expand Up @@ -181,3 +181,18 @@ def test_pretty_string():
}
]"""
assert pretty_string(test) == expected


def test_pretty_check_string():
assert pretty_check_string(' "Hello" ') == False
assert pretty_check_string('"Hello"') == True
assert (
pretty_check_string(
"""{
"name": "lars",
"age": 27
}"""
)
== False
)
assert pretty_check_string('{ "name": "lars", "age": 27 }') == True

0 comments on commit 7ebfafd

Please sign in to comment.