1+ import errno
12import os
23import sys
34import textwrap
45import unittest
5- from subprocess import Popen , PIPE
6+ import subprocess
7+
68from test import support
79from test .support import os_helper
810from test .support .script_helper import assert_python_ok
@@ -85,10 +87,9 @@ class TestTool(unittest.TestCase):
8587
8688 def test_stdin_stdout (self ):
8789 args = sys .executable , '-m' , 'json.tool'
88- with Popen (args , stdin = PIPE , stdout = PIPE , stderr = PIPE ) as proc :
89- out , err = proc .communicate (self .data .encode ())
90- self .assertEqual (out .splitlines (), self .expect .encode ().splitlines ())
91- self .assertEqual (err , b'' )
90+ process = subprocess .run (args , input = self .data , capture_output = True , text = True , check = True )
91+ self .assertEqual (process .stdout , self .expect )
92+ self .assertEqual (process .stderr , '' )
9293
9394 def _create_infile (self , data = None ):
9495 infile = os_helper .TESTFN
@@ -124,18 +125,26 @@ def test_infile_outfile(self):
124125 outfile = os_helper .TESTFN + '.out'
125126 rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile , outfile )
126127 self .addCleanup (os .remove , outfile )
127- with open (outfile , "r" ) as fp :
128+ with open (outfile , "r" , encoding = "utf-8" ) as fp :
129+ self .assertEqual (fp .read (), self .expect )
130+ self .assertEqual (rc , 0 )
131+ self .assertEqual (out , b'' )
132+ self .assertEqual (err , b'' )
133+
134+ def test_writing_in_place (self ):
135+ infile = self ._create_infile ()
136+ rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile , infile )
137+ with open (infile , "r" , encoding = "utf-8" ) as fp :
128138 self .assertEqual (fp .read (), self .expect )
129139 self .assertEqual (rc , 0 )
130140 self .assertEqual (out , b'' )
131141 self .assertEqual (err , b'' )
132142
133143 def test_jsonlines (self ):
134144 args = sys .executable , '-m' , 'json.tool' , '--json-lines'
135- with Popen (args , stdin = PIPE , stdout = PIPE , stderr = PIPE ) as proc :
136- out , err = proc .communicate (self .jsonlines_raw .encode ())
137- self .assertEqual (out .splitlines (), self .jsonlines_expect .encode ().splitlines ())
138- self .assertEqual (err , b'' )
145+ process = subprocess .run (args , input = self .jsonlines_raw , capture_output = True , text = True , check = True )
146+ self .assertEqual (process .stdout , self .jsonlines_expect )
147+ self .assertEqual (process .stderr , '' )
139148
140149 def test_help_flag (self ):
141150 rc , out , err = assert_python_ok ('-m' , 'json.tool' , '-h' )
@@ -150,3 +159,73 @@ def test_sort_keys_flag(self):
150159 self .assertEqual (out .splitlines (),
151160 self .expect_without_sort_keys .encode ().splitlines ())
152161 self .assertEqual (err , b'' )
162+
163+ def test_indent (self ):
164+ input_ = '[1, 2]'
165+ expect = textwrap .dedent ('''\
166+ [
167+ 1,
168+ 2
169+ ]
170+ ''' )
171+ args = sys .executable , '-m' , 'json.tool' , '--indent' , '2'
172+ process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
173+ self .assertEqual (process .stdout , expect )
174+ self .assertEqual (process .stderr , '' )
175+
176+ def test_no_indent (self ):
177+ input_ = '[1,\n 2]'
178+ expect = '[1, 2]\n '
179+ args = sys .executable , '-m' , 'json.tool' , '--no-indent'
180+ process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
181+ self .assertEqual (process .stdout , expect )
182+ self .assertEqual (process .stderr , '' )
183+
184+ def test_tab (self ):
185+ input_ = '[1, 2]'
186+ expect = '[\n \t 1,\n \t 2\n ]\n '
187+ args = sys .executable , '-m' , 'json.tool' , '--tab'
188+ process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
189+ self .assertEqual (process .stdout , expect )
190+ self .assertEqual (process .stderr , '' )
191+
192+ def test_compact (self ):
193+ input_ = '[ 1 ,\n 2]'
194+ expect = '[1,2]\n '
195+ args = sys .executable , '-m' , 'json.tool' , '--compact'
196+ process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
197+ self .assertEqual (process .stdout , expect )
198+ self .assertEqual (process .stderr , '' )
199+
200+ def test_no_ensure_ascii_flag (self ):
201+ infile = self ._create_infile ('{"key":"💩"}' )
202+ outfile = os_helper .TESTFN + '.out'
203+ self .addCleanup (os .remove , outfile )
204+ assert_python_ok ('-m' , 'json.tool' , '--no-ensure-ascii' , infile , outfile )
205+ with open (outfile , "rb" ) as f :
206+ lines = f .read ().splitlines ()
207+ # asserting utf-8 encoded output file
208+ expected = [b'{' , b' "key": "\xf0 \x9f \x92 \xa9 "' , b"}" ]
209+ self .assertEqual (lines , expected )
210+
211+ def test_ensure_ascii_default (self ):
212+ infile = self ._create_infile ('{"key":"💩"}' )
213+ outfile = os_helper .TESTFN + '.out'
214+ self .addCleanup (os .remove , outfile )
215+ assert_python_ok ('-m' , 'json.tool' , infile , outfile )
216+ with open (outfile , "rb" ) as f :
217+ lines = f .read ().splitlines ()
218+ # asserting an ascii encoded output file
219+ expected = [b'{' , rb' "key": "\ud83d\udca9"' , b"}" ]
220+ self .assertEqual (lines , expected )
221+
222+ @unittest .skipIf (sys .platform == "win32" , "The test is failed with ValueError on Windows" )
223+ def test_broken_pipe_error (self ):
224+ cmd = [sys .executable , '-m' , 'json.tool' ]
225+ proc = subprocess .Popen (cmd ,
226+ stdout = subprocess .PIPE ,
227+ stdin = subprocess .PIPE )
228+ # bpo-39828: Closing before json.tool attempts to write into stdout.
229+ proc .stdout .close ()
230+ proc .communicate (b'"{}"' )
231+ self .assertEqual (proc .returncode , errno .EPIPE )
0 commit comments