Skip to content
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

Merged
merged 13 commits into from
Sep 24, 2019
Merged

Add logic to print diff between expected and actual JSON #408

merged 13 commits into from
Sep 24, 2019

Conversation

joepin
Copy link
Contributor

@joepin joepin commented Aug 15, 2019

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.

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
pre_commit_hooks/pretty_format_json.py Outdated Show resolved Hide resolved
pre_commit_hooks/pretty_format_json.py Outdated Show resolved Hide resolved
@joepin joepin changed the title Add logic to print line number of JSON errors Add logic to print diff between expected and actual JSON Aug 23, 2019
This commit uses capsys to test the output of the diff, which is now
hidden behind the autofix flag if it's disabled
@joepin
Copy link
Contributor Author

joepin commented Sep 13, 2019

@asottile thanks for all the help! Please let me know if my most recent change looks good to you 🙂

@asottile
Copy link
Member

let's print the filename stuff to stderr (and flush)

@joepin
Copy link
Contributor Author

joepin commented Sep 13, 2019

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)

@asottile
Copy link
Member

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

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close!!!!

pre_commit_hooks/pretty_format_json.py Show resolved Hide resolved
tests/pretty_format_json_test.py Outdated Show resolved Hide resolved
tests/pretty_format_json_test.py Outdated Show resolved Hide resolved
Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource_path = get_resource_path('not_pretty_formatted_json.json')
expected_retval = 1
expected_out = '''\
--- \n+++ \n@@ -1,6 +1,9 @@
Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Member

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)

Copy link
Contributor Author

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')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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),
Copy link
Member

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?

Copy link
Contributor Author

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?

+ "blah": null,
+ "foo": "bar"
}}

Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries - updated!

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asottile asottile merged commit 277f875 into pre-commit:master Sep 24, 2019
@joepin joepin deleted the pretty-json-print-lines branch September 24, 2019 21:01
@joepin
Copy link
Contributor Author

joepin commented Sep 24, 2019

Awesome! Thanks for all your help and guidance on this one! 😄

@asottile
Copy link
Member

this has been released in v2.4.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants