diff --git a/Lib/tabnanny.py b/Lib/tabnanny.py --- a/Lib/tabnanny.py +++ b/Lib/tabnanny.py @@ -19,30 +19,29 @@ releases; such changes may not be backwa # XXX script-like. This will be addressed later. __version__ = "6" import os import sys import getopt import tokenize -if not hasattr(tokenize, 'NL'): - raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") __all__ = ["check", "NannyNag", "process_tokens"] verbose = 0 filename_only = 0 def errprint(*args): sep = "" for arg in args: sys.stderr.write(sep + str(arg)) sep = " " sys.stderr.write("\n") + sys.exit(1) def main(): global verbose, filename_only try: opts, args = getopt.getopt(sys.argv[1:], "qv") except getopt.error as msg: errprint(msg) return diff --git a/Lib/test/bad_indentation_1.py b/Lib/test/bad_indentation_1.py new file mode 100644 --- /dev/null +++ b/Lib/test/bad_indentation_1.py @@ -0,0 +1,3 @@ +def spam(eggs): + if eggs == 42: + return True diff --git a/Lib/test/test_tabnanny.py b/Lib/test/test_tabnanny.py new file mode 100644 --- /dev/null +++ b/Lib/test/test_tabnanny.py @@ -0,0 +1,47 @@ +import unittest + +from test import support, script_helper + + +class CommandLineTest(unittest.TestCase): + + def tabnannycmd(self, *args, expected_success=True): + if expected_success: + run_cmd = script_helper.assert_python_ok + else: + run_cmd = script_helper.assert_python_failure + rc, out, err = run_cmd('-m', 'tabnanny', *args) + return out if not rc else err + + def test_mix_of_tabs_and_spaces(self): + badfile = support.findfile('bad_indentation_1.py') + out = self.tabnannycmd(badfile) + self.assertIn(b"3 '\\t return True\\n'\n", out) + + def test_mix_of_tabs_and_spaces_verbose(self): + badfile = support.findfile('bad_indentation_1.py') + out = self.tabnannycmd('-v', badfile) + self.assertIn(b"*** Line 3: trouble in tab city! ***\noffending line: '\\t return True\\n'", out) + # more verbose + out = self.tabnannycmd('-vv', badfile) + self.assertTrue(out.startswith(b"checking")) + self.assertIn(b"*** Line 3: trouble in tab city! ***\noffending line: '\\t return True\\n'", out) + + def test_quiet_flag(self): + badfile = support.findfile('bad_indentation_1.py') + out = self.tabnannycmd('-q', badfile) + self.assertEqual(badfile.encode('ascii') + b'\n', out) + + def test_success(self): + goodfile = support.findfile('gdb_sample.py') + out = self.tabnannycmd(goodfile) + self.assertEqual(b'', out) + + def test_usage(self): + libtabnanny = support.findfile('tabnanny.py') + out = self.tabnannycmd(expected_success=False) + expected = b'Usage: ' + libtabnanny.encode('ascii') + b' [-v] file_or_directory ...' + self.assertEqual(expected, out) + +if __name__ == '__main__': + unittest.main()