|
2 | 2 | import sys
|
3 | 3 | import textwrap
|
4 | 4 | import unittest
|
5 |
| -import subprocess |
| 5 | +from subprocess import Popen, PIPE |
6 | 6 | from test import support
|
7 | 7 | from test.support.script_helper import assert_python_ok
|
8 | 8 |
|
@@ -61,12 +61,11 @@ class TestTool(unittest.TestCase):
|
61 | 61 | """)
|
62 | 62 |
|
63 | 63 | def test_stdin_stdout(self):
|
64 |
| - with subprocess.Popen( |
65 |
| - (sys.executable, '-m', 'json.tool'), |
66 |
| - stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc: |
| 64 | + args = sys.executable, '-m', 'json.tool' |
| 65 | + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: |
67 | 66 | out, err = proc.communicate(self.data.encode())
|
68 | 67 | self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
|
69 |
| - self.assertEqual(err, None) |
| 68 | + self.assertEqual(err, b'') |
70 | 69 |
|
71 | 70 | def _create_infile(self):
|
72 | 71 | infile = support.TESTFN
|
@@ -114,3 +113,25 @@ def test_no_ensure_ascii_flag(self):
|
114 | 113 | self.assertEqual(out.splitlines(),
|
115 | 114 | self.expect_without_sort_keys.encode().splitlines())
|
116 | 115 | self.assertEqual(err, b'')
|
| 116 | + |
| 117 | + def test_indent(self): |
| 118 | + json_stdin = b'[1, 2]' |
| 119 | + expect = textwrap.dedent(b'''\ |
| 120 | + [ |
| 121 | + 1, |
| 122 | + 2 |
| 123 | + ] |
| 124 | + ''') |
| 125 | + args = sys.executable, '-m', 'json.tool', '--indent', '2' |
| 126 | + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: |
| 127 | + json_stdout, err = proc.communicate(json_stdin) |
| 128 | + self.assertEqual(expect.splitlines(), json_stdout.splitlines()) |
| 129 | + self.assertEqual(err, b'') |
| 130 | + |
| 131 | + def test_no_indent(self): |
| 132 | + json_stdin = b'[1, 2]' |
| 133 | + args = sys.executable, '-m', 'json.tool', '--no-indent' |
| 134 | + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: |
| 135 | + json_stdout, err = proc.communicate(json_stdin) |
| 136 | + self.assertEqual(json_stdin, json_stdout) |
| 137 | + self.assertEqual(err, b'') |
0 commit comments