diff options
-rw-r--r-- | pygments/cmdline.py | 22 | ||||
-rw-r--r-- | pygments/formatters/__init__.py | 4 | ||||
-rw-r--r-- | tests/test_cmdline.py | 50 |
3 files changed, 66 insertions, 10 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py index b6c319cf..c3f00fca 100644 --- a/pygments/cmdline.py +++ b/pygments/cmdline.py @@ -331,10 +331,13 @@ def main(args=sys.argv): opts.pop('-F', None) # select lexer - lexer = opts.pop('-l', None) - if lexer: + lexer = None + + # given by name? + lexername = opts.pop('-l', None) + if lexername: try: - lexer = get_lexer_by_name(lexer, **parsed_opts) + lexer = get_lexer_by_name(lexername, **parsed_opts) except (OptionError, ClassNotFound) as err: print('Error:', err, file=sys.stderr) return 1 @@ -461,12 +464,16 @@ def main(args=sys.argv): right = escapeinside[1] lexer = LatexEmbeddedLexer(left, right, lexer) - # ... and do it! - try: - # process filters - for fname, fopts in F_opts: + # process filters + for fname, fopts in F_opts: + try: lexer.add_filter(fname, **fopts) + except ClassNotFound as err: + print('Error:', err, file=sys.stderr) + return 1 + # ... and do it! + try: if '-s' not in opts: # process whole input as per normal... highlight(code, lexer, fmter, outfile) @@ -494,7 +501,6 @@ def main(args=sys.argv): return 0 except Exception: - raise import traceback info = traceback.format_exception(*sys.exc_info()) msg = info[-1].strip() diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py index 45ceaff6..22eb64c7 100644 --- a/pygments/formatters/__init__.py +++ b/pygments/formatters/__init__.py @@ -75,7 +75,7 @@ def get_formatter_by_name(_alias, **options): """ cls = find_formatter_class(_alias) if cls is None: - raise ClassNotFound("No formatter found for name %r" % _alias) + raise ClassNotFound("no formatter found for name %r" % _alias) return cls(**options) @@ -95,7 +95,7 @@ def get_formatter_for_filename(fn, **options): for filename in cls.filenames: if _fn_matches(fn, filename): return cls(**options) - raise ClassNotFound("No formatter found for file name %r" % fn) + raise ClassNotFound("no formatter found for file name %r" % fn) class _automodule(types.ModuleType): diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 038b96bf..d02d088a 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -47,6 +47,19 @@ def run_cmdline(*args): return (ret, out, err) +def run_cmdline_with_closed_stdout(*args): + saved_stdout, saved_stderr = sys.stdout, sys.stderr + sys.stdout, sys.stderr = StringIO(), StringIO() + sys.stdout.buffer = sys.stdout + sys.stdout.close() + try: + ret = cmdline_main(['pygmentize'] + list(args)) + err = sys.stderr.getvalue() + finally: + sys.stdout, sys.stderr = saved_stdout, saved_stderr + return ret, err + + class CmdLineTest(unittest.TestCase): def test_L_opt(self): @@ -125,3 +138,40 @@ class CmdLineTest(unittest.TestCase): self.assertEqual(run_cmdline(*opts)[0], 0) finally: os.unlink(name) + + def check_failure(self, *cmdline): + c, o, e = run_cmdline(*cmdline) + self.assertEqual(c, 1) + self.assertEqual(o, '') + return e + + def test_errors(self): + # input file not found + e = self.check_failure('-lpython', 'nonexistent.py') + self.assertIn('Error: cannot read infile', e) + self.assertIn('nonexistent.py', e) + + # lexer not found + e = self.check_failure('-lfooo', TESTFILE) + self.assertIn('Error: no lexer for alias', e) + + # formatter not found + e = self.check_failure('-lpython', '-ffoo', TESTFILE) + self.assertIn('Error: no formatter found for name', e) + + # output file not writable + e = self.check_failure('-o', os.path.join('nonexistent', 'dir', 'out.html'), + '-lpython', TESTFILE) + self.assertIn('Error: cannot open outfile', e) + self.assertIn('out.html', e) + + # unknown filter + e = self.check_failure('-F', 'foo', TESTFILE) + self.assertIn('Error: filter \'foo\' not found', e) + + def test_exception(self): + # unexpected exception while highlighting + # (we can force that by closing stdout) + c, e = run_cmdline_with_closed_stdout('-lpython', TESTFILE) + self.assertEqual(c, 1) + self.assertIn('*** Error while highlighting:', e) |