summaryrefslogtreecommitdiff
path: root/tests/test_cmdline.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_cmdline.py')
-rw-r--r--tests/test_cmdline.py69
1 files changed, 65 insertions, 4 deletions
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index 5883fb5c..a55e30ec 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -3,7 +3,7 @@
Command line test
~~~~~~~~~~~~~~~~~
- :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
+ :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
@@ -28,6 +28,13 @@ def func(args):
'''
+def _decode_output(text):
+ try:
+ return text.decode('utf-8')
+ except UnicodeEncodeError: # implicit encode on Python 2 with data loss
+ return text
+
+
def run_cmdline(*args, **kwds):
saved_stdin = sys.stdin
saved_stdout = sys.stdout
@@ -53,9 +60,9 @@ def run_cmdline(*args, **kwds):
sys.stderr = saved_stderr
new_stdout.flush()
new_stderr.flush()
- out, err = stdout_buffer.getvalue().decode('utf-8'), \
- stderr_buffer.getvalue().decode('utf-8')
- return (ret, out, err)
+ out, err = stdout_buffer.getvalue(), \
+ stderr_buffer.getvalue()
+ return (ret, _decode_output(out), _decode_output(err))
class CmdLineTest(unittest.TestCase):
@@ -110,6 +117,32 @@ class CmdLineTest(unittest.TestCase):
finally:
os.unlink(name)
+ def test_load_from_file(self):
+ lexer_file = os.path.join(TESTDIR, 'support', 'python_lexer.py')
+ formatter_file = os.path.join(TESTDIR, 'support', 'html_formatter.py')
+
+ # By default, use CustomLexer
+ o = self.check_success('-l', lexer_file, '-f', 'html',
+ '-x', stdin=TESTCODE)
+ o = re.sub('<[^>]*>', '', o)
+ # rstrip is necessary since HTML inserts a \n after the last </div>
+ self.assertEqual(o.rstrip(), TESTCODE.rstrip())
+
+ # If user specifies a name, use it
+ o = self.check_success('-f', 'html', '-x', '-l',
+ lexer_file + ':LexerWrapper', stdin=TESTCODE)
+ o = re.sub('<[^>]*>', '', o)
+ # rstrip is necessary since HTML inserts a \n after the last </div>
+ self.assertEqual(o.rstrip(), TESTCODE.rstrip())
+
+ # Should also work for formatters
+ o = self.check_success('-lpython', '-f',
+ formatter_file + ':HtmlFormatterWrapper',
+ '-x', stdin=TESTCODE)
+ o = re.sub('<[^>]*>', '', o)
+ # rstrip is necessary since HTML inserts a \n after the last </div>
+ self.assertEqual(o.rstrip(), TESTCODE.rstrip())
+
def test_stream_opt(self):
o = self.check_success('-lpython', '-s', '-fterminal', stdin=TESTCODE)
o = re.sub(r'\x1b\[.*?m', '', o)
@@ -211,6 +244,20 @@ class CmdLineTest(unittest.TestCase):
e = self.check_failure('-lfooo', TESTFILE)
self.assertTrue('Error: no lexer for alias' in e)
+ # cannot load .py file without load_from_file flag
+ e = self.check_failure('-l', 'nonexistent.py', TESTFILE)
+ self.assertTrue('Error: no lexer for alias' in e)
+
+ # lexer file is missing/unreadable
+ e = self.check_failure('-l', 'nonexistent.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: cannot read' in e)
+
+ # lexer file is malformed
+ e = self.check_failure('-l', 'support/empty.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: no valid CustomLexer class found' in e)
+
# formatter not found
e = self.check_failure('-lpython', '-ffoo', TESTFILE)
self.assertTrue('Error: no formatter found for name' in e)
@@ -219,6 +266,20 @@ class CmdLineTest(unittest.TestCase):
e = self.check_failure('-ofoo.foo', TESTFILE)
self.assertTrue('Error: no formatter found for file name' in e)
+ # cannot load .py file without load_from_file flag
+ e = self.check_failure('-f', 'nonexistent.py', TESTFILE)
+ self.assertTrue('Error: no formatter found for name' in e)
+
+ # formatter file is missing/unreadable
+ e = self.check_failure('-f', 'nonexistent.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: cannot read' in e)
+
+ # formatter file is malformed
+ e = self.check_failure('-f', 'support/empty.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: no valid CustomFormatter class found' in e)
+
# output file not writable
e = self.check_failure('-o', os.path.join('nonexistent', 'dir', 'out.html'),
'-lpython', TESTFILE)