Remove extra cli flag, and update test case
This commit uses capsys to test the output of the diff, which is now
hidden behind the autofix flag if it's disabled
diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py
index 2e04064..3626332 100644
--- a/pre_commit_hooks/pretty_format_json.py
+++ b/pre_commit_hooks/pretty_format_json.py
@@ -56,12 +56,12 @@
return s.split(',')
-def get_diff(source, target): # type: (List[str], List[str]) -> str
- source_lines = ''.join(source).split('\n')
- target_lines = ''.join(target).split('\n')
- d = difflib.Differ()
- diff = d.compare(source_lines, target_lines)
- return '\n'.join(diff)
+def get_diff(source, target): # type: (str, str) -> str
+ source_lines = source.splitlines(True)
+ target_lines = target.splitlines(True)
+ diff = ''.join(difflib.ndiff(source_lines, target_lines))
+ print(diff)
+ return diff
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
@@ -105,14 +105,6 @@
default=[],
help='Ordered list of keys to keep at the top of JSON hashes',
)
- parser.add_argument(
- '--show-expected',
- action='store_true',
- dest='show_expected',
- default=False,
- help='Show a diff between the input file and expected (pretty) output',
- )
-
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
args = parser.parse_args(argv)
@@ -131,11 +123,10 @@
if contents != pretty_contents:
print('File {} is not pretty-formatted'.format(json_file))
- if args.show_expected:
- print(get_diff(contents, list(pretty_contents)))
-
if args.autofix:
_autofix(json_file, pretty_contents)
+ else:
+ print(get_diff(''.join(contents), pretty_contents))
status = 1
except ValueError: