22import sys
33import textwrap
44import unittest
5- from subprocess import Popen , PIPE
5+ import subprocess
66from test import support
77from 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,\n 2]'
169- expect = b '[1, 2]'
165+ input_ = '[1,\n 2]'
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 \t 1,\n \t 2\n ]\n '
173+ input_ = '[1, 2]'
174+ expect = '[\n \t 1,\n \t 2\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