1111
1212
1313@support .requires_subprocess ()
14- class TestTool (unittest .TestCase ):
14+ class TestMain (unittest .TestCase ):
1515 data = """
1616
1717 [["blorpie"],[ "whoops" ] , [
1818 ],\t "d-shtaeou",\r "d-nthiouh",
1919 "i-vhbjkhnth", {"nifty":87}, {"morefield" :\t false,"field"
2020 :"yes"} ]
2121 """
22+ module = 'json'
2223
2324 expect_without_sort_keys = textwrap .dedent ("""\
2425 [
@@ -87,7 +88,7 @@ class TestTool(unittest.TestCase):
8788 """ )
8889
8990 def test_stdin_stdout (self ):
90- args = sys .executable , '-m' , 'json.tool'
91+ args = sys .executable , '-m' , self . module
9192 process = subprocess .run (args , input = self .data , capture_output = True , text = True , check = True )
9293 self .assertEqual (process .stdout , self .expect )
9394 self .assertEqual (process .stderr , '' )
@@ -101,7 +102,7 @@ def _create_infile(self, data=None):
101102
102103 def test_infile_stdout (self ):
103104 infile = self ._create_infile ()
104- rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile )
105+ rc , out , err = assert_python_ok ('-m' , self . module , infile )
105106 self .assertEqual (rc , 0 )
106107 self .assertEqual (out .splitlines (), self .expect .encode ().splitlines ())
107108 self .assertEqual (err , b'' )
@@ -115,7 +116,7 @@ def test_non_ascii_infile(self):
115116 ''' ).encode ()
116117
117118 infile = self ._create_infile (data )
118- rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile )
119+ rc , out , err = assert_python_ok ('-m' , self . module , infile )
119120
120121 self .assertEqual (rc , 0 )
121122 self .assertEqual (out .splitlines (), expect .splitlines ())
@@ -124,7 +125,7 @@ def test_non_ascii_infile(self):
124125 def test_infile_outfile (self ):
125126 infile = self ._create_infile ()
126127 outfile = os_helper .TESTFN + '.out'
127- rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile , outfile )
128+ rc , out , err = assert_python_ok ('-m' , self . module , infile , outfile )
128129 self .addCleanup (os .remove , outfile )
129130 with open (outfile , "r" , encoding = "utf-8" ) as fp :
130131 self .assertEqual (fp .read (), self .expect )
@@ -134,28 +135,28 @@ def test_infile_outfile(self):
134135
135136 def test_writing_in_place (self ):
136137 infile = self ._create_infile ()
137- rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile , infile )
138+ rc , out , err = assert_python_ok ('-m' , self . module , infile , infile )
138139 with open (infile , "r" , encoding = "utf-8" ) as fp :
139140 self .assertEqual (fp .read (), self .expect )
140141 self .assertEqual (rc , 0 )
141142 self .assertEqual (out , b'' )
142143 self .assertEqual (err , b'' )
143144
144145 def test_jsonlines (self ):
145- args = sys .executable , '-m' , 'json.tool' , '--json-lines'
146+ args = sys .executable , '-m' , self . module , '--json-lines'
146147 process = subprocess .run (args , input = self .jsonlines_raw , capture_output = True , text = True , check = True )
147148 self .assertEqual (process .stdout , self .jsonlines_expect )
148149 self .assertEqual (process .stderr , '' )
149150
150151 def test_help_flag (self ):
151- rc , out , err = assert_python_ok ('-m' , 'json.tool' , '-h' )
152+ rc , out , err = assert_python_ok ('-m' , self . module , '-h' )
152153 self .assertEqual (rc , 0 )
153154 self .assertTrue (out .startswith (b'usage: ' ))
154155 self .assertEqual (err , b'' )
155156
156157 def test_sort_keys_flag (self ):
157158 infile = self ._create_infile ()
158- rc , out , err = assert_python_ok ('-m' , 'json.tool' , '--sort-keys' , infile )
159+ rc , out , err = assert_python_ok ('-m' , self . module , '--sort-keys' , infile )
159160 self .assertEqual (rc , 0 )
160161 self .assertEqual (out .splitlines (),
161162 self .expect_without_sort_keys .encode ().splitlines ())
@@ -169,31 +170,31 @@ def test_indent(self):
169170 2
170171 ]
171172 ''' )
172- args = sys .executable , '-m' , 'json.tool' , '--indent' , '2'
173+ args = sys .executable , '-m' , self . module , '--indent' , '2'
173174 process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
174175 self .assertEqual (process .stdout , expect )
175176 self .assertEqual (process .stderr , '' )
176177
177178 def test_no_indent (self ):
178179 input_ = '[1,\n 2]'
179180 expect = '[1, 2]\n '
180- args = sys .executable , '-m' , 'json.tool' , '--no-indent'
181+ args = sys .executable , '-m' , self . module , '--no-indent'
181182 process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
182183 self .assertEqual (process .stdout , expect )
183184 self .assertEqual (process .stderr , '' )
184185
185186 def test_tab (self ):
186187 input_ = '[1, 2]'
187188 expect = '[\n \t 1,\n \t 2\n ]\n '
188- args = sys .executable , '-m' , 'json.tool' , '--tab'
189+ args = sys .executable , '-m' , self . module , '--tab'
189190 process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
190191 self .assertEqual (process .stdout , expect )
191192 self .assertEqual (process .stderr , '' )
192193
193194 def test_compact (self ):
194195 input_ = '[ 1 ,\n 2]'
195196 expect = '[1,2]\n '
196- args = sys .executable , '-m' , 'json.tool' , '--compact'
197+ args = sys .executable , '-m' , self . module , '--compact'
197198 process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
198199 self .assertEqual (process .stdout , expect )
199200 self .assertEqual (process .stderr , '' )
@@ -202,7 +203,7 @@ def test_no_ensure_ascii_flag(self):
202203 infile = self ._create_infile ('{"key":"💩"}' )
203204 outfile = os_helper .TESTFN + '.out'
204205 self .addCleanup (os .remove , outfile )
205- assert_python_ok ('-m' , 'json.tool' , '--no-ensure-ascii' , infile , outfile )
206+ assert_python_ok ('-m' , self . module , '--no-ensure-ascii' , infile , outfile )
206207 with open (outfile , "rb" ) as f :
207208 lines = f .read ().splitlines ()
208209 # asserting utf-8 encoded output file
@@ -213,7 +214,7 @@ def test_ensure_ascii_default(self):
213214 infile = self ._create_infile ('{"key":"💩"}' )
214215 outfile = os_helper .TESTFN + '.out'
215216 self .addCleanup (os .remove , outfile )
216- assert_python_ok ('-m' , 'json.tool' , infile , outfile )
217+ assert_python_ok ('-m' , self . module , infile , outfile )
217218 with open (outfile , "rb" ) as f :
218219 lines = f .read ().splitlines ()
219220 # asserting an ascii encoded output file
@@ -222,11 +223,16 @@ def test_ensure_ascii_default(self):
222223
223224 @unittest .skipIf (sys .platform == "win32" , "The test is failed with ValueError on Windows" )
224225 def test_broken_pipe_error (self ):
225- cmd = [sys .executable , '-m' , 'json.tool' ]
226+ cmd = [sys .executable , '-m' , self . module ]
226227 proc = subprocess .Popen (cmd ,
227228 stdout = subprocess .PIPE ,
228229 stdin = subprocess .PIPE )
229- # bpo-39828: Closing before json.tool attempts to write into stdout.
230+ # bpo-39828: Closing before json attempts to write into stdout.
230231 proc .stdout .close ()
231232 proc .communicate (b'"{}"' )
232233 self .assertEqual (proc .returncode , errno .EPIPE )
234+
235+
236+ @support .requires_subprocess ()
237+ class TestTool (TestMain ):
238+ module = 'json.tool'
0 commit comments