From 8318378b1ee2f6c0a9e5446036c7409228545e16 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Wed, 22 Oct 2014 11:02:51 -0500 Subject: testsuite: better debug capturing for options tests --- src/lib/Bcfg2/Options/Options.py | 8 +++- .../Testsrc/Testlib/TestOptions/TestSubcommands.py | 44 +++++++++++----------- testsuite/common.py | 6 +++ 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/lib/Bcfg2/Options/Options.py b/src/lib/Bcfg2/Options/Options.py index 4efd76929..bd1a72fc7 100644 --- a/src/lib/Bcfg2/Options/Options.py +++ b/src/lib/Bcfg2/Options/Options.py @@ -17,14 +17,18 @@ from Bcfg2.Compat import ConfigParser __all__ = ["Option", "BooleanOption", "PathOption", "PositionalArgument", "_debug"] +unit_test = False + def _debug(msg): """ Option parsing happens before verbose/debug have been set -- they're options, after all -- so option parsing verbosity is enabled by changing this to True. The verbosity here is primarily of use to developers. """ - if os.environ.get('BCFG2_OPTIONS_DEBUG', '0').lower() in ["true", "yes", - "on", "1"]: + if unit_test: + print("DEBUG: %s" % msg) + elif os.environ.get('BCFG2_OPTIONS_DEBUG', '0').lower() in ["true", "yes", + "on", "1"]: sys.stderr.write("%s\n" % msg) diff --git a/testsuite/Testsrc/Testlib/TestOptions/TestSubcommands.py b/testsuite/Testsrc/Testlib/TestOptions/TestSubcommands.py index 35da909cb..65b4c19c0 100644 --- a/testsuite/Testsrc/Testlib/TestOptions/TestSubcommands.py +++ b/testsuite/Testsrc/Testlib/TestOptions/TestSubcommands.py @@ -86,15 +86,21 @@ class TestSubcommands(OptionTestCase): self.assertEqual(self.one().usage().strip(), "localone [--test-one TEST_ONE]") - @make_config() - def test_help(self, config_file): - """sane help message from subcommand registry.""" - self.parser.parse(["-C", config_file, "help"]) + def _get_subcommand_output(self, args): + self.parser.parse(args) old_stdout = sys.stdout sys.stdout = StringIO() - self.assertIn(self.registry.runcommand(), [0, None]) - help_message = sys.stdout.getvalue().splitlines() + rv = self.registry.runcommand() + output = [l for l in sys.stdout.getvalue().splitlines() + if not l.startswith("DEBUG: ")] sys.stdout = old_stdout + return (rv, output) + + @make_config() + def test_help(self, config_file): + """sane help message from subcommand registry.""" + rv, output = self._get_subcommand_output(["-C", config_file, "help"]) + self.assertIn(rv, [0, None]) # the help message will look like: # @@ -106,7 +112,7 @@ class TestSubcommands(OptionTestCase): "help": self.registry.help.usage(), "localone": self.one().usage(), "localtwo": self.two().usage()} - for line in help_message: + for line in output: command = line.split()[0] commands.append(command) if command not in command_help: @@ -118,24 +124,16 @@ class TestSubcommands(OptionTestCase): @make_config() def test_subcommand_help(self, config_file): """get help message on a single command.""" - self.parser.parse(["-C", config_file, "help", "localone"]) - old_stdout = sys.stdout - sys.stdout = StringIO() - self.assertIn(self.registry.runcommand(), [0, None]) - help_message = sys.stdout.getvalue().splitlines() - sys.stdout = old_stdout - - self.assertEqual(help_message[0].strip(), + rv, output = self._get_subcommand_output( + ["-C", config_file, "help", "localone"]) + self.assertIn(rv, [0, None]) + self.assertEqual(output[0].strip(), "usage: %s" % self.one().usage().strip()) @make_config() def test_nonexistent_subcommand_help(self, config_file): """get help message on a nonexistent command.""" - self.parser.parse(["-C", config_file, "help", "blargle"]) - old_stdout = sys.stdout - sys.stdout = StringIO() - self.assertNotEqual(self.registry.runcommand(), 0) - help_message = sys.stdout.getvalue().splitlines() - sys.stdout = old_stdout - - self.assertIn("No such command", help_message[0]) + rv, output = self._get_subcommand_output( + ["-C", config_file, "help", "blargle"]) + self.assertNotEqual(rv, 0) + self.assertIn("No such command", output[0]) diff --git a/testsuite/common.py b/testsuite/common.py index 49579d7ef..a86e9c5d9 100644 --- a/testsuite/common.py +++ b/testsuite/common.py @@ -38,7 +38,13 @@ def set_setup_default(option, value=None): if not hasattr(Bcfg2.Options.setup, option): setattr(Bcfg2.Options.setup, option, value) +# these two variables do slightly different things for unit tests; the +# former skips config file reading, while the latter sends option +# debug logging to stdout so it can be captured. These are separate +# because we want to enable config file reading in order to test +# option parsing. Bcfg2.Options.Parser.unit_test = True +Bcfg2.Options.Options.unit_test = True try: import django.conf -- cgit v1.2.3-1-g7c22