Don't print backtraces for user/input errors
This change introduces a common YapfError parent class
that can be used for input/user error. run_main() then
handles these errors and prints a nice error message
rather than a nasty backtrace (backtraces IMHO should
only be printed for program errors).
Fixes #130
diff --git a/yapftests/main_test.py b/yapftests/main_test.py
index 314dcba..e216d67 100644
--- a/yapftests/main_test.py
+++ b/yapftests/main_test.py
@@ -57,10 +57,23 @@
yapf.py3compat.raw_input = raw_input
+class RunMainTest(unittest.TestCase):
+
+ def testShouldHandleYapfError(self):
+ """run_main should handle YapfError and sys.exit(1)"""
+ expected_message = 'yapf: Input filenames did not match any python files\n'
+ sys.argv = ['yapf', 'foo.c']
+ with captured_output() as (out, err):
+ with self.assertRaises(SystemExit):
+ ret = yapf.run_main()
+ self.assertEqual(out.getvalue(), '')
+ self.assertEqual(err.getvalue(), expected_message)
+
+
class MainTest(unittest.TestCase):
def testNoPythonFilesMatched(self):
- with self.assertRaisesRegexp(yapf.YapfError,
+ with self.assertRaisesRegexp(yapf.errors.YapfError,
'did not match any python files'):
yapf.main(['yapf', 'foo.c'])