-
-
Notifications
You must be signed in to change notification settings - Fork 679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logic to print diff between expected and actual JSON #408
Conversation
This commit makes the pretty JSON check more verbose when it encounters errors, that way developers can see which lines are causing errors in order to debug.
This prints a diff between the given json file and the expected (pretty) output, with this functionality hidden behind a cli flag
This commit uses capsys to test the output of the diff, which is now hidden behind the autofix flag if it's disabled
@asottile thanks for all the help! Please let me know if my most recent change looks good to you 🙂 |
let's print the filename stuff to stderr (and flush) |
I'm not sure what you mean by that - do you mean to say this should be printed to stderr? (As it stands now, the main func prints both the file name thing and the diff output to stdout) |
yes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
close!!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/pretty_format_json_test.py
Outdated
resource_path = get_resource_path('not_pretty_formatted_json.json') | ||
expected_retval = 1 | ||
expected_out = '''\ | ||
--- \n+++ \n@@ -1,6 +1,9 @@ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the line I was talking about. unified_diff
prints:
---
+++
@@ -1,6 +1,9 @@
where the first two lines have a trailing space. This was my temporary solution to pass the test cases and satisfy the pre commit check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohh this is where the filename is supposed to be
maybe we should pass in a/filename
and b/filename
so that it prints better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where filename
is the actual filename -- so os.path.join('a', filename)
and os.path.join('b', filename)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh that makes sense - maybe we can just put in some pseudo-names? That way it's a little clearer what the output is saying
So it would be
unified_diff(source, target, fromfile='not_pretty.json', tofile='pretty.json')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha - adding it!
else: | ||
print( | ||
get_diff( | ||
''.join(contents), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should just be contents
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I remember needing to join it before
I guess not now that we're using splitlines
?
tests/pretty_format_json_test.py
Outdated
+ "blah": null, | ||
+ "foo": "bar" | ||
}} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh sorry for one more
this extra newline indicates that it should be print(..., end='')
for the diff (the diff itself includes the ending newline already)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries - updated!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thanks for all your help and guidance on this one! 😄 |
this has been released in v2.4.0! |
This PR makes the pretty JSON check more verbose when it encounters errors, that way developers can see which lines are causing errors in order to debug.
I haven't seen any talk about adding this anywhere, but would love to hear the maintainers' thoughts on this.