-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_spm.py
executable file
·172 lines (119 loc) · 4.52 KB
/
test_spm.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# Copyright (c) 2015 Antoine Catton
# See the LICENSE.txt file.
import tempfile
import os
import unittest
import subprocess
import contextlib
import signal
import six
from spm import run, pipe, propagate_env
class TempFileMixin(object):
def setUp(self):
super(TempFileMixin, self).setUp()
self._tempfiles = []
def get_temp_filename(self):
fd, fname = tempfile.mkstemp()
os.close(fd)
return fname
def tearDown(self):
for fname in self._tempfiles:
os.remove(fname)
class DeadLockMixin(object):
@contextlib.contextmanager
def assertDoesNotHang(self, timeout=2):
def handler(signum, frame):
assert False, "Hanged for more than {} seconds".format(timeout)
signal.signal(signal.SIGALRM, handler)
try:
signal.alarm(timeout)
yield
finally:
signal.signal(signal.SIGALRM, signal.SIG_DFL)
class RunTest(TempFileMixin, unittest.TestCase):
def test_stdin_from_file(self):
content = '__file_content__'
fname = self.get_temp_filename()
with open(fname, 'w') as file_:
file_.write(content)
cat = run('cat')
cat.stdin = open(fname)
assert cat.stdout.read().decode() == content
def test_stdout_to_file(self):
string = '__output__'
fname = self.get_temp_filename()
echo = run('printf', string)
echo.stdout = open(fname, 'w')
out, err = echo.wait()
assert out is None
with open(fname) as file_:
assert six.u(file_.read()) == string
def test_environment(self):
env = run('env', env={'FOO': 'BAR'}).stdout.read().decode().split('\n')
assert 'FOO=BAR' in env
def test_empty_environment_by_default(self):
env = run('env').stdout.read().decode()
assert env == ''
def test_copy_environment_opt_in(self):
env = run('env', env=propagate_env({})).stdout.read().decode()
assert env != ''
def test_repr(self):
"""
A user should be able to run str(Subprocess) in their shell prompt.
"""
cmd_str = str(run('printf', 'foo"bar'))
output = subprocess.check_output(cmd_str, shell=True).decode()
assert output == 'foo"bar'
def test_repr_env(self):
cmd_str = str(run('env', env=propagate_env(foo='bar')))
env = subprocess.check_output(cmd_str, shell=True).decode().split('\n')
assert 'foo=bar' in env
def test_empty_env(self):
proc = run('env', env={'foo': 'bar'})
spm_run = set(proc.stdout.read().decode().split('\n'))
sh_run = set(
subprocess.check_output(str(proc), shell=True).decode().split('\n')
)
assert sh_run == spm_run
def test_subprocess_failure(self):
with self.assertRaises(subprocess.CalledProcessError):
run('false').wait()
def test_environement_on_pipe(self):
proc = pipe(['env'], ['egrep', '^FOO='], env={'FOO': 'BAR'})
assert proc.stdout.read().decode() == 'FOO=BAR\n'
def test_pass_subprocess_to_pipe(self):
proc = pipe(['printf', 'hello'], ['gzip'], run('zcat'))
assert proc.stdout.read().decode() == 'hello'
class PipeTest(DeadLockMixin, TempFileMixin, unittest.TestCase):
def test_stdin_from_file(self):
content = '__file_content__'
fname = self.get_temp_filename()
with open(fname, 'w') as file_:
file_.write(content)
cat = run('gzip').pipe('zcat')
cat.stdin = open(fname)
assert cat.stdout.read().decode() == content
def test_stdout_to_file(self):
string = '__output__'
fname = self.get_temp_filename()
echo = run('printf', string)
echo.stdout = open(fname, 'w')
out, err = echo.wait()
assert out is None
with open(fname) as file_:
assert six.u(file_.read()) == string
def test_safe_pipe_stdout_read(self):
command = pipe(['gzip'], ['zcat'])
with self.assertDoesNotHang():
assert command.stdout.read().decode() == '' # No deadlock
def test_safe_pipe_wait(self):
command = pipe(['gzip'], ['zcat'])
with self.assertDoesNotHang():
out, _ = command.wait() # No deadlock
assert out.decode() == ''
def test_failing_pipe_command(self):
with self.assertRaises(subprocess.CalledProcessError):
pipe(['true'], ['false'], ['true']).wait()
if __name__ == '__main__':
unittest.main()