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.py32
1 files changed, 20 insertions, 12 deletions
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index 5ad815c0..9e26ce17 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -3,17 +3,18 @@
Command line test
~~~~~~~~~~~~~~~~~
- :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
+ :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
-# Test the command line interface
+from __future__ import print_function
-import sys, os
+import io
+import sys
import unittest
-import StringIO
from pygments import highlight
+from pygments.util import StringIO, BytesIO
from pygments.cmdline import main as cmdline_main
import support
@@ -24,14 +25,24 @@ TESTFILE, TESTDIR = support.location(__file__)
def run_cmdline(*args):
saved_stdout = sys.stdout
saved_stderr = sys.stderr
- new_stdout = sys.stdout = StringIO.StringIO()
- new_stderr = sys.stderr = StringIO.StringIO()
+ if sys.version_info > (3,):
+ stdout_buffer = BytesIO()
+ stderr_buffer = BytesIO()
+ new_stdout = sys.stdout = io.TextIOWrapper(stdout_buffer)
+ new_stderr = sys.stderr = io.TextIOWrapper(stderr_buffer)
+ else:
+ stdout_buffer = new_stdout = sys.stdout = StringIO()
+ stderr_buffer = new_stderr = sys.stderr = StringIO()
try:
ret = cmdline_main(["pygmentize"] + list(args))
finally:
sys.stdout = saved_stdout
sys.stderr = saved_stderr
- return (ret, new_stdout.getvalue(), new_stderr.getvalue())
+ new_stdout.flush()
+ new_stderr.flush()
+ out, err = stdout_buffer.getvalue().decode('utf-8'), \
+ stderr_buffer.getvalue().decode('utf-8')
+ return (ret, out, err)
class CmdLineTest(unittest.TestCase):
@@ -82,7 +93,7 @@ class CmdLineTest(unittest.TestCase):
def test_invalid_opts(self):
for opts in [("-L", "-lpy"), ("-L", "-fhtml"), ("-L", "-Ox"),
("-a",), ("-Sst", "-lpy"), ("-H",),
- ("-H", "formatter"),]:
+ ("-H", "formatter")]:
self.assertTrue(run_cmdline(*opts)[0] == 2)
def test_normal(self):
@@ -90,11 +101,8 @@ class CmdLineTest(unittest.TestCase):
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
filename = TESTFILE
- fp = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as fp:
code = fp.read()
- finally:
- fp.close()
output = highlight(code, PythonLexer(), HtmlFormatter())