2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-09-25 03:42:24 +08:00
|
|
|
import os
|
2016-05-28 05:09:50 +08:00
|
|
|
import shutil
|
2015-06-11 05:08:48 +08:00
|
|
|
|
2015-12-26 01:37:18 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
from pre_commit_hooks.pretty_format_json import main
|
2017-12-10 17:34:36 +08:00
|
|
|
from pre_commit_hooks.pretty_format_json import parse_num_to_int
|
2015-06-11 05:08:48 +08:00
|
|
|
from testing.util import get_resource_path
|
|
|
|
|
|
|
|
|
2017-12-10 17:34:36 +08:00
|
|
|
def test_parse_num_to_int():
|
|
|
|
assert parse_num_to_int('0') == 0
|
|
|
|
assert parse_num_to_int('2') == 2
|
|
|
|
assert parse_num_to_int('\t') == '\t'
|
|
|
|
assert parse_num_to_int(' ') == ' '
|
2016-06-11 02:16:00 +08:00
|
|
|
|
2017-12-10 16:57:34 +08:00
|
|
|
|
2017-07-16 03:56:51 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('filename', 'expected_retval'), (
|
|
|
|
('not_pretty_formatted_json.json', 1),
|
|
|
|
('unsorted_pretty_formatted_json.json', 1),
|
|
|
|
('non_ascii_pretty_formatted_json.json', 1),
|
|
|
|
('pretty_formatted_json.json', 0),
|
|
|
|
),
|
|
|
|
)
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_main(filename, expected_retval):
|
|
|
|
ret = main([get_resource_path(filename)])
|
2015-06-11 05:08:48 +08:00
|
|
|
assert ret == expected_retval
|
|
|
|
|
|
|
|
|
2017-07-16 03:56:51 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('filename', 'expected_retval'), (
|
|
|
|
('not_pretty_formatted_json.json', 1),
|
|
|
|
('unsorted_pretty_formatted_json.json', 0),
|
|
|
|
('non_ascii_pretty_formatted_json.json', 1),
|
|
|
|
('pretty_formatted_json.json', 0),
|
|
|
|
),
|
|
|
|
)
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_unsorted_main(filename, expected_retval):
|
|
|
|
ret = main(['--no-sort-keys', get_resource_path(filename)])
|
2016-04-14 16:36:47 +08:00
|
|
|
assert ret == expected_retval
|
|
|
|
|
|
|
|
|
2017-07-16 03:56:51 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('filename', 'expected_retval'), (
|
|
|
|
('not_pretty_formatted_json.json', 1),
|
|
|
|
('unsorted_pretty_formatted_json.json', 1),
|
|
|
|
('non_ascii_pretty_formatted_json.json', 1),
|
|
|
|
('pretty_formatted_json.json', 1),
|
|
|
|
('tab_pretty_formatted_json.json', 0),
|
|
|
|
),
|
|
|
|
)
|
2020-02-06 03:10:42 +08:00
|
|
|
def test_tab_main(filename, expected_retval):
|
2019-02-01 11:19:10 +08:00
|
|
|
ret = main(['--indent', '\t', get_resource_path(filename)])
|
2016-06-11 02:16:00 +08:00
|
|
|
assert ret == expected_retval
|
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_non_ascii_main():
|
2019-02-12 11:56:15 +08:00
|
|
|
ret = main((
|
|
|
|
'--no-ensure-ascii',
|
|
|
|
get_resource_path('non_ascii_pretty_formatted_json.json'),
|
|
|
|
))
|
2017-03-16 14:54:55 +08:00
|
|
|
assert ret == 0
|
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_autofix_main(tmpdir):
|
2015-12-27 02:58:33 +08:00
|
|
|
srcfile = tmpdir.join('to_be_json_formatted.json')
|
2016-05-28 05:09:50 +08:00
|
|
|
shutil.copyfile(
|
|
|
|
get_resource_path('not_pretty_formatted_json.json'),
|
2020-05-21 00:07:45 +08:00
|
|
|
str(srcfile),
|
2016-05-28 05:09:50 +08:00
|
|
|
)
|
2015-06-11 05:08:48 +08:00
|
|
|
|
|
|
|
# now launch the autofix on that file
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main(['--autofix', str(srcfile)])
|
2015-06-11 05:08:48 +08:00
|
|
|
# it should have formatted it
|
|
|
|
assert ret == 1
|
|
|
|
|
2015-12-27 02:58:33 +08:00
|
|
|
# file was formatted (shouldn't trigger linter again)
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main([str(srcfile)])
|
2015-06-11 05:08:48 +08:00
|
|
|
assert ret == 0
|
|
|
|
|
2016-11-04 06:54:48 +08:00
|
|
|
|
2024-04-13 04:22:10 +08:00
|
|
|
def test_invalid_main(tmpdir):
|
|
|
|
srcfile1 = tmpdir.join('not_valid_json.json')
|
|
|
|
srcfile1.write(
|
|
|
|
'{\n'
|
|
|
|
' // not json\n'
|
|
|
|
' "a": "b"\n'
|
|
|
|
'}',
|
|
|
|
)
|
|
|
|
srcfile2 = tmpdir.join('to_be_json_formatted.json')
|
|
|
|
srcfile2.write('{ "a": "b" }')
|
|
|
|
|
|
|
|
# it should have skipped the first file and formatted the second one
|
|
|
|
assert main(['--autofix', str(srcfile1), str(srcfile2)]) == 1
|
|
|
|
|
|
|
|
# confirm second file was formatted (shouldn't trigger linter again)
|
|
|
|
assert main([str(srcfile2)]) == 0
|
|
|
|
|
|
|
|
|
2016-11-04 00:41:23 +08:00
|
|
|
def test_orderfile_get_pretty_format():
|
2019-02-12 11:56:15 +08:00
|
|
|
ret = main((
|
|
|
|
'--top-keys=alist', get_resource_path('pretty_formatted_json.json'),
|
|
|
|
))
|
2016-11-04 00:41:23 +08:00
|
|
|
assert ret == 0
|
|
|
|
|
2016-11-04 06:54:48 +08:00
|
|
|
|
2016-11-04 06:51:24 +08:00
|
|
|
def test_not_orderfile_get_pretty_format():
|
2019-02-12 11:56:15 +08:00
|
|
|
ret = main((
|
|
|
|
'--top-keys=blah', get_resource_path('pretty_formatted_json.json'),
|
|
|
|
))
|
2016-11-04 00:41:23 +08:00
|
|
|
assert ret == 1
|
2015-06-11 05:08:48 +08:00
|
|
|
|
2016-11-04 06:54:48 +08:00
|
|
|
|
2016-11-04 09:05:43 +08:00
|
|
|
def test_top_sorted_get_pretty_format():
|
2019-02-12 11:56:15 +08:00
|
|
|
ret = main((
|
|
|
|
'--top-keys=01-alist,alist', get_resource_path('top_sorted_json.json'),
|
|
|
|
))
|
2016-11-04 09:05:43 +08:00
|
|
|
assert ret == 0
|
|
|
|
|
|
|
|
|
2019-02-01 11:19:10 +08:00
|
|
|
def test_badfile_main():
|
|
|
|
ret = main([get_resource_path('ok_yaml.yaml')])
|
2015-06-11 05:08:48 +08:00
|
|
|
assert ret == 1
|
2019-08-24 02:14:10 +08:00
|
|
|
|
|
|
|
|
2019-09-14 02:30:52 +08:00
|
|
|
def test_diffing_output(capsys):
|
2019-09-16 00:54:03 +08:00
|
|
|
resource_path = get_resource_path('not_pretty_formatted_json.json')
|
2019-09-14 02:30:52 +08:00
|
|
|
expected_retval = 1
|
2019-09-25 03:42:24 +08:00
|
|
|
a = os.path.join('a', resource_path)
|
|
|
|
b = os.path.join('b', resource_path)
|
2020-02-06 03:10:42 +08:00
|
|
|
expected_out = f'''\
|
|
|
|
--- {a}
|
|
|
|
+++ {b}
|
2019-09-25 03:42:24 +08:00
|
|
|
@@ -1,6 +1,9 @@
|
|
|
|
{{
|
2019-09-16 01:48:00 +08:00
|
|
|
- "foo":
|
|
|
|
- "bar",
|
|
|
|
- "alist": [2, 34, 234],
|
|
|
|
- "blah": null
|
|
|
|
+ "alist": [
|
|
|
|
+ 2,
|
|
|
|
+ 34,
|
|
|
|
+ 234
|
|
|
|
+ ],
|
|
|
|
+ "blah": null,
|
|
|
|
+ "foo": "bar"
|
2019-09-25 03:42:24 +08:00
|
|
|
}}
|
2020-02-06 03:10:42 +08:00
|
|
|
'''
|
2019-09-16 00:54:03 +08:00
|
|
|
actual_retval = main([resource_path])
|
|
|
|
actual_out, actual_err = capsys.readouterr()
|
2019-08-24 02:14:10 +08:00
|
|
|
|
2019-09-14 02:30:52 +08:00
|
|
|
assert actual_retval == expected_retval
|
2019-09-16 00:54:03 +08:00
|
|
|
assert actual_out == expected_out
|
2019-10-13 04:39:15 +08:00
|
|
|
assert actual_err == ''
|