Fix commandline test character encoding issues for Py3

This commit is contained in:
Jim Paris 2019-08-01 13:36:37 -04:00
parent 613a3185e3
commit 6d673bd2be

View File

@ -54,11 +54,6 @@ def setup_module():
def teardown_module():
server_stop()
# Add an encoding property to StringIO so Python will convert Unicode
# properly when writing or reading.
class UTF8StringIO(io.StringIO):
encoding = 'utf-8'
class TestCmdline(object):
def run(self, arg_string, infile=None, outfile=None):
@ -76,22 +71,20 @@ class TestCmdline(object):
( sys.stdin, sys.stdout, sys.stderr ) = self.saved
# Empty input if none provided
if infile is None:
infile = UTF8StringIO("")
infile = io.StringIO("")
# Capture stderr
errfile = UTF8StringIO()
errfile = io.StringIO()
if outfile is None:
# If no output file, capture stdout with stderr
outfile = errfile
with stdio_wrapper(infile, outfile, errfile) as s:
try:
# shlex doesn't support Unicode very well. Encode the
# string as UTF-8 explicitly before splitting.
args = shlex.split(arg_string.encode('utf-8'))
args = shlex.split(arg_string)
nilmdb.cmdline.Cmdline(args).run()
sys.exit(0)
except SystemExit as e:
exitcode = e.code
captured = nilmdb.utils.str.decode(outfile.getvalue())
captured = outfile.getvalue()
self.captured = captured
self.exitcode = exitcode