@@ -110,9 +110,10 @@ def test_errprint(self):
110110
111111 for args , expected in tests :
112112 with self .subTest (arguments = args , expected = expected ):
113- with captured_stderr () as stderr :
114- tabnanny .errprint (* args )
115- self .assertEqual (stderr .getvalue () , expected )
113+ with self .assertRaises (SystemExit ):
114+ with captured_stderr () as stderr :
115+ tabnanny .errprint (* args )
116+ self .assertEqual (stderr .getvalue () , expected )
116117
117118
118119class TestNannyNag (TestCase ):
@@ -203,14 +204,16 @@ def test_when_wrong_indented(self):
203204 err = ('unindent does not match any outer indentation level'
204205 ' (<tokenize>, line 3)\n ' )
205206 err = f"{ file_path !r} : Indentation Error: { err } "
206- self .verify_tabnanny_check (file_path , err = err )
207+ with self .assertRaises (SystemExit ):
208+ self .verify_tabnanny_check (file_path , err = err )
207209
208210 def test_when_tokenize_tokenerror (self ):
209211 """A python source code file eligible for raising 'tokenize.TokenError'."""
210212 with TemporaryPyFile (SOURCE_CODES ["incomplete_expression" ]) as file_path :
211213 err = "('EOF in multi-line statement', (7, 0))\n "
212214 err = f"{ file_path !r} : Token Error: { err } "
213- self .verify_tabnanny_check (file_path , err = err )
215+ with self .assertRaises (SystemExit ):
216+ self .verify_tabnanny_check (file_path , err = err )
214217
215218 def test_when_nannynag_error_verbose (self ):
216219 """A python source code file eligible for raising `tabnanny.NannyNag`.
@@ -236,7 +239,8 @@ def test_when_no_file(self):
236239 path = 'no_file.py'
237240 err = (f"{ path !r} : I/O Error: [Errno { errno .ENOENT } ] "
238241 f"{ os .strerror (errno .ENOENT )} : { path !r} \n " )
239- self .verify_tabnanny_check (path , err = err )
242+ with self .assertRaises (SystemExit ):
243+ self .verify_tabnanny_check (path , err = err )
240244
241245 def test_errored_directory (self ):
242246 """Directory containing wrongly indented python source code files."""
@@ -251,7 +255,8 @@ def test_errored_directory(self):
251255 err = ('unindent does not match any outer indentation level'
252256 ' (<tokenize>, line 3)\n ' )
253257 err = f"{ e_file !r} : Indentation Error: { err } "
254- self .verify_tabnanny_check (tmp_dir , err = err )
258+ with self .assertRaises (SystemExit ):
259+ self .verify_tabnanny_check (tmp_dir , err = err )
255260
256261
257262class TestProcessTokens (TestCase ):
@@ -287,9 +292,12 @@ def test_with_errored_codes_samples(self):
287292class TestCommandLine (TestCase ):
288293 """Tests command line interface of `tabnanny`."""
289294
290- def validate_cmd (self , * args , stdout = "" , stderr = "" , partial = False ):
295+ def validate_cmd (self , * args , stdout = "" , stderr = "" , partial = False , expect_failure = False ):
291296 """Common function to assert the behaviour of command line interface."""
292- _ , out , err = script_helper .assert_python_ok ('-m' , 'tabnanny' , * args )
297+ if expect_failure :
298+ _ , out , err = script_helper .assert_python_failure ('-m' , 'tabnanny' , * args )
299+ else :
300+ _ , out , err = script_helper .assert_python_ok ('-m' , 'tabnanny' , * args )
293301 # Note: The `splitlines()` will solve the problem of CRLF(\r) added
294302 # by OS Windows.
295303 out = os .fsdecode (out )
@@ -310,7 +318,7 @@ def test_with_errored_file(self):
310318 stderr = f"{ file_path !r} : Indentation Error: "
311319 stderr += ('unindent does not match any outer indentation level'
312320 ' (<tokenize>, line 3)' )
313- self .validate_cmd (file_path , stderr = stderr )
321+ self .validate_cmd (file_path , stderr = stderr , expect_failure = True )
314322
315323 def test_with_error_free_file (self ):
316324 """Should not display anything if python file is correctly indented."""
@@ -321,7 +329,7 @@ def test_command_usage(self):
321329 """Should display usage on no arguments."""
322330 path = findfile ('tabnanny.py' )
323331 stderr = f"Usage: { path } [-v] file_or_directory ..."
324- self .validate_cmd (stderr = stderr )
332+ self .validate_cmd (stderr = stderr , expect_failure = True )
325333
326334 def test_quiet_flag (self ):
327335 """Should display less when quite mode is on."""
0 commit comments