bpo-29636: json.tool: Add document for indentation options. (GH-17482)

And updated test to use subprocess.run
This commit is contained in:
Daniel Himmelstein 2019-12-07 07:14:40 -07:00 committed by Inada Naoki
parent 4443450fda
commit 15fb7fa881
3 changed files with 35 additions and 34 deletions

View file

@ -744,6 +744,12 @@ Command line options
.. versionadded:: 3.8 .. versionadded:: 3.8
.. cmdoption:: --indent, --tab, --no-indent, --compact
Mutually exclusive options for whitespace control
.. versionadded:: 3.9
.. cmdoption:: -h, --help .. cmdoption:: -h, --help
Show the help message. Show the help message.

View file

@ -33,7 +33,8 @@ def main():
parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false', parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
help='disable escaping of non-ASCII characters') help='disable escaping of non-ASCII characters')
parser.add_argument('--json-lines', action='store_true', default=False, parser.add_argument('--json-lines', action='store_true', default=False,
help='parse input using the jsonlines format') help='parse input using the JSON Lines format. '
'Use with --no-indent or --compact to produce valid JSON Lines output.')
group = parser.add_mutually_exclusive_group() group = parser.add_mutually_exclusive_group()
group.add_argument('--indent', default=4, type=int, group.add_argument('--indent', default=4, type=int,
help='separate items with newlines and use this number ' help='separate items with newlines and use this number '

View file

@ -2,7 +2,7 @@ import os
import sys import sys
import textwrap import textwrap
import unittest import unittest
from subprocess import Popen, PIPE import subprocess
from test import support from test import support
from test.support.script_helper import assert_python_ok from test.support.script_helper import assert_python_ok
@ -84,10 +84,9 @@ class TestTool(unittest.TestCase):
def test_stdin_stdout(self): def test_stdin_stdout(self):
args = sys.executable, '-m', 'json.tool' args = sys.executable, '-m', 'json.tool'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True)
out, err = proc.communicate(self.data.encode()) self.assertEqual(process.stdout, self.expect)
self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) self.assertEqual(process.stderr, '')
self.assertEqual(err, b'')
def _create_infile(self, data=None): def _create_infile(self, data=None):
infile = support.TESTFN infile = support.TESTFN
@ -131,10 +130,9 @@ class TestTool(unittest.TestCase):
def test_jsonlines(self): def test_jsonlines(self):
args = sys.executable, '-m', 'json.tool', '--json-lines' args = sys.executable, '-m', 'json.tool', '--json-lines'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True)
out, err = proc.communicate(self.jsonlines_raw.encode()) self.assertEqual(process.stdout, self.jsonlines_expect)
self.assertEqual(out.splitlines(), self.jsonlines_expect.encode().splitlines()) self.assertEqual(process.stderr, '')
self.assertEqual(err, b'')
def test_help_flag(self): def test_help_flag(self):
rc, out, err = assert_python_ok('-m', 'json.tool', '-h') rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
@ -151,45 +149,41 @@ class TestTool(unittest.TestCase):
self.assertEqual(err, b'') self.assertEqual(err, b'')
def test_indent(self): def test_indent(self):
json_stdin = b'[1, 2]' input_ = '[1, 2]'
expect = textwrap.dedent('''\ expect = textwrap.dedent('''\
[ [
1, 1,
2 2
] ]
''').encode() ''')
args = sys.executable, '-m', 'json.tool', '--indent', '2' args = sys.executable, '-m', 'json.tool', '--indent', '2'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
json_stdout, err = proc.communicate(json_stdin) self.assertEqual(process.stdout, expect)
self.assertEqual(expect.splitlines(), json_stdout.splitlines()) self.assertEqual(process.stderr, '')
self.assertEqual(err, b'')
def test_no_indent(self): def test_no_indent(self):
json_stdin = b'[1,\n2]' input_ = '[1,\n2]'
expect = b'[1, 2]' expect = '[1, 2]\n'
args = sys.executable, '-m', 'json.tool', '--no-indent' args = sys.executable, '-m', 'json.tool', '--no-indent'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
json_stdout, err = proc.communicate(json_stdin) self.assertEqual(process.stdout, expect)
self.assertEqual(expect.splitlines(), json_stdout.splitlines()) self.assertEqual(process.stderr, '')
self.assertEqual(err, b'')
def test_tab(self): def test_tab(self):
json_stdin = b'[1, 2]' input_ = '[1, 2]'
expect = b'[\n\t1,\n\t2\n]\n' expect = '[\n\t1,\n\t2\n]\n'
args = sys.executable, '-m', 'json.tool', '--tab' args = sys.executable, '-m', 'json.tool', '--tab'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
json_stdout, err = proc.communicate(json_stdin) self.assertEqual(process.stdout, expect)
self.assertEqual(expect.splitlines(), json_stdout.splitlines()) self.assertEqual(process.stderr, '')
self.assertEqual(err, b'')
def test_compact(self): def test_compact(self):
json_stdin = b'[ 1 ,\n 2]' input_ = '[ 1 ,\n 2]'
expect = b'[1,2]' expect = '[1,2]\n'
args = sys.executable, '-m', 'json.tool', '--compact' args = sys.executable, '-m', 'json.tool', '--compact'
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
json_stdout, err = proc.communicate(json_stdin) self.assertEqual(process.stdout, expect)
self.assertEqual(expect.splitlines(), json_stdout.splitlines()) self.assertEqual(process.stderr, '')
self.assertEqual(err, b'')
def test_no_ensure_ascii_flag(self): def test_no_ensure_ascii_flag(self):
infile = self._create_infile('{"key":"💩"}') infile = self._create_infile('{"key":"💩"}')