Example #1
0
File: test_cli.py Project: sstj/cot
class TestCOTCLI(COT_UT):

    """Parent class for CLI test cases."""

    def setUp(self):
        """Test case setup function called automatically prior to each test."""
        self.cli = CLI(terminal_width=80)
        self.maxDiff = None
        super(TestCOTCLI, self).setUp()

    def tearDown(self):
        """Test case cleanup function called automatically after each test."""
        # If we set the verbosity of the CLI directly, the CLI logger is on.
        # The CLI normally turns the logger back off at the end of cli.main()
        # but in some of our CLI test cases we don't call cli.main(), so
        # to prevent leakage of logs, we clean up manually if needed.
        if self.cli.master_logger:
            self.cli.master_logger.removeHandler(self.cli.handler)
            self.cli.master_logger = None
            self.cli.handler.close()
            self.cli.handler = None
        super(TestCOTCLI, self).tearDown()

    def call_cot(self, argv, result=0, fixup_args=True):
        """Invoke COT CLI, capturing stdout and stderr, and check the rc.

        In the case of an incorrect rc, the test will fail.
        Otherwise, will return the combined stdout/stderr.
        """
        rc = -1
        if fixup_args:
            argv = ['--quiet'] + argv
        _si = sys.stdin
        _so = sys.stdout
        _se = sys.stderr
        try:
            with open(os.devnull, 'w') as devnull:
                sys.stdin = devnull
                sys.stdout = StringIO.StringIO()
                sys.stderr = sys.stdout
                rc = self.cli.run(argv)
        except SystemExit as se:
            rc = se.code
            try:
                rc = int(rc)
            except (TypeError, ValueError):
                print(rc, file=sys.stderr)
                rc = 1
        finally:
            sys.stdin = _si
            stdout = sys.stdout.getvalue()
            sys.stdout = _so
            sys.stderr = _se

        self.assertEqual(rc, result,
                         "\nargv: {0}\nstdout:\n{1}"
                         .format(" ".join(argv), stdout))
        return stdout
Example #2
0
File: test_cli.py Project: sstj/cot
 def setUp(self):
     """Test case setup function called automatically prior to each test."""
     self.cli = CLI(terminal_width=80)
     self.maxDiff = None
     super(TestCOTCLI, self).setUp()