11
11
12
12
13
13
@support .requires_subprocess ()
14
- class TestTool (unittest .TestCase ):
14
+ class TestMain (unittest .TestCase ):
15
15
data = """
16
16
17
17
[["blorpie"],[ "whoops" ] , [
18
18
],\t "d-shtaeou",\r "d-nthiouh",
19
19
"i-vhbjkhnth", {"nifty":87}, {"morefield" :\t false,"field"
20
20
:"yes"} ]
21
21
"""
22
+ module = 'json'
22
23
23
24
expect_without_sort_keys = textwrap .dedent ("""\
24
25
[
@@ -87,7 +88,7 @@ class TestTool(unittest.TestCase):
87
88
""" )
88
89
89
90
def test_stdin_stdout (self ):
90
- args = sys .executable , '-m' , 'json.tool'
91
+ args = sys .executable , '-m' , self . module
91
92
process = subprocess .run (args , input = self .data , capture_output = True , text = True , check = True )
92
93
self .assertEqual (process .stdout , self .expect )
93
94
self .assertEqual (process .stderr , '' )
@@ -101,7 +102,7 @@ def _create_infile(self, data=None):
101
102
102
103
def test_infile_stdout (self ):
103
104
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 )
105
106
self .assertEqual (rc , 0 )
106
107
self .assertEqual (out .splitlines (), self .expect .encode ().splitlines ())
107
108
self .assertEqual (err , b'' )
@@ -115,7 +116,7 @@ def test_non_ascii_infile(self):
115
116
''' ).encode ()
116
117
117
118
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 )
119
120
120
121
self .assertEqual (rc , 0 )
121
122
self .assertEqual (out .splitlines (), expect .splitlines ())
@@ -124,7 +125,7 @@ def test_non_ascii_infile(self):
124
125
def test_infile_outfile (self ):
125
126
infile = self ._create_infile ()
126
127
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 )
128
129
self .addCleanup (os .remove , outfile )
129
130
with open (outfile , "r" , encoding = "utf-8" ) as fp :
130
131
self .assertEqual (fp .read (), self .expect )
@@ -134,28 +135,28 @@ def test_infile_outfile(self):
134
135
135
136
def test_writing_in_place (self ):
136
137
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 )
138
139
with open (infile , "r" , encoding = "utf-8" ) as fp :
139
140
self .assertEqual (fp .read (), self .expect )
140
141
self .assertEqual (rc , 0 )
141
142
self .assertEqual (out , b'' )
142
143
self .assertEqual (err , b'' )
143
144
144
145
def test_jsonlines (self ):
145
- args = sys .executable , '-m' , 'json.tool' , '--json-lines'
146
+ args = sys .executable , '-m' , self . module , '--json-lines'
146
147
process = subprocess .run (args , input = self .jsonlines_raw , capture_output = True , text = True , check = True )
147
148
self .assertEqual (process .stdout , self .jsonlines_expect )
148
149
self .assertEqual (process .stderr , '' )
149
150
150
151
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' )
152
153
self .assertEqual (rc , 0 )
153
154
self .assertTrue (out .startswith (b'usage: ' ))
154
155
self .assertEqual (err , b'' )
155
156
156
157
def test_sort_keys_flag (self ):
157
158
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 )
159
160
self .assertEqual (rc , 0 )
160
161
self .assertEqual (out .splitlines (),
161
162
self .expect_without_sort_keys .encode ().splitlines ())
@@ -169,31 +170,31 @@ def test_indent(self):
169
170
2
170
171
]
171
172
''' )
172
- args = sys .executable , '-m' , 'json.tool' , '--indent' , '2'
173
+ args = sys .executable , '-m' , self . module , '--indent' , '2'
173
174
process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
174
175
self .assertEqual (process .stdout , expect )
175
176
self .assertEqual (process .stderr , '' )
176
177
177
178
def test_no_indent (self ):
178
179
input_ = '[1,\n 2]'
179
180
expect = '[1, 2]\n '
180
- args = sys .executable , '-m' , 'json.tool' , '--no-indent'
181
+ args = sys .executable , '-m' , self . module , '--no-indent'
181
182
process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
182
183
self .assertEqual (process .stdout , expect )
183
184
self .assertEqual (process .stderr , '' )
184
185
185
186
def test_tab (self ):
186
187
input_ = '[1, 2]'
187
188
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'
189
190
process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
190
191
self .assertEqual (process .stdout , expect )
191
192
self .assertEqual (process .stderr , '' )
192
193
193
194
def test_compact (self ):
194
195
input_ = '[ 1 ,\n 2]'
195
196
expect = '[1,2]\n '
196
- args = sys .executable , '-m' , 'json.tool' , '--compact'
197
+ args = sys .executable , '-m' , self . module , '--compact'
197
198
process = subprocess .run (args , input = input_ , capture_output = True , text = True , check = True )
198
199
self .assertEqual (process .stdout , expect )
199
200
self .assertEqual (process .stderr , '' )
@@ -202,7 +203,7 @@ def test_no_ensure_ascii_flag(self):
202
203
infile = self ._create_infile ('{"key":"💩"}' )
203
204
outfile = os_helper .TESTFN + '.out'
204
205
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 )
206
207
with open (outfile , "rb" ) as f :
207
208
lines = f .read ().splitlines ()
208
209
# asserting utf-8 encoded output file
@@ -213,7 +214,7 @@ def test_ensure_ascii_default(self):
213
214
infile = self ._create_infile ('{"key":"💩"}' )
214
215
outfile = os_helper .TESTFN + '.out'
215
216
self .addCleanup (os .remove , outfile )
216
- assert_python_ok ('-m' , 'json.tool' , infile , outfile )
217
+ assert_python_ok ('-m' , self . module , infile , outfile )
217
218
with open (outfile , "rb" ) as f :
218
219
lines = f .read ().splitlines ()
219
220
# asserting an ascii encoded output file
@@ -222,11 +223,16 @@ def test_ensure_ascii_default(self):
222
223
223
224
@unittest .skipIf (sys .platform == "win32" , "The test is failed with ValueError on Windows" )
224
225
def test_broken_pipe_error (self ):
225
- cmd = [sys .executable , '-m' , 'json.tool' ]
226
+ cmd = [sys .executable , '-m' , self . module ]
226
227
proc = subprocess .Popen (cmd ,
227
228
stdout = subprocess .PIPE ,
228
229
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.
230
231
proc .stdout .close ()
231
232
proc .communicate (b'"{}"' )
232
233
self .assertEqual (proc .returncode , errno .EPIPE )
234
+
235
+
236
+ @support .requires_subprocess ()
237
+ class TestTool (TestMain ):
238
+ module = 'json.tool'
0 commit comments