gh-63301: Set exit code when tabnanny CLI exits on error (#7699) · python/cpython@8176334 (original) (raw)
`@@ -110,9 +110,10 @@ def test_errprint(self):
`
110
110
``
111
111
`for args, expected in tests:
`
112
112
`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)
`
116
117
``
117
118
``
118
119
`class TestNannyNag(TestCase):
`
`@@ -203,14 +204,16 @@ def test_when_wrong_indented(self):
`
203
204
`err = ('unindent does not match any outer indentation level'
`
204
205
`' (, line 3)\n')
`
205
206
`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)
`
207
209
``
208
210
`def test_when_tokenize_tokenerror(self):
`
209
211
`"""A python source code file eligible for raising 'tokenize.TokenError'."""
`
210
212
`with TemporaryPyFile(SOURCE_CODES["incomplete_expression"]) as file_path:
`
211
213
`err = "('EOF in multi-line statement', (7, 0))\n"
`
212
214
`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)
`
214
217
``
215
218
`def test_when_nannynag_error_verbose(self):
`
216
219
`` """A python source code file eligible for raising tabnanny.NannyNag
.
``
`@@ -236,7 +239,8 @@ def test_when_no_file(self):
`
236
239
`path = 'no_file.py'
`
237
240
`err = (f"{path!r}: I/O Error: [Errno {errno.ENOENT}] "
`
238
241
`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)
`
240
244
``
241
245
`def test_errored_directory(self):
`
242
246
`"""Directory containing wrongly indented python source code files."""
`
`@@ -251,7 +255,8 @@ def test_errored_directory(self):
`
251
255
`err = ('unindent does not match any outer indentation level'
`
252
256
`' (, line 3)\n')
`
253
257
`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)
`
255
260
``
256
261
``
257
262
`class TestProcessTokens(TestCase):
`
`@@ -287,9 +292,12 @@ def test_with_errored_codes_samples(self):
`
287
292
`class TestCommandLine(TestCase):
`
288
293
`` """Tests command line interface of tabnanny
."""
``
289
294
``
290
``
`-
def validate_cmd(self, *args, stdout="", stderr="", partial=False):
`
``
295
`+
def validate_cmd(self, *args, stdout="", stderr="", partial=False, expect_failure=False):
`
291
296
`"""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)
`
293
301
`` # Note: The splitlines()
will solve the problem of CRLF(\r) added
``
294
302
`# by OS Windows.
`
295
303
`out = os.fsdecode(out)
`
`@@ -310,7 +318,7 @@ def test_with_errored_file(self):
`
310
318
`stderr = f"{file_path!r}: Indentation Error: "
`
311
319
`stderr += ('unindent does not match any outer indentation level'
`
312
320
`' (, line 3)')
`
313
``
`-
self.validate_cmd(file_path, stderr=stderr)
`
``
321
`+
self.validate_cmd(file_path, stderr=stderr, expect_failure=True)
`
314
322
``
315
323
`def test_with_error_free_file(self):
`
316
324
`"""Should not display anything if python file is correctly indented."""
`
`@@ -321,7 +329,7 @@ def test_command_usage(self):
`
321
329
`"""Should display usage on no arguments."""
`
322
330
`path = findfile('tabnanny.py')
`
323
331
`stderr = f"Usage: {path} [-v] file_or_directory ..."
`
324
``
`-
self.validate_cmd(stderr=stderr)
`
``
332
`+
self.validate_cmd(stderr=stderr, expect_failure=True)
`
325
333
``
326
334
`def test_quiet_flag(self):
`
327
335
`"""Should display less when quite mode is on."""
`