From: Douglas Bagnall Date: Wed, 7 Sep 2022 04:33:33 +0000 (+1200) Subject: samba-tool: _resolve() can set outf, errf X-Git-Tag: talloc-2.4.0~1126 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=304ac5bb777029701b79b4f64370643865d3ee4f;p=thirdparty%2Fsamba.git samba-tool: _resolve() can set outf, errf We catch output in outf and errf for testing, which we currently do with cmd.outf = self.stringIO() cmd.errf = self.stringIO() on the final resolved commands. But this does not catch the output of the super-commands, of which we normally expect none. Using supercmd._resolve(*args, outf=self.stringIO(), errf=self.stringIO()) will redirect output all the way up the chain. Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/netcmd/__init__.py b/python/samba/netcmd/__init__.py index c51ffc1b2cd..874f553e910 100644 --- a/python/samba/netcmd/__init__.py +++ b/python/samba/netcmd/__init__.py @@ -89,9 +89,14 @@ class Command(object): raw_args = None raw_kwargs = None + def _set_files(self, outf=None, errf=None): + if outf is not None: + self.outf = outf + if errf is not None: + self.errf = errf + def __init__(self, outf=sys.stdout, errf=sys.stderr): - self.outf = outf - self.errf = errf + self._set_files(outf, errf) def usage(self, prog=None): parser, _ = self._create_parser(prog) @@ -158,9 +163,9 @@ class Command(object): def message(self, text): self.outf.write(text + "\n") - - def _resolve(self, path, *argv): + def _resolve(self, path, *argv, outf=None, errf=None): """This is a leaf node, the command that will actually run.""" + self._set_files(outf, errf) self.command_name = path return (self, argv) @@ -234,7 +239,7 @@ class SuperCommand(Command): subcommands = {} - def _resolve(self, path, *args): + def _resolve(self, path, *args, outf=None, errf=None): """This is an internal node. We need to consume one of the args and find the relevant child, returning an instance of that Command. @@ -242,6 +247,7 @@ class SuperCommand(Command): and its _run() will do a --help like thing. """ self.command_name = path + self._set_files(outf, errf) # We collect up certain option arguments and pass them to the # leaf, which is why we iterate over args, though we really @@ -254,7 +260,7 @@ class SuperCommand(Command): sub_path = f'{path} {a}' sub = self.subcommands[a] - return sub._resolve(sub_path, *sub_args) + return sub._resolve(sub_path, *sub_args, outf=outf, errf=errf) elif a in [ '--help', 'help', None, '-h', '-V', '--version' ]: # we pass these to the leaf node.