blob: ded58910497670266a6272296edf310e8d7f1aa4 [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001import unittest
2from test import test_support
3import subprocess
4import sys
5import signal
6import os
7import tempfile
8import time
Tim Peters3761e8d2004-10-13 04:07:12 +00009import re
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000010
11mswindows = (sys.platform == "win32")
12
13#
14# Depends on the following external programs: Python
15#
16
17if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000018 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
19 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000020else:
21 SETBINARY = ''
22
Tim Peters3761e8d2004-10-13 04:07:12 +000023# In a debug build, stuff like "[6580 refs]" is printed to stderr at
24# shutdown time. That frustrates tests trying to check stderr produced
25# from a spawned Python process.
26def remove_stderr_debug_decorations(stderr):
27 if __debug__:
28 stderr = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
29 return stderr
30
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000031class ProcessTestCase(unittest.TestCase):
32 def mkstemp(self):
33 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
34 if hasattr(tempfile, "mkstemp"):
35 return tempfile.mkstemp()
36 else:
37 fname = tempfile.mktemp()
38 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000039
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000040 #
41 # Generic tests
42 #
43 def test_call_seq(self):
Tim Peters7b759da2004-10-12 22:29:54 +000044 # call() function with sequence argument
Tim Peters3b01a702004-10-12 22:19:32 +000045 rc = subprocess.call([sys.executable, "-c",
46 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000047 self.assertEqual(rc, 47)
48
49 def test_call_kwargs(self):
Tim Peters7b759da2004-10-12 22:29:54 +000050 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000051 newenv = os.environ.copy()
52 newenv["FRUIT"] = "banana"
53 rc = subprocess.call([sys.executable, "-c",
54 'import sys, os;' \
55 'sys.exit(os.getenv("FRUIT")=="banana")'],
56 env=newenv)
57 self.assertEqual(rc, 1)
58
59 def test_stdin_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000060 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000061 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
62 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63 p.wait()
64 self.assertEqual(p.stdin, None)
65
66 def test_stdout_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000067 # .stdout is None when not redirected
Tim Peters29b6b4f2004-10-13 03:43:40 +000068 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +000069 'print " this bit of output is from a '
70 'test of stdout in a different '
71 'process ..."'],
72 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000073 p.wait()
74 self.assertEqual(p.stdout, None)
75
76 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000077 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000078 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
79 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
80 p.wait()
81 self.assertEqual(p.stderr, None)
82
83 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +000084 p = subprocess.Popen(["somethingyoudonthave",
85 "-c", "import sys; sys.exit(47)"],
86 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000087 p.wait()
88 self.assertEqual(p.returncode, 47)
89
90 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +000091 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000092 p = subprocess.Popen([sys.executable, "-c",
93 'import sys; sys.exit(sys.stdin.read() == "pear")'],
94 stdin=subprocess.PIPE)
95 p.stdin.write("pear")
96 p.stdin.close()
97 p.wait()
98 self.assertEqual(p.returncode, 1)
99
100 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000101 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000102 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000103 d = tf.fileno()
104 os.write(d, "pear")
105 os.lseek(d, 0, 0)
106 p = subprocess.Popen([sys.executable, "-c",
107 'import sys; sys.exit(sys.stdin.read() == "pear")'],
108 stdin=d)
109 p.wait()
110 self.assertEqual(p.returncode, 1)
111
112 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000113 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000114 tf = tempfile.TemporaryFile()
115 tf.write("pear")
116 tf.seek(0)
117 p = subprocess.Popen([sys.executable, "-c",
118 'import sys; sys.exit(sys.stdin.read() == "pear")'],
119 stdin=tf)
120 p.wait()
121 self.assertEqual(p.returncode, 1)
122
123 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000124 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125 p = subprocess.Popen([sys.executable, "-c",
126 'import sys; sys.stdout.write("orange")'],
127 stdout=subprocess.PIPE)
128 self.assertEqual(p.stdout.read(), "orange")
129
130 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000131 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000132 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000133 d = tf.fileno()
134 p = subprocess.Popen([sys.executable, "-c",
135 'import sys; sys.stdout.write("orange")'],
136 stdout=d)
137 p.wait()
138 os.lseek(d, 0, 0)
139 self.assertEqual(os.read(d, 1024), "orange")
140
141 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000142 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000143 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000144 p = subprocess.Popen([sys.executable, "-c",
145 'import sys; sys.stdout.write("orange")'],
146 stdout=tf)
147 p.wait()
148 tf.seek(0)
149 self.assertEqual(tf.read(), "orange")
150
151 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000152 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000153 p = subprocess.Popen([sys.executable, "-c",
154 'import sys; sys.stderr.write("strawberry")'],
155 stderr=subprocess.PIPE)
Tim Peters3761e8d2004-10-13 04:07:12 +0000156 self.assertEqual(remove_stderr_debug_decorations(p.stderr.read()),
157 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000158
159 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000160 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000161 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000162 d = tf.fileno()
163 p = subprocess.Popen([sys.executable, "-c",
164 'import sys; sys.stderr.write("strawberry")'],
165 stderr=d)
166 p.wait()
167 os.lseek(d, 0, 0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000168 self.assertEqual(remove_stderr_debug_decorations(os.read(d, 1024)),
169 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000170
171 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000172 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000173 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000174 p = subprocess.Popen([sys.executable, "-c",
175 'import sys; sys.stderr.write("strawberry")'],
176 stderr=tf)
177 p.wait()
178 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000179 self.assertEqual(remove_stderr_debug_decorations(tf.read()),
180 "strawberry")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000181
182 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000183 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000184 p = subprocess.Popen([sys.executable, "-c",
185 'import sys;' \
186 'sys.stdout.write("apple");' \
187 'sys.stdout.flush();' \
188 'sys.stderr.write("orange")'],
189 stdout=subprocess.PIPE,
190 stderr=subprocess.STDOUT)
Tim Peters3761e8d2004-10-13 04:07:12 +0000191 output = p.stdout.read()
192 stripped = remove_stderr_debug_decorations(output)
193 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000194
195 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000196 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000197 tf = tempfile.TemporaryFile()
198 p = subprocess.Popen([sys.executable, "-c",
199 'import sys;' \
200 'sys.stdout.write("apple");' \
201 'sys.stdout.flush();' \
202 'sys.stderr.write("orange")'],
203 stdout=tf,
204 stderr=tf)
205 p.wait()
206 tf.seek(0)
Tim Peters3761e8d2004-10-13 04:07:12 +0000207 output = tf.read()
208 stripped = remove_stderr_debug_decorations(output)
209 self.assertEqual(stripped, "appleorange")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000210
211 def test_cwd(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000212 tmpdir = os.getenv("TEMP", "/tmp")
213 tmpdir = os.path.realpath(tmpdir)
214 p = subprocess.Popen([sys.executable, "-c",
215 'import sys,os;' \
216 'sys.stdout.write(os.getcwd())'],
217 stdout=subprocess.PIPE,
218 cwd=tmpdir)
Fredrik Lundh59c05592004-10-13 06:55:40 +0000219 normcase = os.path.normcase
220 self.assertEqual(normcase(p.stdout.read()), normcase(tmpdir))
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000221
222 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000223 newenv = os.environ.copy()
224 newenv["FRUIT"] = "orange"
225 p = subprocess.Popen([sys.executable, "-c",
226 'import sys,os;' \
227 'sys.stdout.write(os.getenv("FRUIT"))'],
228 stdout=subprocess.PIPE,
229 env=newenv)
230 self.assertEqual(p.stdout.read(), "orange")
231
232 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000233 p = subprocess.Popen([sys.executable, "-c",
234 'import sys,os;' \
235 'sys.stderr.write("pineapple");' \
236 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000237 stdin=subprocess.PIPE,
238 stdout=subprocess.PIPE,
239 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000240 (stdout, stderr) = p.communicate("banana")
241 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000242 self.assertEqual(remove_stderr_debug_decorations(stderr),
243 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000244
245 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000246 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000247 p = subprocess.Popen([sys.executable, "-c",
248 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000249 (stdout, stderr) = p.communicate()
250 self.assertEqual(stdout, None)
251 self.assertEqual(stderr, None)
252
253 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000254 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000255 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000256 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000257 x, y = os.pipe()
258 if mswindows:
259 pipe_buf = 512
260 else:
261 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
262 os.close(x)
263 os.close(y)
264 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000265 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000266 'sys.stdout.write(sys.stdin.read(47));' \
267 'sys.stderr.write("xyz"*%d);' \
268 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000269 stdin=subprocess.PIPE,
270 stdout=subprocess.PIPE,
271 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000272 string_to_write = "abc"*pipe_buf
273 (stdout, stderr) = p.communicate(string_to_write)
274 self.assertEqual(stdout, string_to_write)
275
276 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000277 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000278 p = subprocess.Popen([sys.executable, "-c",
279 'import sys,os;' \
280 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000281 stdin=subprocess.PIPE,
282 stdout=subprocess.PIPE,
283 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000284 p.stdin.write("banana")
285 (stdout, stderr) = p.communicate("split")
286 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000287 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000288
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000289 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000290 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000291 'import sys,os;' + SETBINARY +
292 'sys.stdout.write("line1\\n");'
293 'sys.stdout.flush();'
294 'sys.stdout.write("line2\\r");'
295 'sys.stdout.flush();'
296 'sys.stdout.write("line3\\r\\n");'
297 'sys.stdout.flush();'
298 'sys.stdout.write("line4\\r");'
299 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000300 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000301 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000302 'sys.stdout.write("\\nline6");'],
303 stdout=subprocess.PIPE,
304 universal_newlines=1)
305 stdout = p.stdout.read()
306 if hasattr(open, 'newlines'):
307 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000308 self.assertEqual(stdout,
309 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000310 else:
311 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000312 self.assertEqual(stdout,
313 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000314
315 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000316 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000317 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000318 'import sys,os;' + SETBINARY +
319 'sys.stdout.write("line1\\n");'
320 'sys.stdout.flush();'
321 'sys.stdout.write("line2\\r");'
322 'sys.stdout.flush();'
323 'sys.stdout.write("line3\\r\\n");'
324 'sys.stdout.flush();'
325 'sys.stdout.write("line4\\r");'
326 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000328 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000329 'sys.stdout.write("\\nline6");'],
330 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
331 universal_newlines=1)
332 (stdout, stderr) = p.communicate()
333 if hasattr(open, 'newlines'):
334 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000335 self.assertEqual(stdout,
336 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000337 else:
338 # Interpreter without universal newline support
339 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
340
Tim Petersf73cc972004-10-13 03:14:40 +0000341 # XXX test_no_leaking takes > a minute to run on a high-end WinXP Pro box
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000342 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000343 # Make sure we leak no resources
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344 for i in range(1026):
Tim Peters3b01a702004-10-12 22:19:32 +0000345 p = subprocess.Popen([sys.executable, "-c",
346 "import sys;sys.stdout.write(sys.stdin.read())"],
347 stdin=subprocess.PIPE,
348 stdout=subprocess.PIPE,
349 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000350 data = p.communicate("lime")[0]
351 self.assertEqual(data, "lime")
352
353
354 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
356 '"a b c" d e')
357 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
358 'ab\\"c \\ d')
359 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
360 'a\\\\\\b "de fg" h')
361 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
362 'a\\\\\\"b c d')
363 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
364 '"a\\\\b c" d e')
365 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
366 '"a\\\\b\\ c" d e')
367
368
369 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000370 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000371 "-c", "import time; time.sleep(1)"])
372 count = 0
373 while p.poll() is None:
374 time.sleep(0.1)
375 count += 1
376 # We expect that the poll loop probably went around about 10 times,
377 # but, based on system scheduling we can't control, it's possible
378 # poll() never returned None. It "should be" very rare that it
379 # didn't go around at least twice.
380 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000381 # Subsequent invocations should just return the returncode
382 self.assertEqual(p.poll(), 0)
383
384
385 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000386 p = subprocess.Popen([sys.executable,
387 "-c", "import time; time.sleep(2)"])
388 self.assertEqual(p.wait(), 0)
389 # Subsequent invocations should just return the returncode
390 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000391
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000392 #
393 # POSIX tests
394 #
395 if not mswindows:
396 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000397 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000398 try:
399 p = subprocess.Popen([sys.executable, "-c", ""],
400 cwd="/this/path/does/not/exist")
401 except OSError, e:
402 # The attribute child_traceback should contain "os.chdir"
403 # somewhere.
404 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
405 else:
406 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000407
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000408 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000409 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000410 p = subprocess.Popen([sys.executable,
411 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000412 p.wait()
413 self.assertEqual(-p.returncode, signal.SIGABRT)
414
415 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000416 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 p = subprocess.Popen([sys.executable, "-c",
418 'import sys,os;' \
419 'sys.stdout.write(os.getenv("FRUIT"))'],
420 stdout=subprocess.PIPE,
421 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
422 self.assertEqual(p.stdout.read(), "apple")
423
424 def test_close_fds(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000425 # Make sure we have some fds open
426 os.pipe()
427 p = subprocess.Popen([sys.executable, "-c",
428 'import sys,os;' \
429 'sys.stdout.write(str(os.dup(0)))'],
430 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000431 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000432 self.assertEqual(p.stdout.read(), "3")
433
434 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000435 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000436 f, fname = self.mkstemp()
437 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000438 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
439 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000440 os.close(f)
441 os.chmod(fname, 0700)
442 p = subprocess.Popen(fname)
443 p.wait()
444 self.assertEqual(p.returncode, 47)
445 os.remove(fname)
446
447 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000448 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000449 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000450 [sys.executable,
451 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000452 startupinfo=47)
453 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000454 [sys.executable,
455 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456 creationflags=47)
457
458 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000459 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000460 newenv = os.environ.copy()
461 newenv["FRUIT"] = "apple"
462 p = subprocess.Popen(["echo $FRUIT"], shell=1,
463 stdout=subprocess.PIPE,
464 env=newenv)
465 self.assertEqual(p.stdout.read().strip(), "apple")
466
467 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000468 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469 newenv = os.environ.copy()
470 newenv["FRUIT"] = "apple"
471 p = subprocess.Popen("echo $FRUIT", shell=1,
472 stdout=subprocess.PIPE,
473 env=newenv)
474 self.assertEqual(p.stdout.read().strip(), "apple")
475
476 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000477 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 f, fname = self.mkstemp()
479 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000480 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
481 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000482 os.close(f)
483 os.chmod(fname, 0700)
484 rc = subprocess.call(fname)
485 self.assertEqual(rc, 47)
486
Tim Peterse718f612004-10-12 21:51:32 +0000487
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000488 #
489 # Windows tests
490 #
491 if mswindows:
492 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000493 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000495 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 STARTF_USESHOWWINDOW = 1
497 SW_MAXIMIZE = 3
498 startupinfo = subprocess.STARTUPINFO()
499 startupinfo.dwFlags = STARTF_USESHOWWINDOW
500 startupinfo.wShowWindow = SW_MAXIMIZE
501 # Since Python is a console process, it won't be affected
502 # by wShowWindow, but the argument should be silently
503 # ignored
504 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
505 startupinfo=startupinfo)
506
507 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000508 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000509 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000510 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000511 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000512 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000513 creationflags=CREATE_NEW_CONSOLE)
514
515 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000516 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000517 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000518 [sys.executable,
519 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000520 preexec_fn=lambda: 1)
521 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000522 [sys.executable,
523 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000524 close_fds=True)
525
526 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000527 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000528 newenv = os.environ.copy()
529 newenv["FRUIT"] = "physalis"
530 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000531 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000532 env=newenv)
533 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
534
535 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000536 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000537 newenv = os.environ.copy()
538 newenv["FRUIT"] = "physalis"
539 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000540 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000541 env=newenv)
542 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
543
544 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000545 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000546 rc = subprocess.call(sys.executable +
547 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000548 self.assertEqual(rc, 47)
549
550
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000551def test_main():
552 test_support.run_unittest(ProcessTestCase)
553
554if __name__ == "__main__":
555 test_main()