14
14
findfile )
15
15
from test .support .os_helper import unlink
16
16
17
- import unittest # XXX : RUSTPYTHON
17
+ import unittest # TODO : RUSTPYTHON
18
18
19
19
20
20
SOURCE_CODES = {
@@ -112,9 +112,10 @@ def test_errprint(self):
112
112
113
113
for args , expected in tests :
114
114
with self .subTest (arguments = args , expected = expected ):
115
- with captured_stderr () as stderr :
116
- tabnanny .errprint (* args )
117
- self .assertEqual (stderr .getvalue () , expected )
115
+ with self .assertRaises (SystemExit ):
116
+ with captured_stderr () as stderr :
117
+ tabnanny .errprint (* args )
118
+ self .assertEqual (stderr .getvalue () , expected )
118
119
119
120
120
121
class TestNannyNag (TestCase ):
@@ -199,53 +200,54 @@ def test_correct_directory(self):
199
200
with TemporaryPyFile (SOURCE_CODES ["error_free" ], directory = tmp_dir ):
200
201
self .verify_tabnanny_check (tmp_dir )
201
202
202
- # TODO: RUSTPYTHON
203
- @unittest .expectedFailure
204
203
def test_when_wrong_indented (self ):
205
204
"""A python source code file eligible for raising `IndentationError`."""
206
205
with TemporaryPyFile (SOURCE_CODES ["wrong_indented" ]) as file_path :
207
206
err = ('unindent does not match any outer indentation level'
208
207
' (<tokenize>, line 3)\n ' )
209
208
err = f"{ file_path !r} : Indentation Error: { err } "
210
- self .verify_tabnanny_check (file_path , err = err )
209
+ with self .assertRaises (SystemExit ):
210
+ self .verify_tabnanny_check (file_path , err = err )
211
211
212
212
def test_when_tokenize_tokenerror (self ):
213
213
"""A python source code file eligible for raising 'tokenize.TokenError'."""
214
214
with TemporaryPyFile (SOURCE_CODES ["incomplete_expression" ]) as file_path :
215
215
err = "('EOF in multi-line statement', (7, 0))\n "
216
216
err = f"{ file_path !r} : Token Error: { err } "
217
- self .verify_tabnanny_check (file_path , err = err )
217
+ with self .assertRaises (SystemExit ):
218
+ self .verify_tabnanny_check (file_path , err = err )
218
219
220
+ # TODO: RUSTPYTHON
221
+ @unittest .expectedFailure
219
222
def test_when_nannynag_error_verbose (self ):
220
223
"""A python source code file eligible for raising `tabnanny.NannyNag`.
221
224
222
225
Tests will assert `stdout` after activating `tabnanny.verbose` mode.
223
226
"""
224
227
with TemporaryPyFile (SOURCE_CODES ["nannynag_errored" ]) as file_path :
225
228
out = f"{ file_path !r} : *** Line 3: trouble in tab city! ***\n "
226
- out += "offending line: '\\ tprint(\" world\" )\\ n '\n "
227
- out += "indent not equal e.g. at tab size 1 \n "
229
+ out += "offending line: '\\ tprint(\" world\" )'\n "
230
+ out += "inconsistent use of tabs and spaces in indentation \n "
228
231
229
232
tabnanny .verbose = 1
230
233
self .verify_tabnanny_check (file_path , out = out )
231
234
235
+ # TODO: RUSTPYTHON
236
+ @unittest .expectedFailure
232
237
def test_when_nannynag_error (self ):
233
238
"""A python source code file eligible for raising `tabnanny.NannyNag`."""
234
239
with TemporaryPyFile (SOURCE_CODES ["nannynag_errored" ]) as file_path :
235
- out = f"{ file_path } 3 '\\ tprint(\" world\" )\\ n '\n "
240
+ out = f"{ file_path } 3 '\\ tprint(\" world\" )'\n "
236
241
self .verify_tabnanny_check (file_path , out = out )
237
242
238
- # TODO: RUSTPYTHON
239
- @unittest .expectedFailure
240
243
def test_when_no_file (self ):
241
244
"""A python file which does not exist actually in system."""
242
245
path = 'no_file.py'
243
246
err = (f"{ path !r} : I/O Error: [Errno { errno .ENOENT } ] "
244
247
f"{ os .strerror (errno .ENOENT )} : { path !r} \n " )
245
- self .verify_tabnanny_check (path , err = err )
248
+ with self .assertRaises (SystemExit ):
249
+ self .verify_tabnanny_check (path , err = err )
246
250
247
- # TODO: RUSTPYTHON
248
- @unittest .expectedFailure
249
251
def test_errored_directory (self ):
250
252
"""Directory containing wrongly indented python source code files."""
251
253
with tempfile .TemporaryDirectory () as tmp_dir :
@@ -259,7 +261,8 @@ def test_errored_directory(self):
259
261
err = ('unindent does not match any outer indentation level'
260
262
' (<tokenize>, line 3)\n ' )
261
263
err = f"{ e_file !r} : Indentation Error: { err } "
262
- self .verify_tabnanny_check (tmp_dir , err = err )
264
+ with self .assertRaises (SystemExit ):
265
+ self .verify_tabnanny_check (tmp_dir , err = err )
263
266
264
267
265
268
class TestProcessTokens (TestCase ):
@@ -295,9 +298,12 @@ def test_with_errored_codes_samples(self):
295
298
class TestCommandLine (TestCase ):
296
299
"""Tests command line interface of `tabnanny`."""
297
300
298
- def validate_cmd (self , * args , stdout = "" , stderr = "" , partial = False ):
301
+ def validate_cmd (self , * args , stdout = "" , stderr = "" , partial = False , expect_failure = False ):
299
302
"""Common function to assert the behaviour of command line interface."""
300
- _ , out , err = script_helper .assert_python_ok ('-m' , 'tabnanny' , * args )
303
+ if expect_failure :
304
+ _ , out , err = script_helper .assert_python_failure ('-m' , 'tabnanny' , * args )
305
+ else :
306
+ _ , out , err = script_helper .assert_python_ok ('-m' , 'tabnanny' , * args )
301
307
# Note: The `splitlines()` will solve the problem of CRLF(\r) added
302
308
# by OS Windows.
303
309
out = os .fsdecode (out )
@@ -319,8 +325,8 @@ def test_with_errored_file(self):
319
325
with TemporaryPyFile (SOURCE_CODES ["wrong_indented" ]) as file_path :
320
326
stderr = f"{ file_path !r} : Indentation Error: "
321
327
stderr += ('unindent does not match any outer indentation level'
322
- ' (<tokenize >, line 3)' )
323
- self .validate_cmd (file_path , stderr = stderr )
328
+ ' (<string >, line 3)' )
329
+ self .validate_cmd (file_path , stderr = stderr , expect_failure = True )
324
330
325
331
def test_with_error_free_file (self ):
326
332
"""Should not display anything if python file is correctly indented."""
@@ -331,26 +337,30 @@ def test_command_usage(self):
331
337
"""Should display usage on no arguments."""
332
338
path = findfile ('tabnanny.py' )
333
339
stderr = f"Usage: { path } [-v] file_or_directory ..."
334
- self .validate_cmd (stderr = stderr )
340
+ self .validate_cmd (stderr = stderr , expect_failure = True )
335
341
336
342
def test_quiet_flag (self ):
337
343
"""Should display less when quite mode is on."""
338
344
with TemporaryPyFile (SOURCE_CODES ["nannynag_errored" ]) as file_path :
339
345
stdout = f"{ file_path } \n "
340
346
self .validate_cmd ("-q" , file_path , stdout = stdout )
341
347
348
+ # TODO: RUSTPYTHON
349
+ @unittest .expectedFailure
342
350
def test_verbose_mode (self ):
343
351
"""Should display more error information if verbose mode is on."""
344
352
with TemporaryPyFile (SOURCE_CODES ["nannynag_errored" ]) as path :
345
353
stdout = textwrap .dedent (
346
- "offending line: '\\ tprint(\" world\" )\\ n '"
354
+ "offending line: '\\ tprint(\" world\" )'"
347
355
).strip ()
348
356
self .validate_cmd ("-v" , path , stdout = stdout , partial = True )
349
357
358
+ # TODO: RUSTPYTHON
359
+ @unittest .expectedFailure
350
360
def test_double_verbose_mode (self ):
351
361
"""Should display detailed error information if double verbose is on."""
352
362
with TemporaryPyFile (SOURCE_CODES ["nannynag_errored" ]) as path :
353
363
stdout = textwrap .dedent (
354
- "offending line: '\\ tprint(\" world\" )\\ n '"
364
+ "offending line: '\\ tprint(\" world\" )'"
355
365
).strip ()
356
366
self .validate_cmd ("-vv" , path , stdout = stdout , partial = True )
0 commit comments