2
2
import sys
3
3
import textwrap
4
4
import unittest
5
- from subprocess import Popen , PIPE
5
+ import subprocess
6
6
from test import support
7
7
from test .support .script_helper import assert_python_ok
8
8
@@ -84,10 +84,9 @@ class TestTool(unittest.TestCase):
84
84
85
85
def test_stdin_stdout (self ):
86
86
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 , '' )
91
90
92
91
def _create_infile (self , data = None ):
93
92
infile = support .TESTFN
@@ -131,10 +130,9 @@ def test_infile_outfile(self):
131
130
132
131
def test_jsonlines (self ):
133
132
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 , '' )
138
136
139
137
def test_help_flag (self ):
140
138
rc , out , err = assert_python_ok ('-m' , 'json.tool' , '-h' )
@@ -151,45 +149,41 @@ def test_sort_keys_flag(self):
151
149
self .assertEqual (err , b'' )
152
150
153
151
def test_indent (self ):
154
- json_stdin = b '[1, 2]'
152
+ input_ = '[1, 2]'
155
153
expect = textwrap .dedent ('''\
156
154
[
157
155
1,
158
156
2
159
157
]
160
- ''' ). encode ()
158
+ ''' )
161
159
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 , '' )
166
163
167
164
def test_no_indent (self ):
168
- json_stdin = b '[1,\n 2]'
169
- expect = b '[1, 2]'
165
+ input_ = '[1,\n 2]'
166
+ expect = '[1, 2]\n '
170
167
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 , '' )
175
171
176
172
def test_tab (self ):
177
- json_stdin = b '[1, 2]'
178
- expect = b '[\n \t 1,\n \t 2\n ]\n '
173
+ input_ = '[1, 2]'
174
+ expect = '[\n \t 1,\n \t 2\n ]\n '
179
175
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 , '' )
184
179
185
180
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 '
188
183
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 , '' )
193
187
194
188
def test_no_ensure_ascii_flag (self ):
195
189
infile = self ._create_infile ('{"key":"💩"}' )
0 commit comments