mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 04:26:52 +00:00
Merge remote-tracking branch 'refs/remotes/origin/empty-object-with-newline' into empty-object-with-newline
This commit is contained in:
commit
65e5e39d2d
2 changed files with 18 additions and 13 deletions
|
|
@ -8,11 +8,14 @@ from collections.abc import Mapping
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from difflib import unified_diff
|
from difflib import unified_diff
|
||||||
|
|
||||||
|
|
||||||
def _insert_linebreaks(json_str: str) -> str:
|
def _insert_linebreaks(json_str: str) -> str:
|
||||||
return re.sub(
|
return re.sub(
|
||||||
r'\n(?P<spaces>\s*)(?P<json_key>.*): {}(?P<delim>,??)',
|
r'\n(?P<spaces>\s*)(?P<json_key>.*): {}(?P<delim>,??)',
|
||||||
'\n\g<spaces>\g<json_key>: {\n\g<spaces>}\g<delim>',
|
'\n\\g<spaces>\\g<json_key>: {\n\\g<spaces>}\\g<delim>',
|
||||||
json_str)
|
json_str,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _get_pretty_format(
|
def _get_pretty_format(
|
||||||
contents: str,
|
contents: str,
|
||||||
|
|
@ -110,8 +113,8 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='empty_object_with_newline',
|
dest='empty_object_with_newline',
|
||||||
default=False,
|
default=False,
|
||||||
help='Format empty JSON objects to have a linebreak, ' \
|
help='Format empty JSON objects to have a linebreak, ' +
|
||||||
+ 'also activates --no-sort-keys',
|
'also activates --no-sort-keys',
|
||||||
)
|
)
|
||||||
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)
|
||||||
|
|
@ -150,7 +153,7 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||||
diff_output = get_diff(
|
diff_output = get_diff(
|
||||||
contents,
|
contents,
|
||||||
pretty_contents,
|
pretty_contents,
|
||||||
json_file
|
json_file,
|
||||||
)
|
)
|
||||||
sys.stdout.buffer.write(diff_output.encode())
|
sys.stdout.buffer.write(diff_output.encode())
|
||||||
status = 1
|
status = 1
|
||||||
|
|
|
||||||
|
|
@ -165,7 +165,7 @@ def test_empty_object_with_newline(tmpdir):
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
||||||
# a template to be compared against.
|
# a template to be compared against.
|
||||||
multiline = get_resource_path("empty_object_json_multiline.json")
|
multiline = get_resource_path('empty_object_json_multiline.json')
|
||||||
|
|
||||||
# file has empty object with newline => expect fail with default settings
|
# file has empty object with newline => expect fail with default settings
|
||||||
ret = main([str(multiline)])
|
ret = main([str(multiline)])
|
||||||
|
|
@ -173,20 +173,22 @@ def test_empty_object_with_newline(tmpdir):
|
||||||
|
|
||||||
# launch the autofix with empty object with newline support on that file
|
# launch the autofix with empty object with newline support on that file
|
||||||
to_be_formatted_sameline = tmpdir.join(
|
to_be_formatted_sameline = tmpdir.join(
|
||||||
"not_pretty_formatted_empty_object_json_sameline.json"
|
'not_pretty_formatted_empty_object_json_sameline.json',
|
||||||
)
|
)
|
||||||
shutil.copyfile(str(sameline), str(to_be_formatted_sameline))
|
shutil.copyfile(str(sameline), str(to_be_formatted_sameline))
|
||||||
ret = main(
|
ret = main(
|
||||||
["--autofix",
|
[
|
||||||
"--empty-object-with-newline",
|
'--autofix',
|
||||||
str(to_be_formatted_sameline)]
|
'--empty-object-with-newline',
|
||||||
|
str(to_be_formatted_sameline),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
# it should have formatted it and don't raise an error code
|
# it should have formatted it and don't raise an error code
|
||||||
# to not stop the the commit
|
# to not stop the the commit
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
# file was formatted (shouldn't trigger linter with
|
# file was formatted (shouldn't trigger linter with
|
||||||
# --empty-object-with-newline switch)
|
# --empty-object-with-newline switch)
|
||||||
ret = main(["--empty-object-with-newline", str(to_be_formatted_sameline)])
|
ret = main(['--empty-object-with-newline', str(to_be_formatted_sameline)])
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
assert filecmp.cmp(to_be_formatted_sameline, multiline)
|
assert filecmp.cmp(to_be_formatted_sameline, multiline)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue