From 3c7ab91f0764a6258b25b0e145f36a19036058c7 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Wed, 26 Jun 2019 10:59:39 -0400 Subject: [PATCH] Replace the monkey-patching with attributes on instances --- src/allmydata/scripts/tahoe_daemonize.py | 7 ++++++- src/allmydata/test/cli/test_daemonize.py | 11 ++++++++--- src/allmydata/test/cli/test_start.py | 5 ++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/allmydata/scripts/tahoe_daemonize.py b/src/allmydata/scripts/tahoe_daemonize.py index d7cfc89cf..48fa16d56 100644 --- a/src/allmydata/scripts/tahoe_daemonize.py +++ b/src/allmydata/scripts/tahoe_daemonize.py @@ -105,6 +105,8 @@ the twistd-options. class MyTwistdConfig(twistd.ServerOptions): subCommands = [("DaemonizeTahoeNode", None, usage.Options, "node")] + stderr = sys.stderr + class DaemonizeTheRealService(Service, HookMixin): """ @@ -122,6 +124,7 @@ class DaemonizeTheRealService(Service, HookMixin): self._hooks = { "running": None, } + self.stderr = options.parent.stderr def startService(self): @@ -143,7 +146,7 @@ class DaemonizeTheRealService(Service, HookMixin): def handle_config_error(fail): fail.trap(UnknownConfigError) - sys.stderr.write("\nConfiguration error:\n{}\n\n".format(fail.value)) + self.stderr.write("\nConfiguration error:\n{}\n\n".format(fail.value)) reactor.stop() return @@ -204,6 +207,8 @@ def daemonize(config): twistd_args.append("DaemonizeTahoeNode") # point at our DaemonizeTahoeNodePlugin twistd_config = MyTwistdConfig() + twistd_config.stdout = out + twistd_config.stderr = err try: twistd_config.parseOptions(twistd_args) except usage.error as ue: diff --git a/src/allmydata/test/cli/test_daemonize.py b/src/allmydata/test/cli/test_daemonize.py index 78df0aa1a..414061977 100644 --- a/src/allmydata/test/cli/test_daemonize.py +++ b/src/allmydata/test/cli/test_daemonize.py @@ -8,9 +8,14 @@ from allmydata.scripts import runner from allmydata.scripts.tahoe_daemonize import identify_node_type from allmydata.scripts.tahoe_daemonize import DaemonizeTahoeNodePlugin from allmydata.scripts.tahoe_daemonize import DaemonizeOptions +from allmydata.scripts.tahoe_daemonize import MyTwistdConfig class Util(unittest.TestCase): + def setUp(self): + self.twistd_options = MyTwistdConfig() + self.twistd_options.parseOptions(["DaemonizeTahoeNode"]) + self.options = self.twistd_options.subOptions def test_node_type_nothing(self): tmpdir = self.mktemp() @@ -39,7 +44,7 @@ class Util(unittest.TestCase): fn() r.stop = lambda: None r.callWhenRunning = call - service = plug.makeService(None) + service = plug.makeService(self.options) service.parent = Mock() service.startService() @@ -55,7 +60,7 @@ class Util(unittest.TestCase): d.addErrback(lambda _: None) # ignore the error we'll trigger r.callWhenRunning = call r.stop = 'foo' - service = plug.makeService(None) + service = plug.makeService(self.options) service.parent = Mock() # we'll raise ValueError because there's no key-generator # .. BUT we do this in an async function called via @@ -80,7 +85,7 @@ class Util(unittest.TestCase): fn() r.stop = lambda: None r.callWhenRunning = call - service = plug.makeService(None) + service = plug.makeService(self.options) service.parent = Mock() with self.assertRaises(ValueError) as ctx: service.startService() diff --git a/src/allmydata/test/cli/test_start.py b/src/allmydata/test/cli/test_start.py index 790b3ce24..205dc665e 100644 --- a/src/allmydata/test/cli/test_start.py +++ b/src/allmydata/test/cli/test_start.py @@ -255,10 +255,9 @@ class RunTests(unittest.TestCase): ]) i, o, e = StringIO(), StringIO(), StringIO() - with patch.object(sys, 'stdout', o), patch.object(sys, 'stderr', e): - runner.dispatch(config, i, o, e) + runner.dispatch(config, i, o, e) - output = o.getvalue() + output = e.getvalue() # should print out the collected logs and an error-code self.assertIn( "invalid section",