Debian Codegraph @

Lib/test/test_json/test_tool.py

raw
1 import errno
2 import pathlib
3 import os
4 import sys
5 import textwrap
6 import unittest
7 import subprocess
8
9 from test import support
10 from test.support import force_colorized, force_not_colorized, os_helper
11 from test.support.script_helper import assert_python_ok
12
13 from _colorize import get_theme
14
15
16 @support.requires_subprocess()
17 @support.skip_if_pgo_task
18 class TestMain(unittest.TestCase):
19 data = """
20
21 [["blorpie"],[ "whoops" ] , [
22 ],\t"d-shtaeou",\r"d-nthiouh",
23 "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
24 :"yes"} ]
25 """
26 module = 'json'
27
28 expect_without_sort_keys = textwrap.dedent("""\
29 [
30 [
31 "blorpie"
32 ],
33 [
34 "whoops"
35 ],
36 [],
37 "d-shtaeou",
38 "d-nthiouh",
39 "i-vhbjkhnth",
40 {
41 "nifty": 87
42 },
43 {
44 "field": "yes",
45 "morefield": false
46 }
47 ]
48 """)
49
50 expect = textwrap.dedent("""\
51 [
52 [
53 "blorpie"
54 ],
55 [
56 "whoops"
57 ],
58 [],
59 "d-shtaeou",
60 "d-nthiouh",
61 "i-vhbjkhnth",
62 {
63 "nifty": 87
64 },
65 {
66 "morefield": false,
67 "field": "yes"
68 }
69 ]
70 """)
71
72 jsonlines_raw = textwrap.dedent("""\
73 {"ingredients":["frog", "water", "chocolate", "glucose"]}
74 {"ingredients":["chocolate","steel bolts"]}
75 """)
76
77 jsonlines_expect = textwrap.dedent("""\
78 {
79 "ingredients": [
80 "frog",
81 "water",
82 "chocolate",
83 "glucose"
84 ]
85 }
86 {
87 "ingredients": [
88 "chocolate",
89 "steel bolts"
90 ]
91 }
92 """)
93
94 @force_not_colorized
95 def test_stdin_stdout(self):
96 args = sys.executable, '-m', self.module
97 process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True)
98 self.assertEqual(process.stdout, self.expect)
99 self.assertEqual(process.stderr, '')
100
101 def _create_infile(self, data=None):
102 infile = os_helper.TESTFN
103 with open(infile, "w", encoding="utf-8") as fp:
104 self.addCleanup(os.remove, infile)
105 fp.write(data or self.data)
106 return infile
107
108 def test_infile_stdout(self):
109 infile = self._create_infile()
110 rc, out, err = assert_python_ok('-m', self.module, infile,
111 PYTHON_COLORS='0')
112 self.assertEqual(rc, 0)
113 self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
114 self.assertEqual(err, b'')
115
116 def test_non_ascii_infile(self):
117 data = '{"msg": "\u3053\u3093\u306b\u3061\u306f"}'
118 expect = textwrap.dedent('''\
119 {
120 "msg": "\\u3053\\u3093\\u306b\\u3061\\u306f"
121 }
122 ''').encode()
123
124 infile = self._create_infile(data)
125 rc, out, err = assert_python_ok('-m', self.module, infile,
126 PYTHON_COLORS='0')
127
128 self.assertEqual(rc, 0)
129 self.assertEqual(out.splitlines(), expect.splitlines())
130 self.assertEqual(err, b'')
131
132 def test_infile_outfile(self):
133 infile = self._create_infile()
134 outfile = os_helper.TESTFN + '.out'
135 rc, out, err = assert_python_ok('-m', self.module, infile, outfile,
136 PYTHON_COLORS='0')
137 self.addCleanup(os.remove, outfile)
138 with open(outfile, "r", encoding="utf-8") as fp:
139 self.assertEqual(fp.read(), self.expect)
140 self.assertEqual(rc, 0)
141 self.assertEqual(out, b'')
142 self.assertEqual(err, b'')
143
144 def test_writing_in_place(self):
145 infile = self._create_infile()
146 rc, out, err = assert_python_ok('-m', self.module, infile, infile,
147 PYTHON_COLORS='0')
148 with open(infile, "r", encoding="utf-8") as fp:
149 self.assertEqual(fp.read(), self.expect)
150 self.assertEqual(rc, 0)
151 self.assertEqual(out, b'')
152 self.assertEqual(err, b'')
153
154 @force_not_colorized
155 def test_jsonlines(self):
156 args = sys.executable, '-m', self.module, '--json-lines'
157 process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True)
158 self.assertEqual(process.stdout, self.jsonlines_expect)
159 self.assertEqual(process.stderr, '')
160
161 @force_not_colorized
162 def test_jsonlines_from_file(self):
163 jsonl = pathlib.Path(__file__).parent / 'json_lines.jsonl'
164 args = sys.executable, '-m', self.module, '--json-lines', jsonl
165 process = subprocess.run(args, capture_output=True, text=True, check=True)
166 self.assertEqual(process.stdout, self.jsonlines_expect)
167 self.assertEqual(process.stderr, '')
168
169 def test_help_flag(self):
170 rc, out, err = assert_python_ok('-m', self.module, '-h',
171 PYTHON_COLORS='0')
172 self.assertEqual(rc, 0)
173 self.assertStartsWith(out, b'usage: ')
174 self.assertEqual(err, b'')
175
176 def test_sort_keys_flag(self):
177 infile = self._create_infile()
178 rc, out, err = assert_python_ok('-m', self.module, '--sort-keys', infile,
179 PYTHON_COLORS='0')
180 self.assertEqual(rc, 0)
181 self.assertEqual(out.splitlines(),
182 self.expect_without_sort_keys.encode().splitlines())
183 self.assertEqual(err, b'')
184
185 @force_not_colorized
186 def test_indent(self):
187 input_ = '[1, 2]'
188 expect = textwrap.dedent('''\
189 [
190 1,
191 2
192 ]
193 ''')
194 args = sys.executable, '-m', self.module, '--indent', '2'
195 process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
196 self.assertEqual(process.stdout, expect)
197 self.assertEqual(process.stderr, '')
198
199 @force_not_colorized
200 def test_no_indent(self):
201 input_ = '[1,\n2]'
202 expect = '[1, 2]\n'
203 args = sys.executable, '-m', self.module, '--no-indent'
204 process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
205 self.assertEqual(process.stdout, expect)
206 self.assertEqual(process.stderr, '')
207
208 @force_not_colorized
209 def test_tab(self):
210 input_ = '[1, 2]'
211 expect = '[\n\t1,\n\t2\n]\n'
212 args = sys.executable, '-m', self.module, '--tab'
213 process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
214 self.assertEqual(process.stdout, expect)
215 self.assertEqual(process.stderr, '')
216
217 @force_not_colorized
218 def test_compact(self):
219 input_ = '[ 1 ,\n 2]'
220 expect = '[1,2]\n'
221 args = sys.executable, '-m', self.module, '--compact'
222 process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
223 self.assertEqual(process.stdout, expect)
224 self.assertEqual(process.stderr, '')
225
226 def test_no_ensure_ascii_flag(self):
227 infile = self._create_infile('{"key":"💩"}')
228 outfile = os_helper.TESTFN + '.out'
229 self.addCleanup(os.remove, outfile)
230 assert_python_ok('-m', self.module, '--no-ensure-ascii', infile,
231 outfile, PYTHON_COLORS='0')
232 with open(outfile, "rb") as f:
233 lines = f.read().splitlines()
234 # asserting utf-8 encoded output file
235 expected = [b'{', b' "key": "\xf0\x9f\x92\xa9"', b"}"]
236 self.assertEqual(lines, expected)
237
238 def test_ensure_ascii_default(self):
239 infile = self._create_infile('{"key":"💩"}')
240 outfile = os_helper.TESTFN + '.out'
241 self.addCleanup(os.remove, outfile)
242 assert_python_ok('-m', self.module, infile, outfile, PYTHON_COLORS='0')
243 with open(outfile, "rb") as f:
244 lines = f.read().splitlines()
245 # asserting an ascii encoded output file
246 expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"]
247 self.assertEqual(lines, expected)
248
249 @force_not_colorized
250 @unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows")
251 def test_broken_pipe_error(self):
252 cmd = [sys.executable, '-m', self.module]
253 proc = subprocess.Popen(cmd,
254 stdout=subprocess.PIPE,
255 stdin=subprocess.PIPE)
256 # bpo-39828: Closing before json attempts to write into stdout.
257 proc.stdout.close()
258 proc.communicate(b'"{}"')
259 self.assertEqual(proc.returncode, errno.EPIPE)
260
261 @force_colorized
262 def test_colors(self):
263 infile = os_helper.TESTFN
264 self.addCleanup(os.remove, infile)
265
266 t = get_theme().syntax
267 ob = "{"
268 cb = "}"
269
270 cases = (
271 ('{}', '{}'),
272 ('[]', '[]'),
273 ('null', f'{t.keyword}null{t.reset}'),
274 ('true', f'{t.keyword}true{t.reset}'),
275 ('false', f'{t.keyword}false{t.reset}'),
276 ('NaN', f'{t.number}NaN{t.reset}'),
277 ('Infinity', f'{t.number}Infinity{t.reset}'),
278 ('-Infinity', f'{t.number}-Infinity{t.reset}'),
279 ('"foo"', f'{t.string}"foo"{t.reset}'),
280 (r'" \"foo\" "', f'{t.string}" \\"foo\\" "{t.reset}'),
281 ('"α"', f'{t.string}"\\u03b1"{t.reset}'),
282 ('123', f'{t.number}123{t.reset}'),
283 ('-1.25e+23', f'{t.number}-1.25e+23{t.reset}'),
284 (r'{"\\": ""}',
285 f'''\
286 {ob}
287 {t.definition}"\\\\"{t.reset}: {t.string}""{t.reset}
288 {cb}'''),
289 (r'{"\\\\": ""}',
290 f'''\
291 {ob}
292 {t.definition}"\\\\\\\\"{t.reset}: {t.string}""{t.reset}
293 {cb}'''),
294 ('''\
295 {
296 "foo": "bar",
297 "baz": 1234,
298 "qux": [true, false, null],
299 "xyz": [NaN, -Infinity, Infinity]
300 }''',
301 f'''\
302 {ob}
303 {t.definition}"foo"{t.reset}: {t.string}"bar"{t.reset},
304 {t.definition}"baz"{t.reset}: {t.number}1234{t.reset},
305 {t.definition}"qux"{t.reset}: [
306 {t.keyword}true{t.reset},
307 {t.keyword}false{t.reset},
308 {t.keyword}null{t.reset}
309 ],
310 {t.definition}"xyz"{t.reset}: [
311 {t.number}NaN{t.reset},
312 {t.number}-Infinity{t.reset},
313 {t.number}Infinity{t.reset}
314 ]
315 {cb}'''),
316 )
317
318 for input_, expected in cases:
319 with self.subTest(input=input_):
320 with open(infile, "w", encoding="utf-8") as fp:
321 fp.write(input_)
322 _, stdout_b, _ = assert_python_ok(
323 '-m', self.module, infile, FORCE_COLOR='1', __isolated='1'
324 )
325 stdout = stdout_b.decode()
326 stdout = stdout.replace('\r\n', '\n') # normalize line endings
327 stdout = stdout.strip()
328 self.assertEqual(stdout, expected)
329
330
331 @support.requires_subprocess()
332 @support.skip_if_pgo_task
333 class TestTool(TestMain):
334 module = 'json.tool'
335
336
337 if __name__ == "__main__":
338 unittest.main()