mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 20:26:54 +00:00
Merge pull request #120 from sanmai-NL/JSON_arbitrary_indentation_separator
Refactor legacy `indent: int` -> `indent: str`
This commit is contained in:
commit
cf3aabe9b1
3 changed files with 53 additions and 3 deletions
|
|
@ -24,6 +24,25 @@ def _autofix(filename, new_contents):
|
||||||
f.write(new_contents)
|
f.write(new_contents)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_indent(s):
|
||||||
|
# type: (str) -> str
|
||||||
|
try:
|
||||||
|
int_indentation_spec = int(s)
|
||||||
|
if int_indentation_spec >= 0:
|
||||||
|
return int_indentation_spec * ' '
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
'Negative integer supplied to construct JSON indentation delimiter. ',
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
if s.strip() == '':
|
||||||
|
return s
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
'Non-whitespace JSON indentation delimiter supplied. ',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def pretty_format_json(argv=None):
|
def pretty_format_json(argv=None):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -34,9 +53,9 @@ def pretty_format_json(argv=None):
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--indent',
|
'--indent',
|
||||||
type=int,
|
type=parse_indent,
|
||||||
default=2,
|
default=' ',
|
||||||
help='Number of indent spaces used to pretty-format files',
|
help='String used as delimiter for one indentation level',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--no-sort-keys',
|
'--no-sort-keys',
|
||||||
|
|
|
||||||
9
testing/resources/tab_pretty_formatted_json.json
Normal file
9
testing/resources/tab_pretty_formatted_json.json
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"alist": [
|
||||||
|
2,
|
||||||
|
34,
|
||||||
|
234
|
||||||
|
],
|
||||||
|
"blah": null,
|
||||||
|
"foo": "bar"
|
||||||
|
}
|
||||||
|
|
@ -2,10 +2,21 @@ import shutil
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from pre_commit_hooks.pretty_format_json import parse_indent
|
||||||
from pre_commit_hooks.pretty_format_json import pretty_format_json
|
from pre_commit_hooks.pretty_format_json import pretty_format_json
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_indent():
|
||||||
|
assert parse_indent('0') == ''
|
||||||
|
assert parse_indent('2') == ' '
|
||||||
|
assert parse_indent('\t') == '\t'
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
parse_indent('a')
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
parse_indent('-2')
|
||||||
|
|
||||||
|
|
||||||
@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),
|
('unsorted_pretty_formatted_json.json', 1),
|
||||||
|
|
@ -26,6 +37,17 @@ def test_unsorted_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', 1),
|
||||||
|
('pretty_formatted_json.json', 1),
|
||||||
|
('tab_pretty_formatted_json.json', 0),
|
||||||
|
))
|
||||||
|
def test_tab_pretty_format_json(filename, expected_retval):
|
||||||
|
ret = pretty_format_json(['--indent', '\t', 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')
|
||||||
shutil.copyfile(
|
shutil.copyfile(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue