aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/testutils/popen.py
blob: 725e1f7720f026a8858d48f59681af778605ab48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# vim: set fileencoding=utf-8 :

import functools
import mock


def patch_popen(stdout=b'', stderr=b'', returncode=1):
    """Decorator to easily set the return value of popen.communicate()"""
    def patch_popen_decorator(func):
        @functools.wraps(func)
        def wrap(self):
            with mock.patch('subprocess.Popen') as create_mock:
                popen_mock = mock.Mock(**{'returncode': returncode,
                                          'communicate.return_value': (stdout, stderr)})
                create_mock.return_value = popen_mock
                return func(self, create_mock)
        return wrap
    return patch_popen_decorator