Skip to content

Commit 8176334

Browse files
gh-63301: Set exit code when tabnanny CLI exits on error (#7699)
Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 77a3196 commit 8176334

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

Lib/tabnanny.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def errprint(*args):
3535
sys.stderr.write(sep + str(arg))
3636
sep = " "
3737
sys.stderr.write("\n")
38+
sys.exit(1)
3839

3940
def main():
4041
import getopt
@@ -44,15 +45,13 @@ def main():
4445
opts, args = getopt.getopt(sys.argv[1:], "qv")
4546
except getopt.error as msg:
4647
errprint(msg)
47-
return
4848
for o, a in opts:
4949
if o == '-q':
5050
filename_only = filename_only + 1
5151
if o == '-v':
5252
verbose = verbose + 1
5353
if not args:
5454
errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...")
55-
return
5655
for arg in args:
5756
check(arg)
5857

Lib/test/test_tabnanny.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

118119
class 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

257262
class TestProcessTokens(TestCase):
@@ -287,9 +292,12 @@ def test_with_errored_codes_samples(self):
287292
class 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."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set exit code when :mod:`tabnanny` CLI exits on error.

0 commit comments

Comments
 (0)