-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_subprocess_mock.py
139 lines (111 loc) · 5.23 KB
/
test_subprocess_mock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
# Copyright 2017 Volumental AB. CONFIDENTIAL. DO NOT REDISTRIBUTE.
import subprocess
from unittest import TestCase
from nose.tools import assert_equal, raises
import subprocess_mock
class TestSubprocessMock(TestCase):
def test_patch_restored(self):
original = subprocess.Popen
with subprocess_mock.patch_subprocess():
pass
assert_equal(original, subprocess.Popen)
def test_check_call_success(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['ls', '-l'], returncode=0)
subprocess.check_call(['ls', '-l'])
@raises(subprocess.CalledProcessError)
def test_check_call_failure(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['ls', '-l'], returncode=-1)
subprocess.check_call(['ls', '-l'])
@raises(AssertionError)
def test_unexpected_popen(self):
with subprocess_mock.patch_subprocess():
subprocess.check_call(['ls', '-l'])
def test_check_ouput_success(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['ls', '-l'], stdout="FOOBAR", returncode=0)
stdout = subprocess.check_output(['ls', '-l'])
assert_equal(stdout, b'FOOBAR')
@raises(subprocess.CalledProcessError)
def test_check_ouput_failure(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['ls', '-l'], returncode=-1)
subprocess.check_output(['ls', '-l'])
@raises(subprocess.TimeoutExpired) # type: ignore
def test_timeout(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['ls', '-l'], returncode=-1, duration=10)
p = subprocess.Popen(['ls', '-l'])
p.wait(9)
def test_popen_communicate(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['ls', '-l'], stdout="X", stderr="Y", returncode=0)
p = subprocess.Popen(['ls', '-l'])
stdout, stderr = p.communicate()
assert_equal(stdout, b'X')
assert_equal(stderr, b'Y')
def test_popen_communicate_universal_newlines(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['ls', '-l'], stdout="X", stderr="Y", returncode=0)
p = subprocess.Popen(['ls', '-l'], universal_newlines=True)
stdout, stderr = p.communicate()
assert_equal(stdout, "X")
assert_equal(stderr, "Y")
def test_two_expectations(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['list_serials'], returncode=0)
mock.expect(['vandra_capture'], returncode=0)
subprocess.check_call(['list_serials'])
subprocess.check_call(['vandra_capture'])
def test_regexp_match(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['cmd', '--flag=.+'], returncode=0)
subprocess.check_call(['cmd', '--flag=YES'])
@raises(AssertionError)
def test_regexp_no_match(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['cmd', '--flag=.+'], returncode=0)
subprocess.check_call(['cmd', '--wrong=YES'])
@raises(AssertionError)
def test_too_few_args(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['cmd', '--flag=.+'], returncode=0)
subprocess.check_call(['cmd'])
@raises(AssertionError)
def test_too_many_args(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['cmd', '--flag=.+'], returncode=0)
subprocess.check_call(['cmd', '--flag=YES', '--unexpected'])
def test_string_command(self):
with subprocess_mock.patch_subprocess() as mock:
mock.expect('just_a_string', returncode=0)
subprocess.check_call('just_a_string')
def test_side_effect_stdout(self):
def side_effect(argv, stdin, stdout, stderr):
print("OH HI THERE!", file=stdout)
return 0
with subprocess_mock.patch_subprocess() as mock:
mock.expect('foo', side_effect=side_effect)
assert_equal(subprocess.check_output('foo'), b'OH HI THERE!\n')
def test_side_effect_returncode(self):
def side_effect(argv, stdin, stdout, stderr):
return 17
with subprocess_mock.patch_subprocess() as mock:
mock.expect('foo', side_effect=side_effect)
assert_equal(subprocess.call('foo'), 17)
@raises(AssertionError)
def test_side_effect_bad_expectation(self):
"""It is an error to specify both side_effect and stdout, stderr or returncode"""
def side_effect(argv, stdin, stdout, stderr):
return 0
with subprocess_mock.patch_subprocess() as mock:
mock.expect('foo', side_effect=side_effect, stdout="what?")
def test_side_effect_argv(self):
def side_effect(argv, stdin, stdout, stderr):
print(argv[1], file=stdout)
return 0
with subprocess_mock.patch_subprocess() as mock:
mock.expect(['foo', '--lol'], side_effect=side_effect)
assert_equal(subprocess.check_output(['foo', '--lol']), b'--lol\n')