Exemple #1
0
    def execute_cmd_capture(self, cmd, expected_out, expected_err):
        """Run a single command, captures the output and and stderr and compare it to the expected ones"""
        sys_stdout, sys_stderr = sys.stdout, sys.stderr
        sys.stdout = p3._fake_stdio(
            additional_out=sys_stdout if PRINT_OUTPUT else None)
        sys.stderr = p3._fake_stdio(
            additional_out=sys_stderr if PRINT_OUTPUT else None)

        try:
            pubs_cmd.execute(cmd)
        finally:
            # capturing output even if exception was raised.
            self.captured_stdout = self.normalize(
                p3._get_fake_stdio_ucontent(sys.stdout))
            self.captured_stderr = self.normalize(
                p3._get_fake_stdio_ucontent(sys.stderr))
            sys.stderr, sys.stdout = sys_stderr, sys_stdout

        if expected_out is not None:
            self.assertEqual(p3.u_maybe(self.captured_stdout),
                             p3.u_maybe(expected_out))
        if expected_err is not None:
            self.assertEqual(p3.u_maybe(self.captured_stderr),
                             p3.u_maybe(expected_err))
        return self.captured_stdout
Exemple #2
0
    def execute_cmds(self, cmds, capture_output=CAPTURE_OUTPUT):
        """ Execute a list of commands, and capture their output

        A command can be a string, or a tuple of size 2, 3 or 4.
        In the latter case, the command is :
        1. a string reprensenting the command to execute
        2. the user inputs to feed to the command during execution
        3. the expected output on stdout, verified with assertEqual.
        4. the expected output on stderr, verified with assertEqual.
        """
        try:
            outs = []
            for cmd in cmds:
                inputs = []
                expected_out, expected_err = None, None
                actual_cmd = cmd
                if not isinstance(cmd, p3.ustr):
                    actual_cmd = cmd[0]
                    if len(cmd) >= 2 and cmd[1] is not None:  # Inputs provided
                        inputs = cmd[1]
                    if len(cmd) >= 3:  # Expected output provided
                        capture_output = True
                        if cmd[2] is not None:
                            expected_out = color.undye(cmd[2])
                    if len(cmd) >= 4 and cmd[
                            3] is not None:  # Expected error output provided
                        expected_err = color.undye(cmd[3])
                # Always set fake input: test should not ask unexpected user input
                input = fake_env.FakeInput(inputs, [content, uis, p3])
                input.as_global()
                try:
                    if capture_output:
                        capture_wrap = fake_env.capture(pubs_cmd.execute,
                                                        verbose=PRINT_OUTPUT)
                        _, stdout, stderr = capture_wrap(actual_cmd.split())
                        actual_out = color.undye(stdout)
                        actual_err = color.undye(stderr)
                        if expected_out is not None:
                            self.assertEqual(p3.u_maybe(actual_out),
                                             p3.u_maybe(expected_out))
                        if expected_err is not None:
                            self.assertEqual(p3.u_maybe(actual_err),
                                             p3.u_maybe(expected_err))
                        outs.append(color.undye(actual_out))
                    else:
                        pubs_cmd.execute(actual_cmd.split())
                except fake_env.FakeInput.UnexpectedInput as e:
                    self.fail('Unexpected input asked by command: {}.'.format(
                        actual_cmd))
            return outs
        except SystemExit as exc:
            exc_class, exc, tb = sys.exc_info()
            if sys.version_info.major == 2:
                # using six to avoid a SyntaxError in Python 3.x
                six.reraise(FakeSystemExit, FakeSystemExit(*exc.args), tb)
            else:
                raise FakeSystemExit(*exc.args).with_traceback(tb)
Exemple #3
0
    def execute_cmds(self, cmds, capture_output=CAPTURE_OUTPUT):
        """ Execute a list of commands, and capture their output

        A command can be a string, or a tuple of size 2, 3 or 4.
        In the latter case, the command is :
        1. a string reprensenting the command to execute
        2. the user inputs to feed to the command during execution
        3. the expected output on stdout, verified with assertEqual.
        4. the expected output on stderr, verified with assertEqual.
        """
        try:
            outs = []
            for cmd in cmds:
                inputs = []
                expected_out, expected_err = None, None
                actual_cmd = cmd
                if not isinstance(cmd, p3.ustr):
                    actual_cmd = cmd[0]
                    if len(cmd) == 2:  # Inputs provided
                        inputs = cmd[1]
                    if len(cmd) == 3:  # Expected output provided
                        capture_output = True
                        expected_out = color.undye(cmd[2])
                    if len(cmd) == 4:  # Expected error output provided
                        expected_err = color.undye(cmd[3])
                # Always set fake input: test should not ask unexpected user input
                input = fake_env.FakeInput(inputs, [content, uis, p3])
                input.as_global()
                try:
                    if capture_output:
                        _, stdout, stderr = fake_env.redirect(pubs_cmd.execute)(
                            actual_cmd.split())
                        actual_out = color.undye(stdout)
                        actual_err = color.undye(stderr)
                        if expected_out is not None:
                            self.assertEqual(actual_out, expected_out)
                        if expected_err is not None:
                            self.assertEqual(actual_err, expected_err)
                        outs.append(color.undye(actual_out))
                    else:
                        pubs_cmd.execute(actual_cmd.split())
                except fake_env.FakeInput.UnexpectedInput:
                    self.fail('Unexpected input asked by command: {}.'.format(
                        actual_cmd))
            if PRINT_OUTPUT:
                print(outs)
            return outs
        except SystemExit as exc:
            exc_class, exc, tb = sys.exc_info()
            if sys.version_info.major == 2:
                # using six to avoid a SyntaxError in Python 3.x
                six.reraise(FakeSystemExit, exc, tb)
            else:
                raise FakeSystemExit(exc).with_traceback(tb)
Exemple #4
0
    def execute_cmds(self, cmds, capture_output=CAPTURE_OUTPUT):
        """ Execute a list of commands, and capture their output

        A command can be a string, or a tuple of size 2 or 3.
        In the latter case, the command is :
        1. a string reprensenting the command to execute
        2. the user inputs to feed to the command during execution
        3. the output expected, verified with assertEqual. Always captures
        output in this case.

        """
        outs = []
        for cmd in cmds:
            inputs = []
            output = None
            actual_cmd = cmd
            current_capture_output = capture_output
            if not isinstance(cmd, p3.ustr):
                actual_cmd = cmd[0]
                if len(cmd) == 2:  # Inputs provided
                    inputs = cmd[1]
                if len(cmd) == 3:  # Expected output provided
                    current_capture_output = True
                    output = cmd[2]
            # Always set fake input: test should not ask unexpected user input
            input = fake_env.FakeInput(inputs, [content, uis, p3])
            input.as_global()
            try:
                if current_capture_output:
                    _, stdout, stderr = fake_env.redirect(pubs_cmd.execute)(
                        actual_cmd.split())
                    self.assertEqual(stderr, '')
                    actual_out = color.undye(stdout)
                    if output is not None:
                        correct_out = color.undye(output)
                        self.assertEqual(actual_out, correct_out)
                    outs.append(color.undye(actual_out))
                else:
                    pubs_cmd.execute(cmd.split())
            except fake_env.FakeInput.UnexpectedInput:
                self.fail('Unexpected input asked by command: {}.'.format(
                    actual_cmd))
        if PRINT_OUTPUT:
            print(outs)
        return outs