Exemple #1
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 #2
0
 def execute_cmdsplit(self, actual_cmdlist, expected_out, expected_err):
     """Run a single command, which has been split into a list containing cmd and args"""
     capture_wrap = fake_env.capture(pubs_cmd.execute, verbose=PRINT_OUTPUT)
     _, stdout, stderr = capture_wrap(actual_cmdlist)
     actual_out = self.normalize(stdout)
     actual_err = self.normalize(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))
     return actual_out
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.
        """
        def normalize(s):
            s = color.undye(s)
            try:
                s = s.decode('utf-8')
            except AttributeError:
                pass
            return s

        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 = normalize(stdout)
                        actual_err = normalize(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:
                    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)