File: popen.py

package info (click to toggle)
git-buildpackage 0.9.39
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,464 kB
  • sloc: python: 18,427; xml: 8,746; sh: 731; makefile: 139
file content (18 lines) | stat: -rw-r--r-- 674 bytes parent folder | download | duplicates (2)
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
from unittest 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