test_system.py passes on both Python 2 and Python 3.

This commit is contained in:
Itamar Turner-Trauring 2020-12-14 11:07:37 -05:00
parent 93fb28f6ad
commit 28f46e9b06
2 changed files with 29 additions and 18 deletions

View File

@ -1,5 +1,8 @@
from __future__ import print_function from __future__ import print_function
from future.utils import PY2, native_str
from future.builtins import str as future_str
import os import os
import time import time
import signal import signal
@ -50,24 +53,23 @@ def _getvalue(io):
return io.read() return io.read()
def run_cli_bytes(verb, *args, **kwargs): def run_cli_native(verb, *args, **kwargs):
""" """
Run a Tahoe-LAFS CLI command specified as bytes. Run a Tahoe-LAFS CLI command specified as bytes (on Python 2) or Unicode
(on Python 3); basically, it accepts a native string.
Most code should prefer ``run_cli_unicode`` which deals with all the Most code should prefer ``run_cli_unicode`` which deals with all the
necessary encoding considerations. This helper still exists so that novel necessary encoding considerations.
misconfigurations can be explicitly tested (for example, receiving UTF-8
bytes when the system encoding claims to be ASCII).
:param bytes verb: The command to run. For example, ``b"create-node"``. :param native_str verb: The command to run. For example, ``b"create-node"``.
:param [bytes] args: The arguments to pass to the command. For example, :param [native_str] args: The arguments to pass to the command. For example,
``(b"--hostname=localhost",)``. ``(b"--hostname=localhost",)``.
:param [bytes] nodeargs: Extra arguments to pass to the Tahoe executable :param [native_str] nodeargs: Extra arguments to pass to the Tahoe executable
before ``verb``. before ``verb``.
:param bytes stdin: Text to pass to the command via stdin. :param native_str stdin: Text to pass to the command via stdin.
:param NoneType|str encoding: The name of an encoding which stdout and :param NoneType|str encoding: The name of an encoding which stdout and
stderr will be configured to use. ``None`` means stdout and stderr stderr will be configured to use. ``None`` means stdout and stderr
@ -77,8 +79,8 @@ def run_cli_bytes(verb, *args, **kwargs):
nodeargs = kwargs.pop("nodeargs", []) nodeargs = kwargs.pop("nodeargs", [])
encoding = kwargs.pop("encoding", None) encoding = kwargs.pop("encoding", None)
precondition( precondition(
all(isinstance(arg, bytes) for arg in [verb] + nodeargs + list(args)), all(isinstance(arg, native_str) for arg in [verb] + nodeargs + list(args)),
"arguments to run_cli must be bytes -- convert using unicode_to_argv", "arguments to run_cli must be a native string -- convert using unicode_to_argv",
verb=verb, verb=verb,
args=args, args=args,
nodeargs=nodeargs, nodeargs=nodeargs,
@ -137,15 +139,19 @@ def run_cli_unicode(verb, argv, nodeargs=None, stdin=None, encoding=None):
if nodeargs is None: if nodeargs is None:
nodeargs = [] nodeargs = []
precondition( precondition(
all(isinstance(arg, unicode) for arg in [verb] + nodeargs + argv), all(isinstance(arg, future_str) for arg in [verb] + nodeargs + argv),
"arguments to run_cli_unicode must be unicode", "arguments to run_cli_unicode must be unicode",
verb=verb, verb=verb,
nodeargs=nodeargs, nodeargs=nodeargs,
argv=argv, argv=argv,
) )
codec = encoding or "ascii" codec = encoding or "ascii"
encode = lambda t: None if t is None else t.encode(codec) if PY2:
d = run_cli_bytes( encode = lambda t: None if t is None else t.encode(codec)
else:
# On Python 3 command-line parsing expects Unicode!
encode = lambda t: t
d = run_cli_native(
encode(verb), encode(verb),
nodeargs=list(encode(arg) for arg in nodeargs), nodeargs=list(encode(arg) for arg in nodeargs),
stdin=encode(stdin), stdin=encode(stdin),
@ -163,7 +169,7 @@ def run_cli_unicode(verb, argv, nodeargs=None, stdin=None, encoding=None):
return d return d
run_cli = run_cli_bytes run_cli = run_cli_native
def parse_cli(*argv): def parse_cli(*argv):

View File

@ -63,7 +63,7 @@ from .web.common import (
# TODO: move this to common or common_util # TODO: move this to common or common_util
from allmydata.test.test_runner import RunBinTahoeMixin from allmydata.test.test_runner import RunBinTahoeMixin
from . import common_util as testutil from . import common_util as testutil
from .common_util import run_cli_bytes from .common_util import run_cli_unicode
from ..scripts.common import ( from ..scripts.common import (
write_introducer, write_introducer,
) )
@ -72,7 +72,10 @@ def run_cli(*args, **kwargs):
""" """
Backwards compatible version so we don't have to change all the tests. Backwards compatible version so we don't have to change all the tests.
""" """
return run_cli_bytes(*(ensure_binary(a) for a in args), **kwargs) nodeargs = [ensure_text(a) for a in kwargs.pop("nodeargs", [])]
kwargs["nodeargs"] = nodeargs
return run_cli_unicode(
ensure_text(args[0]), [ensure_text(a) for a in args[1:]], **kwargs)
@ -1287,7 +1290,9 @@ class SystemTest(SystemTestMixin, RunBinTahoeMixin, unittest.TestCase):
s = stats["stats"] s = stats["stats"]
self.failUnlessEqual(s["storage_server.accepting_immutable_shares"], 1) self.failUnlessEqual(s["storage_server.accepting_immutable_shares"], 1)
c = stats["counters"] c = stats["counters"]
self.failUnless("storage_server.allocate" in c) # Probably this should be Unicode eventually? But we haven't ported
# stats code yet.
self.failUnless(b"storage_server.allocate" in c)
d.addCallback(_grab_stats) d.addCallback(_grab_stats)
return d return d