From 61b72ab2da57188d8cc764f5bd8500965eb4a5f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Aug 2023 18:07:38 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pre_commit_hooks/pretty_format_json.py | 10 +++++----- tests/pretty_format_json_test.py | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index d9956fe..c547ad7 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -2,16 +2,17 @@ from __future__ import annotations import argparse import json -import sys import re - +import sys from difflib import unified_diff from typing import Mapping from typing import Sequence + def _insert_linebreaks(json_str) -> str: # (?P\s*) seems to capture the \n. Hence, there is no need for it in the substitution string - return re.sub(r'\n(?P\s*)(?P.*): {}(?P,??)', '\n\g\g: {\n\g}\g', json_str) + return re.sub(r'\n(?P\s*)(?P.*): {}(?P,??)', '\n\\g\\g: {\n\\g}\\g', json_str) + def _get_pretty_format( contents: str, @@ -126,7 +127,7 @@ def main(argv: Sequence[str] | None = None) -> int: pretty_contents = _get_pretty_format( contents, args.indent, ensure_ascii=not args.no_ensure_ascii, sort_keys=not args.no_sort_keys, top_keys=args.top_keys, - empty_object_with_newline=args.empty_object_with_newline + empty_object_with_newline=args.empty_object_with_newline, ) except ValueError: print( @@ -145,7 +146,6 @@ def main(argv: Sequence[str] | None = None) -> int: diff_output = get_diff(contents, pretty_contents, json_file) sys.stdout.buffer.write(diff_output.encode()) - return status diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 31f3955..a1ff100 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -1,10 +1,10 @@ from __future__ import annotations +import filecmp import os import shutil import pytest -import filecmp from pre_commit_hooks.pretty_format_json import main from pre_commit_hooks.pretty_format_json import parse_num_to_int @@ -142,13 +142,13 @@ def test_diffing_output(capsys): def test_empty_object_with_newline(tmpdir): # same line objects shoud trigger with --empty-object-with-newline switch - sameline = get_resource_path("empty_object_json_sameline.json") - ret = main(["--empty-object-with-newline", str(sameline)]) + sameline = get_resource_path('empty_object_json_sameline.json') + ret = main(['--empty-object-with-newline', str(sameline)]) assert ret == 1 - multiline = get_resource_path("empty_object_json_multiline.json") + multiline = get_resource_path('empty_object_json_multiline.json') 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)) @@ -158,12 +158,12 @@ def test_empty_object_with_newline(tmpdir): # now launch the autofix with empty object with newline support on that file ret = main( - ["--autofix", "--empty-object-with-newline", str(to_be_formatted_sameline)] + ['--autofix', '--empty-object-with-newline', str(to_be_formatted_sameline)], ) # it should have formatted it and don't raise an error code assert ret == 0 # file was formatted (shouldn't trigger linter with --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 filecmp.cmp(to_be_formatted_sameline, multiline)