示例#1
0
文件: test_usecase.py 项目: 73/pubs
    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)
示例#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:  # 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)
示例#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 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