mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 04:34:16 +00:00
Merge pull request #109 from mattclegg/no-sort-keys
Adding no-sort-keys to pretty_format_json
This commit is contained in:
commit
ea867c2e68
6 changed files with 41 additions and 9 deletions
|
|
@ -1,12 +1,12 @@
|
||||||
language: python
|
language: python
|
||||||
env: # These should match the tox env list
|
env: # These should match the tox env list
|
||||||
- TOXENV=py26
|
|
||||||
- TOXENV=py27
|
- TOXENV=py27
|
||||||
- TOXENV=py33
|
- TOXENV=py33
|
||||||
- TOXENV=py34
|
- TOXENV=py34
|
||||||
- TOXENV=pypy
|
- TOXENV=pypy
|
||||||
- TOXENV=pypy3
|
- TOXENV=pypy3
|
||||||
install: pip install coveralls tox
|
install:
|
||||||
|
- pip install coveralls tox
|
||||||
script: tox
|
script: tox
|
||||||
# Special snowflake. Our tests depend on making real commits.
|
# Special snowflake. Our tests depend on making real commits.
|
||||||
before_install:
|
before_install:
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@ Add this to your `.pre-commit-config.yaml`
|
||||||
- Use `args: ['--django']` to match `test*.py` instead.
|
- Use `args: ['--django']` to match `test*.py` instead.
|
||||||
- `pyflakes` - Run pyflakes on your python files.
|
- `pyflakes` - Run pyflakes on your python files.
|
||||||
- `pretty-format-json` - Checks that all your JSON files are pretty
|
- `pretty-format-json` - Checks that all your JSON files are pretty
|
||||||
|
- Use `args: ['--autofix']` to automatically fixing the encountered not-pretty-formatted files and
|
||||||
|
`args: ['--no-sort-keys']` to disable the sort on the keys.
|
||||||
- `requirements-txt-fixer` - Sorts entries in requirements.txt
|
- `requirements-txt-fixer` - Sorts entries in requirements.txt
|
||||||
- `trailing-whitespace` - Trims trailing whitespace.
|
- `trailing-whitespace` - Trims trailing whitespace.
|
||||||
- Markdown linebreak trailing spaces preserved for `.md` and`.markdown`;
|
- Markdown linebreak trailing spaces preserved for `.md` and`.markdown`;
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,16 @@ from __future__ import print_function
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
import simplejson
|
import simplejson
|
||||||
|
|
||||||
|
|
||||||
def _get_pretty_format(contents, indent):
|
def _get_pretty_format(contents, indent, sort_keys=True):
|
||||||
return simplejson.dumps(
|
return simplejson.dumps(
|
||||||
simplejson.loads(contents),
|
simplejson.loads(contents,
|
||||||
sort_keys=True,
|
object_pairs_hook=None if sort_keys else OrderedDict),
|
||||||
|
sort_keys=sort_keys,
|
||||||
indent=indent
|
indent=indent
|
||||||
) + "\n" # dumps don't end with a newline
|
) + "\n" # dumps don't end with a newline
|
||||||
|
|
||||||
|
|
@ -34,6 +36,13 @@ def pretty_format_json(argv=None):
|
||||||
default=2,
|
default=2,
|
||||||
help='Number of indent spaces used to pretty-format files'
|
help='Number of indent spaces used to pretty-format files'
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--no-sort-keys',
|
||||||
|
action='store_true',
|
||||||
|
dest='no_sort_keys',
|
||||||
|
default=False,
|
||||||
|
help='Keep JSON nodes in the same order'
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -46,7 +55,7 @@ def pretty_format_json(argv=None):
|
||||||
contents = f.read()
|
contents = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
pretty_contents = _get_pretty_format(contents, args.indent)
|
pretty_contents = _get_pretty_format(contents, args.indent, (not args.no_sort_keys))
|
||||||
|
|
||||||
if contents != pretty_contents:
|
if contents != pretty_contents:
|
||||||
print("File {0} is not pretty-formatted".format(json_file))
|
print("File {0} is not pretty-formatted".format(json_file))
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"foo": "bar",
|
"foo":
|
||||||
"alist": [2, 34, 234],
|
"bar",
|
||||||
"blah": null
|
"alist": [2, 34, 234],
|
||||||
|
"blah": null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
testing/resources/unsorted_pretty_formatted_json.json
Normal file
9
testing/resources/unsorted_pretty_formatted_json.json
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"foo": "bar",
|
||||||
|
"alist": [
|
||||||
|
34,
|
||||||
|
2,
|
||||||
|
234
|
||||||
|
],
|
||||||
|
"blah": null
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ from testing.util import get_resource_path
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'expected_retval'), (
|
@pytest.mark.parametrize(('filename', 'expected_retval'), (
|
||||||
('not_pretty_formatted_json.json', 1),
|
('not_pretty_formatted_json.json', 1),
|
||||||
|
('unsorted_pretty_formatted_json.json', 1),
|
||||||
('pretty_formatted_json.json', 0),
|
('pretty_formatted_json.json', 0),
|
||||||
))
|
))
|
||||||
def test_pretty_format_json(filename, expected_retval):
|
def test_pretty_format_json(filename, expected_retval):
|
||||||
|
|
@ -15,6 +16,16 @@ def test_pretty_format_json(filename, expected_retval):
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(('filename', 'expected_retval'), (
|
||||||
|
('not_pretty_formatted_json.json', 1),
|
||||||
|
('unsorted_pretty_formatted_json.json', 0),
|
||||||
|
('pretty_formatted_json.json', 0),
|
||||||
|
))
|
||||||
|
def test_unsorted_pretty_format_json(filename, expected_retval):
|
||||||
|
ret = pretty_format_json(['--no-sort-keys', get_resource_path(filename)])
|
||||||
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
||||||
def test_autofix_pretty_format_json(tmpdir):
|
def test_autofix_pretty_format_json(tmpdir):
|
||||||
srcfile = tmpdir.join('to_be_json_formatted.json')
|
srcfile = tmpdir.join('to_be_json_formatted.json')
|
||||||
with io.open(get_resource_path('not_pretty_formatted_json.json')) as f:
|
with io.open(get_resource_path('not_pretty_formatted_json.json')) as f:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue