diff options
-rw-r--r-- | pygments/cmdline.py | 9 | ||||
-rw-r--r-- | tests/test_cmdline.py | 29 |
2 files changed, 27 insertions, 11 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py index 608bd859..9d6742a3 100644 --- a/pygments/cmdline.py +++ b/pygments/cmdline.py @@ -301,12 +301,7 @@ def main_inner(popts, args, usage): print(err, file=sys.stderr) return 1 - arg = a_opt or '' - try: - print(fmter.get_style_defs(arg)) - except Exception as err: - print('Error:', err, file=sys.stderr) - return 1 + print(fmter.get_style_defs(a_opt or '')) return 0 # if no -S is given, -a is not allowed @@ -341,7 +336,7 @@ def main_inner(popts, args, usage): if '-s' in opts: print('Error: -s option not usable when input file specified', file=sys.stderr) - return 1 + return 2 infn = args[0] try: diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index f4c866a6..bbb6c88c 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -115,6 +115,10 @@ class CmdLineTest(unittest.TestCase): o = re.sub(r'\x1b\[.*?m', '', o) self.assertEqual(o.replace('\r\n', '\n'), TESTCODE) + def test_h_opt(self): + o = self.check_success("-h") + self.assertTrue('Usage:' in o) + def test_L_opt(self): o = self.check_success("-L") self.assertTrue("Lexers" in o and "Formatters" in o and @@ -160,7 +164,7 @@ class CmdLineTest(unittest.TestCase): self.assertTrue('not found' in e) def test_S_opt(self): - o = self.check_success("-S", "default", "-f", "html", "-O", "linenos=1") + o = self.check_success('-S', 'default', '-f', 'html', '-O', 'linenos=1') lines = o.splitlines() for line in lines: # every line is for a token class @@ -171,6 +175,7 @@ class CmdLineTest(unittest.TestCase): self.assertTrue(parts[-4] == '}') self.assertTrue(parts[-3] == '/*') self.assertTrue(parts[-1] == '*/') + self.check_failure('-S', 'default', '-f', 'foobar') def test_N_opt(self): o = self.check_success("-N", "test.py") @@ -179,9 +184,21 @@ class CmdLineTest(unittest.TestCase): self.assertEqual('text', o.strip()) def test_invalid_opts(self): - for opts in [("-L", "-lpy"), ("-L", "-fhtml"), ("-L", "-Ox"), - ("-a", "arg"), ("-Sst", "-lpy"), ("-H",), - ("-H", "formatter"), ("-H", "foo", "bar"), ("-s",)]: + for opts in [ + ('-X',), + ('-L', '-lpy'), + ('-L', '-fhtml'), + ('-L', '-Ox'), + ('-S', 'default', '-l', 'py', '-f', 'html'), + ('-S', 'default'), + ('-a', 'arg'), + ('-H',), + (TESTFILE, TESTFILE), + ('-H', 'formatter'), + ('-H', 'foo', 'bar'), + ('-s',), + ('-s', TESTFILE), + ]: self.check_failure(*opts, code=2) def test_errors(self): @@ -198,6 +215,10 @@ class CmdLineTest(unittest.TestCase): e = self.check_failure('-lpython', '-ffoo', TESTFILE) self.assertTrue('Error: no formatter found for name' in e) + # formatter for outfile not found + e = self.check_failure('-ofoo.foo', TESTFILE) + self.assertTrue('Error: no formatter found for file name' in e) + # output file not writable e = self.check_failure('-o', os.path.join('nonexistent', 'dir', 'out.html'), '-lpython', TESTFILE) |