コード例 #1
0
ファイル: popen.py プロジェクト: mandarcthorat/testfixtures
    def Popen(self,
              args,
              bufsize=0,
              executable=None,
              stdin=None,
              stdout=None,
              stderr=None,
              preexec_fn=None,
              close_fds=False,
              shell=False,
              cwd=None,
              env=None,
              universal_newlines=False,
              startupinfo=None,
              creationflags=0,
              restore_signals=True,
              start_new_session=False,
              pass_fds=(),
              encoding=None,
              errors=None):

        if isinstance(args, basestring):
            cmd = args
        else:
            cmd = ' '.join(args)

        behaviour = self.commands.get(cmd, self.default_command)
        if behaviour is None:
            raise KeyError('Nothing specified for command %r' % cmd)

        stdout_value, stderr_value, self.returncode, pid, poll = behaviour

        if stderr == STDOUT:
            line_iterator = chain.from_iterable(
                zip_longest(stdout_value.splitlines(True),
                            stderr_value.splitlines(True)))
            stdout_value = b''.join(l for l in line_iterator if l)
            stderr_value = None

        self.poll_count = poll
        for name, option, mock_value in (('stdout', stdout, stdout_value),
                                         ('stderr', stderr, stderr_value)):
            value = None
            if option is PIPE:
                value = TemporaryFile()
                value.write(mock_value)
                value.flush()
                value.seek(0)
            setattr(self.mock.Popen_instance, name, value)

        if stdin == PIPE:
            self.mock.Popen_instance.stdin = Mock()

        self.mock.Popen_instance.pid = pid
        self.mock.Popen_instance.returncode = None

        return self.mock.Popen_instance
コード例 #2
0
ファイル: popen.py プロジェクト: nebulans/testfixtures
    def Popen(self, args, bufsize=0, executable=None,
              stdin=None, stdout=None, stderr=None,
              preexec_fn=None, close_fds=False, shell=False, cwd=None,
              env=None, universal_newlines=False,
              startupinfo=None, creationflags=0, restore_signals=True,
              start_new_session=False, pass_fds=(), encoding=None, errors=None):

        if isinstance(args, basestring):
            cmd = args
        else:
            cmd = ' '.join(args)

        behaviour = self.commands.get(cmd, self.default_behaviour)
        if behaviour is None:
            raise KeyError('Nothing specified for command %r' % cmd)

        if callable(behaviour):
            behaviour = behaviour(command=cmd, stdin=stdin)

        self.returncode = behaviour.returncode

        stdout_value = behaviour.stdout
        stderr_value = behaviour.stderr

        if stderr == STDOUT:
            line_iterator = chain.from_iterable(zip_longest(
                stdout_value.splitlines(True),
                stderr_value.splitlines(True)
            ))
            stdout_value = b''.join(l for l in line_iterator if l)
            stderr_value = None

        self.poll_count = behaviour.poll_count
        for name, option, mock_value in (
            ('stdout', stdout, stdout_value),
            ('stderr', stderr, stderr_value)
        ):
            value = None
            if option is PIPE:
                value = TemporaryFile()
                value.write(mock_value)
                value.flush()
                value.seek(0)
            setattr(self.mock.Popen_instance, name, value)

        if stdin == PIPE:
            self.mock.Popen_instance.stdin = Mock()

        self.mock.Popen_instance.pid = behaviour.pid
        self.mock.Popen_instance.returncode = None

        return self.mock.Popen_instance
コード例 #3
0
    def __init__(self,
                 mock_class,
                 root_call,
                 args,
                 bufsize=0,
                 executable=None,
                 stdin=None,
                 stdout=None,
                 stderr=None,
                 preexec_fn=None,
                 close_fds=False,
                 shell=False,
                 cwd=None,
                 env=None,
                 universal_newlines=False,
                 startupinfo=None,
                 creationflags=0,
                 restore_signals=True,
                 start_new_session=False,
                 pass_fds=(),
                 encoding=None,
                 errors=None,
                 text=None):
        self.mock = Mock()
        self.class_instance_mock = mock_class.mock.Popen_instance
        #: A :func:`unittest.mock.call` representing the call made to instantiate
        #: this mock process.
        self.root_call = root_call
        #: The calls made on this mock process, represented using
        #: :func:`~unittest.mock.call` instances.
        self.calls = []
        self.all_calls = mock_class.all_calls

        cmd = shell_join(args)

        behaviour = mock_class.commands.get(cmd, mock_class.default_behaviour)
        if behaviour is None:
            raise KeyError('Nothing specified for command %r' % cmd)

        if callable(behaviour):
            behaviour = behaviour(command=cmd, stdin=stdin)

        self.behaviour = behaviour

        stdout_value = behaviour.stdout
        stderr_value = behaviour.stderr

        if stderr == STDOUT:
            line_iterator = chain.from_iterable(
                zip_longest(stdout_value.splitlines(True),
                            stderr_value.splitlines(True)))
            stdout_value = b''.join(l for l in line_iterator if l)
            stderr_value = None

        self.poll_count = behaviour.poll_count
        for name, option, mock_value in (('stdout', stdout, stdout_value),
                                         ('stderr', stderr, stderr_value)):
            value = None
            if option is PIPE:
                value = TemporaryFile()
                value.write(mock_value)
                value.flush()
                value.seek(0)
                if PY3 and (universal_newlines or text or encoding):
                    value = TextIOWrapper(value,
                                          encoding=encoding,
                                          errors=errors)
            setattr(self, name, value)

        if stdin == PIPE:
            self.stdin = Mock()
            for method in 'write', 'close':
                record_writes = partial(self._record, ('stdin', method))
                getattr(self.stdin, method).side_effect = record_writes

        self.pid = behaviour.pid
        #: The return code of this mock process.
        self.returncode = None
        if PY3:
            self.args = args
コード例 #4
0
ファイル: popen.py プロジェクト: Simplistix/testfixtures
    def __init__(self, mock_class, root_call,
                 args, bufsize=0, executable=None,
                 stdin=None, stdout=None, stderr=None,
                 preexec_fn=None, close_fds=False, shell=False, cwd=None,
                 env=None, universal_newlines=False,
                 startupinfo=None, creationflags=0, restore_signals=True,
                 start_new_session=False, pass_fds=(),
                 encoding=None, errors=None):
        self.mock = Mock()
        self.class_instance_mock = mock_class.mock.Popen_instance
        #: A :func:`unittest.mock.call` representing the call made to instantiate
        #: this mock process.
        self.root_call = root_call
        #: The calls made on this mock process, represented using
        #: :func:`~unittest.mock.call` instances.
        self.calls = []
        self.all_calls = mock_class.all_calls

        cmd = shell_join(args)

        behaviour = mock_class.commands.get(cmd, mock_class.default_behaviour)
        if behaviour is None:
            raise KeyError('Nothing specified for command %r' % cmd)

        if callable(behaviour):
            behaviour = behaviour(command=cmd, stdin=stdin)

        self.behaviour = behaviour

        stdout_value = behaviour.stdout
        stderr_value = behaviour.stderr

        if stderr == STDOUT:
            line_iterator = chain.from_iterable(zip_longest(
                stdout_value.splitlines(True),
                stderr_value.splitlines(True)
            ))
            stdout_value = b''.join(l for l in line_iterator if l)
            stderr_value = None

        self.poll_count = behaviour.poll_count
        for name, option, mock_value in (
            ('stdout', stdout, stdout_value),
            ('stderr', stderr, stderr_value)
        ):
            value = None
            if option is PIPE:
                value = TemporaryFile()
                value.write(mock_value)
                value.flush()
                value.seek(0)
            setattr(self, name, value)

        if stdin == PIPE:
            self.stdin = Mock()
            for method in 'write', 'close':
                record_writes = partial(self._record, ('stdin', method))
                getattr(self.stdin, method).side_effect = record_writes

        self.pid = behaviour.pid
        #: The return code of this mock process.
        self.returncode = None
        if PY3:
            self.args = args