Skip to content

Commit b8a080d

Browse files
committed
Fixed inconsistency between format json and format policy function
JSON code was returning 1 (error) for case where file was reformatted. Also ended up refactoring a bit to make this as clear as possible. Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent 4f76e8b commit b8a080d

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/cfengine_cli/format.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@
2929

3030

3131
def format_json_file(filename: str, check: bool) -> int:
32-
"""Reformat a JSON file in place using cfbs pretty-printer."""
32+
"""Reformat a JSON file in place using cfbs pretty-printer.
33+
34+
Returns 0 in case of successful reformat or no reformat needed.
35+
Returns 1 when check is True and reformat is needed.
36+
"""
3337
assert filename.endswith(".json")
3438

3539
if check:
36-
r = not pretty_check_file(filename)
37-
if r:
40+
success = pretty_check_file(filename)
41+
# pretty_check_file() in cfbs needs correct typehint:
42+
assert type(success) is bool
43+
if not success:
3844
print(f"JSON file '{filename}' needs reformatting")
39-
return int(r)
45+
return int(not success)
4046

41-
r = pretty_file(filename)
42-
if r:
47+
reformatted = pretty_file(filename)
48+
if reformatted:
4349
print(f"JSON file '{filename}' was reformatted")
44-
return int(r)
50+
return 0 # Successfully reformatted or no reformat needed
4551

4652

4753
def text(node: Node) -> str:
@@ -655,7 +661,10 @@ def autoformat(
655661

656662

657663
def format_policy_file(filename: str, line_length: int, check: bool) -> int:
658-
"""Format a .cf policy file in place, writing only if content changed."""
664+
"""Format a .cf policy file in place, writing only if content changed.
665+
666+
Returns 0 in case of successful reformat or no reformat needed.
667+
Returns 1 when check is True and reformat is needed."""
659668
assert filename.endswith(".cf")
660669

661670
PY_LANGUAGE = Language(tscfengine.language())

0 commit comments

Comments
 (0)