| 1 | """ |
|---|
| 2 | Ported to Python 3. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from six import ensure_str, ensure_text |
|---|
| 6 | |
|---|
| 7 | from ...scripts import runner |
|---|
| 8 | from ..common_util import ReallyEqualMixin, run_cli, run_cli_unicode |
|---|
| 9 | |
|---|
| 10 | def parse_options(basedir, command, args): |
|---|
| 11 | args = [ensure_text(s) for s in args] |
|---|
| 12 | o = runner.Options() |
|---|
| 13 | o.parseOptions(["--node-directory", basedir, command] + args) |
|---|
| 14 | while hasattr(o, "subOptions"): |
|---|
| 15 | o = o.subOptions |
|---|
| 16 | return o |
|---|
| 17 | |
|---|
| 18 | class CLITestMixin(ReallyEqualMixin): |
|---|
| 19 | """ |
|---|
| 20 | A mixin for use with ``GridTestMixin`` to execute CLI commands against |
|---|
| 21 | nodes created by methods of that mixin. |
|---|
| 22 | """ |
|---|
| 23 | def do_cli_unicode(self, verb, argv, client_num=0, **kwargs): |
|---|
| 24 | """ |
|---|
| 25 | Run a Tahoe-LAFS CLI command. |
|---|
| 26 | |
|---|
| 27 | :param verb: See ``run_cli_unicode``. |
|---|
| 28 | |
|---|
| 29 | :param argv: See ``run_cli_unicode``. |
|---|
| 30 | |
|---|
| 31 | :param int client_num: The number of the ``GridTestMixin``-created |
|---|
| 32 | node against which to execute the command. |
|---|
| 33 | |
|---|
| 34 | :param kwargs: Additional keyword arguments to pass to |
|---|
| 35 | ``run_cli_unicode``. |
|---|
| 36 | """ |
|---|
| 37 | # client_num is used to execute client CLI commands on a specific |
|---|
| 38 | # client. |
|---|
| 39 | client_dir = self.get_clientdir(i=client_num) |
|---|
| 40 | nodeargs = [ u"--node-directory", client_dir ] |
|---|
| 41 | return run_cli_unicode(verb, argv, nodeargs=nodeargs, **kwargs) |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | def do_cli(self, verb, *args, **kwargs): |
|---|
| 45 | """ |
|---|
| 46 | Like ``do_cli_unicode`` but work with ``bytes`` everywhere instead of |
|---|
| 47 | ``unicode``. |
|---|
| 48 | |
|---|
| 49 | Where possible, prefer ``do_cli_unicode``. |
|---|
| 50 | """ |
|---|
| 51 | # client_num is used to execute client CLI commands on a specific |
|---|
| 52 | # client. |
|---|
| 53 | client_num = kwargs.pop("client_num", 0) |
|---|
| 54 | # If we were really going to launch a child process then |
|---|
| 55 | # `unicode_to_argv` would be the right thing to do here. However, |
|---|
| 56 | # we're just going to call some Python functions directly and those |
|---|
| 57 | # Python functions want native strings. So ignore the requirements |
|---|
| 58 | # for passing arguments to another process and make sure this argument |
|---|
| 59 | # is a native string. |
|---|
| 60 | verb = ensure_str(verb) |
|---|
| 61 | args = [ensure_str(arg) for arg in args] |
|---|
| 62 | client_dir = ensure_str(self.get_clientdir(i=client_num)) |
|---|
| 63 | nodeargs = [ "--node-directory", client_dir ] |
|---|
| 64 | return run_cli(verb, *args, nodeargs=nodeargs, **kwargs) |
|---|