def runTest(self, cmd):
        """Test that the log context manager works with the butler cli to
        initialize the logging system according to cli inputs for the duration
        of the command execution and resets the logging system to its previous
        state or expected state when command execution finishes."""
        pyRoot = self.PythonLogger(None)
        pyButler = self.PythonLogger("lsst.daf.butler")
        lsstRoot = self.LsstLogger("")
        lsstButler = self.LsstLogger("lsst.daf.butler")

        with command_test_env(self.runner, "lsst.daf.butler.tests.cliLogTestBase",
                                           "command-log-settings-test"):
            result = cmd()
        self.assertEqual(result.exit_code, 0, clickResultMsg(result))

        if lsstLog is not None:
            self.assertFalse(hasLsstLogHandler(logging.getLogger()),
                             msg="CliLog should remove the lsst.log handler it added to the root logger.")
        self.assertEqual(pyRoot.logger.level, logging.INFO)
        self.assertEqual(pyButler.logger.level, pyButler.initialLevel)
        if lsstLog is not None:
            self.assertEqual(lsstRoot.logger.getLevel(), lsstLog.INFO)
            # lsstLogLevel can either be the inital level, or uninitialized or
            # the defined default value.
            expectedLsstLogLevel = ((lsstButler.initialLevel, ) if lsstButler.initialLevel != -1
                                    else(-1, CliLog.defaultLsstLogLevel))
            self.assertIn(lsstButler.logger.getLevel(), expectedLsstLogLevel)
 def test_loadTopHelp(self):
     """Test that an expected command is produced by 'butler --help'"""
     with command_test_env(self.runner, "test_cliPluginLoader",
                           "command-test"):
         result = self.runner.invoke(butler.cli, "--help")
         self.assertEqual(
             result.exit_code, 0,
             f"output: {result.output} exception: {result.exception}")
         self.assertIn("command-test", result.stdout)
 def test_loadAndExecutePluginCommand(self):
     """Test that a plugin command can be loaded and executed."""
     with command_test_env(self.runner, "test_cliPluginLoader",
                           "command-test"):
         result = self.runner.invoke(butler.cli, "command-test")
         self.assertEqual(
             result.exit_code, 0,
             f"output: {result.output} exception: {result.exception}")
         self.assertEqual(result.stdout, "test command\n")
 def test_unimportablePlugin(self):
     with command_test_env(self.runner, "test_cliPluginLoader",
                           "non-existant-command-function"):
         with self.assertLogs() as cm:
             result = self.runner.invoke(butler.cli, "--help")
         self.assertEqual(
             result.exit_code, 0,
             f"output: {result.output} exception: {result.exception}")
         expectedErrMsg = "Could not import plugin from " \
                          "test_cliPluginLoader.non_existant_command_function, skipping."
         self.assertIn(expectedErrMsg, " ".join(cm.output))