6
6
import subprocess
7
7
8
8
from test import support
9
- from test .support import os_helper
9
+ from test .support import force_not_colorized , os_helper
10
10
from test .support .script_helper import assert_python_ok
11
11
12
12
@@ -87,6 +87,7 @@ class TestMain(unittest.TestCase):
87
87
}
88
88
""" )
89
89
90
+ @force_not_colorized
90
91
def test_stdin_stdout (self ):
91
92
args = sys .executable , '-m' , self .module
92
93
process = subprocess .run (args , input = self .data , capture_output = True , text = True , check = True )
@@ -102,7 +103,8 @@ def _create_infile(self, data=None):
102
103
103
104
def test_infile_stdout (self ):
104
105
infile = self ._create_infile ()
105
- rc , out , err = assert_python_ok ('-m' , self .module , infile )
106
+ rc , out , err = assert_python_ok ('-m' , self .module , infile ,
107
+ PYTHON_COLORS = '0' )
106
108
self .assertEqual (rc , 0 )
107
109
self .assertEqual (out .splitlines (), self .expect .encode ().splitlines ())
108
110
self .assertEqual (err , b'' )
@@ -116,7 +118,8 @@ def test_non_ascii_infile(self):
116
118
''' ).encode ()
117
119
118
120
infile = self ._create_infile (data )
119
- rc , out , err = assert_python_ok ('-m' , self .module , infile )
121
+ rc , out , err = assert_python_ok ('-m' , self .module , infile ,
122
+ PYTHON_COLORS = '0' )
120
123
121
124
self .assertEqual (rc , 0 )
122
125
self .assertEqual (out .splitlines (), expect .splitlines ())
@@ -125,7 +128,8 @@ def test_non_ascii_infile(self):
125
128
def test_infile_outfile (self ):
126
129
infile = self ._create_infile ()
127
130
outfile = os_helper .TESTFN + '.out'
128
- rc , out , err = assert_python_ok ('-m' , self .module , infile , outfile )
131
+ rc , out , err = assert_python_ok ('-m' , self .module , infile , outfile ,
132
+ PYTHON_COLORS = '0' )
129
133
self .addCleanup (os .remove , outfile )
130
134
with open (outfile , "r" , encoding = "utf-8" ) as fp :
131
135
self .assertEqual (fp .read (), self .expect )
@@ -135,33 +139,38 @@ def test_infile_outfile(self):
135
139
136
140
def test_writing_in_place (self ):
137
141
infile = self ._create_infile ()
138
- rc , out , err = assert_python_ok ('-m' , self .module , infile , infile )
142
+ rc , out , err = assert_python_ok ('-m' , self .module , infile , infile ,
143
+ PYTHON_COLORS = '0' )
139
144
with open (infile , "r" , encoding = "utf-8" ) as fp :
140
145
self .assertEqual (fp .read (), self .expect )
141
146
self .assertEqual (rc , 0 )
142
147
self .assertEqual (out , b'' )
143
148
self .assertEqual (err , b'' )
144
149
150
+ @force_not_colorized
145
151
def test_jsonlines (self ):
146
152
args = sys .executable , '-m' , self .module , '--json-lines'
147
153
process = subprocess .run (args , input = self .jsonlines_raw , capture_output = True , text = True , check = True )
148
154
self .assertEqual (process .stdout , self .jsonlines_expect )
149
155
self .assertEqual (process .stderr , '' )
150
156
151
157
def test_help_flag (self ):
152
- rc , out , err = assert_python_ok ('-m' , self .module , '-h' )
158
+ rc , out , err = assert_python_ok ('-m' , self .module , '-h' ,
159
+ PYTHON_COLORS = '0' )
153
160
self .assertEqual (rc , 0 )
154
161
self .assertTrue (out .startswith (b'usage: ' ))
155
162
self .assertEqual (err , b'' )
156
163
157
164
def test_sort_keys_flag (self ):
158
165
infile = self ._create_infile ()
159
- rc , out , err = assert_python_ok ('-m' , self .module , '--sort-keys' , infile )
166
+ rc , out , err = assert_python_ok ('-m' , self .module , '--sort-keys' , infile ,
167
+ PYTHON_COLORS = '0' )
160
168
self .assertEqual (rc , 0 )
161
169
self .assertEqual (out .splitlines (),
162
170
self .expect_without_sort_keys .encode ().splitlines ())
163
171
self .assertEqual (err , b'' )
164
172
173
+ @force_not_colorized
165
174
def test_indent (self ):
166
175
input_ = '[1, 2]'
167
176
expect = textwrap .dedent ('''\
@@ -175,6 +184,7 @@ def test_indent(self):
175
184
self .assertEqual (process .stdout , expect )
176
185
self .assertEqual (process .stderr , '' )
177
186
187
+ @force_not_colorized
178
188
def test_no_indent (self ):
179
189
input_ = '[1,\n 2]'
180
190
expect = '[1, 2]\n '
@@ -183,6 +193,7 @@ def test_no_indent(self):
183
193
self .assertEqual (process .stdout , expect )
184
194
self .assertEqual (process .stderr , '' )
185
195
196
+ @force_not_colorized
186
197
def test_tab (self ):
187
198
input_ = '[1, 2]'
188
199
expect = '[\n \t 1,\n \t 2\n ]\n '
@@ -191,6 +202,7 @@ def test_tab(self):
191
202
self .assertEqual (process .stdout , expect )
192
203
self .assertEqual (process .stderr , '' )
193
204
205
+ @force_not_colorized
194
206
def test_compact (self ):
195
207
input_ = '[ 1 ,\n 2]'
196
208
expect = '[1,2]\n '
@@ -203,7 +215,8 @@ def test_no_ensure_ascii_flag(self):
203
215
infile = self ._create_infile ('{"key":"💩"}' )
204
216
outfile = os_helper .TESTFN + '.out'
205
217
self .addCleanup (os .remove , outfile )
206
- assert_python_ok ('-m' , self .module , '--no-ensure-ascii' , infile , outfile )
218
+ assert_python_ok ('-m' , self .module , '--no-ensure-ascii' , infile ,
219
+ outfile , PYTHON_COLORS = '0' )
207
220
with open (outfile , "rb" ) as f :
208
221
lines = f .read ().splitlines ()
209
222
# asserting utf-8 encoded output file
@@ -214,13 +227,14 @@ def test_ensure_ascii_default(self):
214
227
infile = self ._create_infile ('{"key":"💩"}' )
215
228
outfile = os_helper .TESTFN + '.out'
216
229
self .addCleanup (os .remove , outfile )
217
- assert_python_ok ('-m' , self .module , infile , outfile )
230
+ assert_python_ok ('-m' , self .module , infile , outfile , PYTHON_COLORS = '0' )
218
231
with open (outfile , "rb" ) as f :
219
232
lines = f .read ().splitlines ()
220
233
# asserting an ascii encoded output file
221
234
expected = [b'{' , rb' "key": "\ud83d\udca9"' , b"}" ]
222
235
self .assertEqual (lines , expected )
223
236
237
+ @force_not_colorized
224
238
@unittest .skipIf (sys .platform == "win32" , "The test is failed with ValueError on Windows" )
225
239
def test_broken_pipe_error (self ):
226
240
cmd = [sys .executable , '-m' , self .module ]
@@ -232,7 +246,73 @@ def test_broken_pipe_error(self):
232
246
proc .communicate (b'"{}"' )
233
247
self .assertEqual (proc .returncode , errno .EPIPE )
234
248
249
+ def test_colors (self ):
250
+ infile = os_helper .TESTFN
251
+ self .addCleanup (os .remove , infile )
252
+
253
+ cases = (
254
+ ('{}' , b'{}' ),
255
+ ('[]' , b'[]' ),
256
+ ('null' , b'\x1b [1;36mnull\x1b [0m' ),
257
+ ('true' , b'\x1b [1;36mtrue\x1b [0m' ),
258
+ ('false' , b'\x1b [1;36mfalse\x1b [0m' ),
259
+ ('NaN' , b'NaN' ),
260
+ ('Infinity' , b'Infinity' ),
261
+ ('-Infinity' , b'-Infinity' ),
262
+ ('"foo"' , b'\x1b [1;32m"foo"\x1b [0m' ),
263
+ (r'" \"foo\" "' , b'\x1b [1;32m" \\ "foo\\ " "\x1b [0m' ),
264
+ ('"α"' , b'\x1b [1;32m"\\ u03b1"\x1b [0m' ),
265
+ ('123' , b'123' ),
266
+ ('-1.2345e+23' , b'-1.2345e+23' ),
267
+ (r'{"\\": ""}' ,
268
+ b'''\
269
+ {
270
+ \x1b [94m"\\ \\ "\x1b [0m: \x1b [1;32m""\x1b [0m
271
+ }''' ),
272
+ (r'{"\\\\": ""}' ,
273
+ b'''\
274
+ {
275
+ \x1b [94m"\\ \\ \\ \\ "\x1b [0m: \x1b [1;32m""\x1b [0m
276
+ }''' ),
277
+ ('''\
278
+ {
279
+ "foo": "bar",
280
+ "baz": 1234,
281
+ "qux": [true, false, null],
282
+ "xyz": [NaN, -Infinity, Infinity]
283
+ }''' ,
284
+ b'''\
285
+ {
286
+ \x1b [94m"foo"\x1b [0m: \x1b [1;32m"bar"\x1b [0m,
287
+ \x1b [94m"baz"\x1b [0m: 1234,
288
+ \x1b [94m"qux"\x1b [0m: [
289
+ \x1b [1;36mtrue\x1b [0m,
290
+ \x1b [1;36mfalse\x1b [0m,
291
+ \x1b [1;36mnull\x1b [0m
292
+ ],
293
+ \x1b [94m"xyz"\x1b [0m: [
294
+ NaN,
295
+ -Infinity,
296
+ Infinity
297
+ ]
298
+ }''' ),
299
+ )
300
+
301
+ for input_ , expected in cases :
302
+ with self .subTest (input = input_ ):
303
+ with open (infile , "w" , encoding = "utf-8" ) as fp :
304
+ fp .write (input_ )
305
+ _ , stdout , _ = assert_python_ok ('-m' , self .module , infile ,
306
+ PYTHON_COLORS = '1' )
307
+ stdout = stdout .replace (b'\r \n ' , b'\n ' ) # normalize line endings
308
+ stdout = stdout .strip ()
309
+ self .assertEqual (stdout , expected )
310
+
235
311
236
312
@support .requires_subprocess ()
237
313
class TestTool (TestMain ):
238
314
module = 'json.tool'
315
+
316
+
317
+ if __name__ == "__main__" :
318
+ unittest .main ()
0 commit comments