blob: 5ded724a6a095f04e38342028411fba999e1f69f [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Joey Pinhasec6c39e2019-09-24 15:42:24 -04003import os
Anthony Sottilea99475a2016-05-27 14:09:50 -07004import shutil
Léo Cavaillé55bf22d2015-06-10 17:08:48 -04005
Anthony Sottile3a3a7a12015-12-25 09:37:18 -08006import pytest
7
Anthony Sottile030bfac2019-01-31 19:19:10 -08008from pre_commit_hooks.pretty_format_json import main
Calum Lind5b6ddaf2017-12-10 09:34:36 +00009from pre_commit_hooks.pretty_format_json import parse_num_to_int
Léo Cavaillé55bf22d2015-06-10 17:08:48 -040010from testing.util import get_resource_path
11
12
Calum Lind5b6ddaf2017-12-10 09:34:36 +000013def test_parse_num_to_int():
14 assert parse_num_to_int('0') == 0
15 assert parse_num_to_int('2') == 2
16 assert parse_num_to_int('\t') == '\t'
17 assert parse_num_to_int(' ') == ' '
Sander Maijersa5628862016-06-10 20:16:00 +020018
Calum Lind00974ef2017-12-10 08:57:34 +000019
Anthony Sottilee9aea742017-07-15 12:56:51 -070020@pytest.mark.parametrize(
21 ('filename', 'expected_retval'), (
22 ('not_pretty_formatted_json.json', 1),
23 ('unsorted_pretty_formatted_json.json', 1),
24 ('non_ascii_pretty_formatted_json.json', 1),
25 ('pretty_formatted_json.json', 0),
26 ),
27)
Anthony Sottile030bfac2019-01-31 19:19:10 -080028def test_main(filename, expected_retval):
29 ret = main([get_resource_path(filename)])
Léo Cavaillé55bf22d2015-06-10 17:08:48 -040030 assert ret == expected_retval
31
32
Anthony Sottilee9aea742017-07-15 12:56:51 -070033@pytest.mark.parametrize(
34 ('filename', 'expected_retval'), (
35 ('not_pretty_formatted_json.json', 1),
36 ('unsorted_pretty_formatted_json.json', 0),
37 ('non_ascii_pretty_formatted_json.json', 1),
38 ('pretty_formatted_json.json', 0),
39 ),
40)
Anthony Sottile030bfac2019-01-31 19:19:10 -080041def test_unsorted_main(filename, expected_retval):
42 ret = main(['--no-sort-keys', get_resource_path(filename)])
mattcleggbd4e37c2016-04-14 09:36:47 +010043 assert ret == expected_retval
44
45
Anthony Sottilee9aea742017-07-15 12:56:51 -070046@pytest.mark.parametrize(
47 ('filename', 'expected_retval'), (
48 ('not_pretty_formatted_json.json', 1),
49 ('unsorted_pretty_formatted_json.json', 1),
50 ('non_ascii_pretty_formatted_json.json', 1),
51 ('pretty_formatted_json.json', 1),
52 ('tab_pretty_formatted_json.json', 0),
53 ),
54)
Anthony Sottilef5c42a02020-02-05 11:10:42 -080055def test_tab_main(filename, expected_retval):
Anthony Sottile030bfac2019-01-31 19:19:10 -080056 ret = main(['--indent', '\t', get_resource_path(filename)])
Sander Maijersa5628862016-06-10 20:16:00 +020057 assert ret == expected_retval
58
59
Anthony Sottile030bfac2019-01-31 19:19:10 -080060def test_non_ascii_main():
Anthony Sottile45756522019-02-11 19:56:15 -080061 ret = main((
62 '--no-ensure-ascii',
63 get_resource_path('non_ascii_pretty_formatted_json.json'),
64 ))
ushuz10f8bd22017-03-16 14:54:55 +080065 assert ret == 0
66
67
Anthony Sottile030bfac2019-01-31 19:19:10 -080068def test_autofix_main(tmpdir):
Anthony Sottile7376a292015-12-26 10:58:33 -080069 srcfile = tmpdir.join('to_be_json_formatted.json')
Anthony Sottilea99475a2016-05-27 14:09:50 -070070 shutil.copyfile(
71 get_resource_path('not_pretty_formatted_json.json'),
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040072 str(srcfile),
Anthony Sottilea99475a2016-05-27 14:09:50 -070073 )
Léo Cavaillé55bf22d2015-06-10 17:08:48 -040074
75 # now launch the autofix on that file
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040076 ret = main(['--autofix', str(srcfile)])
Léo Cavaillé55bf22d2015-06-10 17:08:48 -040077 # it should have formatted it
78 assert ret == 1
79
Anthony Sottile7376a292015-12-26 10:58:33 -080080 # file was formatted (shouldn't trigger linter again)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040081 ret = main([str(srcfile)])
Léo Cavaillé55bf22d2015-06-10 17:08:48 -040082 assert ret == 0
83
dmlb200084b1fb62016-11-03 15:54:48 -070084
dmlb2000845a3d52016-11-03 09:41:23 -070085def test_orderfile_get_pretty_format():
Anthony Sottile45756522019-02-11 19:56:15 -080086 ret = main((
87 '--top-keys=alist', get_resource_path('pretty_formatted_json.json'),
88 ))
dmlb2000845a3d52016-11-03 09:41:23 -070089 assert ret == 0
90
dmlb200084b1fb62016-11-03 15:54:48 -070091
dmlb20007f057b02016-11-03 15:51:24 -070092def test_not_orderfile_get_pretty_format():
Anthony Sottile45756522019-02-11 19:56:15 -080093 ret = main((
94 '--top-keys=blah', get_resource_path('pretty_formatted_json.json'),
95 ))
dmlb2000845a3d52016-11-03 09:41:23 -070096 assert ret == 1
Léo Cavaillé55bf22d2015-06-10 17:08:48 -040097
dmlb200084b1fb62016-11-03 15:54:48 -070098
David Browne9e9c3d2016-11-03 18:05:43 -070099def test_top_sorted_get_pretty_format():
Anthony Sottile45756522019-02-11 19:56:15 -0800100 ret = main((
101 '--top-keys=01-alist,alist', get_resource_path('top_sorted_json.json'),
102 ))
David Browne9e9c3d2016-11-03 18:05:43 -0700103 assert ret == 0
104
105
Anthony Sottile030bfac2019-01-31 19:19:10 -0800106def test_badfile_main():
107 ret = main([get_resource_path('ok_yaml.yaml')])
Léo Cavaillé55bf22d2015-06-10 17:08:48 -0400108 assert ret == 1
Joey Pinhasb28837a2019-08-23 14:14:10 -0400109
110
Joey Pinhas0ff23d42019-09-13 14:30:52 -0400111def test_diffing_output(capsys):
Joey Pinhas35c76c42019-09-15 12:54:03 -0400112 resource_path = get_resource_path('not_pretty_formatted_json.json')
Joey Pinhas0ff23d42019-09-13 14:30:52 -0400113 expected_retval = 1
Joey Pinhasec6c39e2019-09-24 15:42:24 -0400114 a = os.path.join('a', resource_path)
115 b = os.path.join('b', resource_path)
Anthony Sottilef5c42a02020-02-05 11:10:42 -0800116 expected_out = f'''\
117--- {a}
118+++ {b}
Joey Pinhasec6c39e2019-09-24 15:42:24 -0400119@@ -1,6 +1,9 @@
120 {{
Joey Pinhas31e740e2019-09-15 13:48:00 -0400121- "foo":
122- "bar",
123- "alist": [2, 34, 234],
124- "blah": null
125+ "alist": [
126+ 2,
127+ 34,
128+ 234
129+ ],
130+ "blah": null,
131+ "foo": "bar"
Joey Pinhasec6c39e2019-09-24 15:42:24 -0400132 }}
Anthony Sottilef5c42a02020-02-05 11:10:42 -0800133'''
Joey Pinhas35c76c42019-09-15 12:54:03 -0400134 actual_retval = main([resource_path])
135 actual_out, actual_err = capsys.readouterr()
Joey Pinhas0ff23d42019-09-13 14:30:52 -0400136
137 assert actual_retval == expected_retval
Joey Pinhas35c76c42019-09-15 12:54:03 -0400138 assert actual_out == expected_out
Anthony Sottile86a010b2019-10-12 13:39:15 -0700139 assert actual_err == ''