E575 bpo-29636: json.tool: Add document for indentation options. (GH-17482) · python/cpython@15fb7fa · GitHub
[go: up one dir, main page]

Skip to content

Commit 15fb7fa

Browse files
dhimmelmethane
authored andcommitted
bpo-29636: json.tool: Add document for indentation options. (GH-17482)
And updated test to use subprocess.run
1 parent 4443450 commit 15fb7fa

File tree

3 files changed

+35
-34
lines changed
  • test/test_json
  • 3 files changed

    +35
    -34
    lines changed

    Doc/library/json.rst

    Lines changed: 6 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -744,6 +744,12 @@ Command line options
    744744

    745745
    .. versionadded:: 3.8
    746746

    747+
    .. cmdoption:: --indent, --tab, --no-indent, --compact
    748+
    749+
    Mutually exclusive options for whitespace control
    750+
    751+
    .. versionadded:: 3.9
    752+
    747753
    .. cmdoption:: -h, --help
    748754

    749755
    Show the help message.

    Lib/json/tool.py

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -33,7 +33,8 @@ def main():
    3333
    parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
    3434
    help='disable escaping of non-ASCII characters')
    3535
    parser.add_argument('--json-lines', action='store_true', default=False,
    36-
    help='parse input using the jsonlines format')
    36+
    help='parse input using the JSON Lines format. '
    37+
    'Use with --no-indent or --compact to produce valid JSON Lines output.')
    3738
    group = parser.add_mutually_exclusive_group()
    3839
    group.add_argument('--indent', default=4, type=int,
    3940
    help='separate items with newlines and use this number '

    Lib/test/test_json/test_tool.py

    Lines changed: 27 additions & 33 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2,7 +2,7 @@
    22
    import sys
    33
    import textwrap
    44
    import unittest
    5-
    from subprocess import Popen, PIPE
    5+
    import subprocess
    66
    from test import support
    77
    from test.support.script_helper import assert_python_ok
    88

    @@ -84,10 +84,9 @@ class TestTool(unittest.TestCase):
    8484

    8585
    def test_stdin_stdout(self):
    8686
    args = sys.executable, '-m', 'json.tool'
    87-
    with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
    88-
    out, err = proc.communicate(self.data.encode())
    89-
    self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
    90-
    self.assertEqual(err, b'')
    87+
    process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True)
    88+
    self.assertEqual(process.stdout, self.expect)
    89+
    self.assertEqual(process.stderr, '')
    9190

    9291
    def _create_infile(self, data=None):
    9392
    infile = support.TESTFN
    @@ -131,10 +130,9 @@ def test_infile_outfile(self):
    131130

    132131
    def test_jsonlines(self):
    133132
    args = sys.executable, '-m', 'json.tool', '--json-lines'
    134-
    with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
    135-
    out, err = proc.communicate(self.jsonlines_raw.encode())
    136-
    self.assertEqual(out.splitlines(), self.jsonlines_expect.encode().splitlines())
    137-
    self.assertEqual(err, b'')
    133+
    process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True)
    134+
    self.assertEqual(process.stdout, self.jsonlines_expect)
    135+
    self.assertEqual(process.stderr, '')
    138136

    139137
    def test_help_flag(self):
    140138
    rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
    @@ -151,45 +149,41 @@ def test_sort_keys_flag(self):
    151149
    self.assertEqual(err, b'')
    152150

    153151
    def test_indent(self):
    154-
    json_stdin = b'[1, 2]'
    152+
    input_ = '[1, 2]'
    155153
    expect = textwrap.dedent('''\
    156154
    [
    157155
    1,
    158156
    2
    159157
    ]
    160-
    ''').encode()
    158+
    ''')
    161159
    args = sys.executable, '-m', 'json.tool', '--indent', '2'
    162-
    with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
    163-
    json_stdout, err = proc.communicate(json_stdin)
    164-
    self.assertEqual(expect.splitlines(), json_stdout.splitlines())
    165-
    self.assertEqual(err, b'')
    160+
    process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
    161+
    self.assertEqual(process.stdout, expect)
    162+
    self.assertEqual(process.stderr, '')
    166163

    167164
    def test_no_indent(self):
    168-
    json_stdin = b'[1,\n2]'
    169-
    expect = b'[1, 2]'
    165+
    input_ = '[1,\n2]'
    166+
    expect = '[1, 2]\n'
    170167
    args = sys.executable, '-m', 'json.tool', '--no-indent'
    171-
    with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
    172-
    json_stdout, err = proc.communicate(json_stdin)
    173-
    self.assertEqual(expect.splitlines(), json_stdout.splitlines())
    174-
    self.assertEqual(err, b'')
    168+
    process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
    169+
    self.assertEqual(process.stdout, expect)
    170+
    self.assertEqual(process.stderr, '')
    175171

    176172
    def test_tab(self):
    177-
    json_stdin = b'[1, 2]'
    178-
    expect = b'[\n\t1,\n\t2\n]\n'
    173+
    input_ = '[1, 2]'
    174+
    expect = '[\n\t1,\n\t2\n]\n'
    179175
    args = sys.executable, '-m', 'json.tool', '--tab'
    180-
    with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
    181-
    json_stdout, err = proc.communicate(json_stdin)
    182-
    self.assertEqual(expect.splitlines(), json_stdout.splitlines())
    183-
    self.assertEqual(err, b'')
    176+
    process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
    177+
    self.assertEqual(process.stdout, expect)
    178+
    self.assertEqual(process.stderr, '')
    184179

    185180
    def test_compact(self):
    186-
    json_stdin = b'[ 1 ,\n 2]'
    187-
    expect = b'[1,2]'
    181+
    input_ = '[ 1 ,\n 2]'
    182+
    expect = '[1,2]\n'
    188183
    args = sys.executable, '-m', 'json.tool', '--compact'
    189-
    with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
    190-
    json_stdout, err = proc.communicate(json_stdin)
    191-
    self.assertEqual(expect.splitlines(), json_stdout.splitlines())
    192-
    self.assertEqual(err, b'')
    184+
    process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
    185+
    self.assertEqual(process.stdout, expect)
    186+
    self.assertEqual(process.stderr, '')
    193187

    194188
    def test_no_ensure_ascii_flag(self):
    195189
    infile = self._create_infile('{"key":"💩"}')

    0 commit comments

    Comments
     (0)
    0