summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaysinh Shukla <jaysinhp@gmail.com>2023-03-04 21:32:13 (GMT)
committerGitHub <noreply@github.com>2023-03-04 21:32:13 (GMT)
commit81763341ede99ea5ae8993a57b8e3b71b46b2d72 (patch)
tree420ee91b32e84342ee3bd76d6f229d004e46d919
parent77a3196b7cc17d90a8aae5629aa71ff183b9266a (diff)
downloadcpython-81763341ede99ea5ae8993a57b8e3b71b46b2d72.zip
cpython-81763341ede99ea5ae8993a57b8e3b71b46b2d72.tar.gz
cpython-81763341ede99ea5ae8993a57b8e3b71b46b2d72.tar.bz2
gh-63301: Set exit code when tabnanny CLI exits on error (#7699)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
-rwxr-xr-xLib/tabnanny.py3
-rw-r--r--Lib/test/test_tabnanny.py30
-rw-r--r--Misc/NEWS.d/next/Library/2023-02-01-10-42-16.gh-issue-63301.XNxSFh.rst1
3 files changed, 21 insertions, 13 deletions
diff --git a/Lib/tabnanny.py b/Lib/tabnanny.py
index a47f5a9..9d2df59 100755
--- a/Lib/tabnanny.py
+++ b/Lib/tabnanny.py
@@ -35,6 +35,7 @@ def errprint(*args):
sys.stderr.write(sep + str(arg))
sep = " "
sys.stderr.write("\n")
+ sys.exit(1)
def main():
import getopt
@@ -44,7 +45,6 @@ def main():
opts, args = getopt.getopt(sys.argv[1:], "qv")
except getopt.error as msg:
errprint(msg)
- return
for o, a in opts:
if o == '-q':
filename_only = filename_only + 1
@@ -52,7 +52,6 @@ def main():
verbose = verbose + 1
if not args:
errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...")
- return
for arg in args:
check(arg)
diff --git a/Lib/test/test_tabnanny.py b/Lib/test/test_tabnanny.py
index e0a82e9..afb8da7 100644
--- a/Lib/test/test_tabnanny.py
+++ b/Lib/test/test_tabnanny.py
@@ -110,9 +110,10 @@ class TestErrPrint(TestCase):
for args, expected in tests:
with self.subTest(arguments=args, expected=expected):
- with captured_stderr() as stderr:
- tabnanny.errprint(*args)
- self.assertEqual(stderr.getvalue() , expected)
+ with self.assertRaises(SystemExit):
+ with captured_stderr() as stderr:
+ tabnanny.errprint(*args)
+ self.assertEqual(stderr.getvalue() , expected)
class TestNannyNag(TestCase):
@@ -203,14 +204,16 @@ class TestCheck(TestCase):
err = ('unindent does not match any outer indentation level'
' (<tokenize>, line 3)\n')
err = f"{file_path!r}: Indentation Error: {err}"
- self.verify_tabnanny_check(file_path, err=err)
+ with self.assertRaises(SystemExit):
+ self.verify_tabnanny_check(file_path, err=err)
def test_when_tokenize_tokenerror(self):
"""A python source code file eligible for raising 'tokenize.TokenError'."""
with TemporaryPyFile(SOURCE_CODES["incomplete_expression"]) as file_path:
err = "('EOF in multi-line statement', (7, 0))\n"
err = f"{file_path!r}: Token Error: {err}"
- self.verify_tabnanny_check(file_path, err=err)
+ with self.assertRaises(SystemExit):
+ self.verify_tabnanny_check(file_path, err=err)
def test_when_nannynag_error_verbose(self):
"""A python source code file eligible for raising `tabnanny.NannyNag`.
@@ -236,7 +239,8 @@ class TestCheck(TestCase):
path = 'no_file.py'
err = (f"{path!r}: I/O Error: [Errno {errno.ENOENT}] "
f"{os.strerror(errno.ENOENT)}: {path!r}\n")
- self.verify_tabnanny_check(path, err=err)
+ with self.assertRaises(SystemExit):
+ self.verify_tabnanny_check(path, err=err)
def test_errored_directory(self):
"""Directory containing wrongly indented python source code files."""
@@ -251,7 +255,8 @@ class TestCheck(TestCase):
err = ('unindent does not match any outer indentation level'
' (<tokenize>, line 3)\n')
err = f"{e_file!r}: Indentation Error: {err}"
- self.verify_tabnanny_check(tmp_dir, err=err)
+ with self.assertRaises(SystemExit):
+ self.verify_tabnanny_check(tmp_dir, err=err)
class TestProcessTokens(TestCase):
@@ -287,9 +292,12 @@ class TestProcessTokens(TestCase):
class TestCommandLine(TestCase):
"""Tests command line interface of `tabnanny`."""
- def validate_cmd(self, *args, stdout="", stderr="", partial=False):
+ def validate_cmd(self, *args, stdout="", stderr="", partial=False, expect_failure=False):
"""Common function to assert the behaviour of command line interface."""
- _, out, err = script_helper.assert_python_ok('-m', 'tabnanny', *args)
+ if expect_failure:
+ _, out, err = script_helper.assert_python_failure('-m', 'tabnanny', *args)
+ else:
+ _, out, err = script_helper.assert_python_ok('-m', 'tabnanny', *args)
# Note: The `splitlines()` will solve the problem of CRLF(\r) added
# by OS Windows.
out = os.fsdecode(out)
@@ -310,7 +318,7 @@ class TestCommandLine(TestCase):
stderr = f"{file_path!r}: Indentation Error: "
stderr += ('unindent does not match any outer indentation level'
' (<tokenize>, line 3)')
- self.validate_cmd(file_path, stderr=stderr)
+ self.validate_cmd(file_path, stderr=stderr, expect_failure=True)
def test_with_error_free_file(self):
"""Should not display anything if python file is correctly indented."""
@@ -321,7 +329,7 @@ class TestCommandLine(TestCase):
"""Should display usage on no arguments."""
path = findfile('tabnanny.py')
stderr = f"Usage: {path} [-v] file_or_directory ..."
- self.validate_cmd(stderr=stderr)
+ self.validate_cmd(stderr=stderr, expect_failure=True)
def test_quiet_flag(self):
"""Should display less when quite mode is on."""
diff --git a/Misc/NEWS.d/next/Library/2023-02-01-10-42-16.gh-issue-63301.XNxSFh.rst b/Misc/NEWS.d/next/Library/2023-02-01-10-42-16.gh-issue-63301.XNxSFh.rst
new file mode 100644
index 0000000..e00e71f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-02-01-10-42-16.gh-issue-63301.XNxSFh.rst
@@ -0,0 +1 @@
+Set exit code when :mod:`tabnanny` CLI exits on error.