blob: 3e89825a42f5b3fd90a7c6afe37e992297277186 [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)
219 self.assertEqual(p.stdout.read(), tmpdir)
220
221 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000222 newenv = os.environ.copy()
223 newenv["FRUIT"] = "orange"
224 p = subprocess.Popen([sys.executable, "-c",
225 'import sys,os;' \
226 'sys.stdout.write(os.getenv("FRUIT"))'],
227 stdout=subprocess.PIPE,
228 env=newenv)
229 self.assertEqual(p.stdout.read(), "orange")
230
231 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000232 p = subprocess.Popen([sys.executable, "-c",
233 'import sys,os;' \
234 'sys.stderr.write("pineapple");' \
235 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000236 stdin=subprocess.PIPE,
237 stdout=subprocess.PIPE,
238 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000239 (stdout, stderr) = p.communicate("banana")
240 self.assertEqual(stdout, "banana")
Tim Peters3761e8d2004-10-13 04:07:12 +0000241 self.assertEqual(remove_stderr_debug_decorations(stderr),
242 "pineapple")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000243
244 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000245 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000246 p = subprocess.Popen([sys.executable, "-c",
247 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000248 (stdout, stderr) = p.communicate()
249 self.assertEqual(stdout, None)
250 self.assertEqual(stderr, None)
251
252 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000253 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000254 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000255 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000256 x, y = os.pipe()
257 if mswindows:
258 pipe_buf = 512
259 else:
260 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
261 os.close(x)
262 os.close(y)
263 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000264 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000265 'sys.stdout.write(sys.stdin.read(47));' \
266 'sys.stderr.write("xyz"*%d);' \
267 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000268 stdin=subprocess.PIPE,
269 stdout=subprocess.PIPE,
270 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000271 string_to_write = "abc"*pipe_buf
272 (stdout, stderr) = p.communicate(string_to_write)
273 self.assertEqual(stdout, string_to_write)
274
275 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000276 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277 p = subprocess.Popen([sys.executable, "-c",
278 'import sys,os;' \
279 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000280 stdin=subprocess.PIPE,
281 stdout=subprocess.PIPE,
282 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000283 p.stdin.write("banana")
284 (stdout, stderr) = p.communicate("split")
285 self.assertEqual(stdout, "bananasplit")
Tim Peters3761e8d2004-10-13 04:07:12 +0000286 self.assertEqual(remove_stderr_debug_decorations(stderr), "")
Tim Peterse718f612004-10-12 21:51:32 +0000287
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000288 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000289 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000290 'import sys,os;' + SETBINARY +
291 'sys.stdout.write("line1\\n");'
292 'sys.stdout.flush();'
293 'sys.stdout.write("line2\\r");'
294 'sys.stdout.flush();'
295 'sys.stdout.write("line3\\r\\n");'
296 'sys.stdout.flush();'
297 'sys.stdout.write("line4\\r");'
298 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000300 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000301 'sys.stdout.write("\\nline6");'],
302 stdout=subprocess.PIPE,
303 universal_newlines=1)
304 stdout = p.stdout.read()
305 if hasattr(open, 'newlines'):
306 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000307 self.assertEqual(stdout,
308 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000309 else:
310 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000311 self.assertEqual(stdout,
312 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000313
314 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000315 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000316 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000317 'import sys,os;' + SETBINARY +
318 'sys.stdout.write("line1\\n");'
319 'sys.stdout.flush();'
320 'sys.stdout.write("line2\\r");'
321 'sys.stdout.flush();'
322 'sys.stdout.write("line3\\r\\n");'
323 'sys.stdout.flush();'
324 'sys.stdout.write("line4\\r");'
325 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000326 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000327 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000328 'sys.stdout.write("\\nline6");'],
329 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
330 universal_newlines=1)
331 (stdout, stderr) = p.communicate()
332 if hasattr(open, 'newlines'):
333 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000334 self.assertEqual(stdout,
335 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000336 else:
337 # Interpreter without universal newline support
338 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
339
Tim Petersf73cc972004-10-13 03:14:40 +0000340 # XXX test_no_leaking takes > a minute to run on a high-end WinXP Pro box
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000341 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000342 # Make sure we leak no resources
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000343 for i in range(1026):
Tim Peters3b01a702004-10-12 22:19:32 +0000344 p = subprocess.Popen([sys.executable, "-c",
345 "import sys;sys.stdout.write(sys.stdin.read())"],
346 stdin=subprocess.PIPE,
347 stdout=subprocess.PIPE,
348 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000349 data = p.communicate("lime")[0]
350 self.assertEqual(data, "lime")
351
352
353 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000354 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
355 '"a b c" d e')
356 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
357 'ab\\"c \\ d')
358 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
359 'a\\\\\\b "de fg" h')
360 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
361 'a\\\\\\"b c d')
362 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
363 '"a\\\\b c" d e')
364 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
365 '"a\\\\b\\ c" d e')
366
367
368 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000369 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000370 "-c", "import time; time.sleep(1)"])
371 count = 0
372 while p.poll() is None:
373 time.sleep(0.1)
374 count += 1
375 # We expect that the poll loop probably went around about 10 times,
376 # but, based on system scheduling we can't control, it's possible
377 # poll() never returned None. It "should be" very rare that it
378 # didn't go around at least twice.
379 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000380 # Subsequent invocations should just return the returncode
381 self.assertEqual(p.poll(), 0)
382
383
384 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000385 p = subprocess.Popen([sys.executable,
386 "-c", "import time; time.sleep(2)"])
387 self.assertEqual(p.wait(), 0)
388 # Subsequent invocations should just return the returncode
389 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000390
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000391 #
392 # POSIX tests
393 #
394 if not mswindows:
395 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000396 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000397 try:
398 p = subprocess.Popen([sys.executable, "-c", ""],
399 cwd="/this/path/does/not/exist")
400 except OSError, e:
401 # The attribute child_traceback should contain "os.chdir"
402 # somewhere.
403 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
404 else:
405 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000406
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000407 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000408 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000409 p = subprocess.Popen([sys.executable,
410 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000411 p.wait()
412 self.assertEqual(-p.returncode, signal.SIGABRT)
413
414 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000415 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000416 p = subprocess.Popen([sys.executable, "-c",
417 'import sys,os;' \
418 'sys.stdout.write(os.getenv("FRUIT"))'],
419 stdout=subprocess.PIPE,
420 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
421 self.assertEqual(p.stdout.read(), "apple")
422
423 def test_close_fds(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000424 # Make sure we have some fds open
425 os.pipe()
426 p = subprocess.Popen([sys.executable, "-c",
427 'import sys,os;' \
428 'sys.stdout.write(str(os.dup(0)))'],
429 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000430 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000431 self.assertEqual(p.stdout.read(), "3")
432
433 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000434 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 f, fname = self.mkstemp()
436 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000437 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
438 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000439 os.close(f)
440 os.chmod(fname, 0700)
441 p = subprocess.Popen(fname)
442 p.wait()
443 self.assertEqual(p.returncode, 47)
444 os.remove(fname)
445
446 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000447 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000448 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000449 [sys.executable,
450 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000451 startupinfo=47)
452 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000453 [sys.executable,
454 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455 creationflags=47)
456
457 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000458 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000459 newenv = os.environ.copy()
460 newenv["FRUIT"] = "apple"
461 p = subprocess.Popen(["echo $FRUIT"], shell=1,
462 stdout=subprocess.PIPE,
463 env=newenv)
464 self.assertEqual(p.stdout.read().strip(), "apple")
465
466 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000467 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000468 newenv = os.environ.copy()
469 newenv["FRUIT"] = "apple"
470 p = subprocess.Popen("echo $FRUIT", shell=1,
471 stdout=subprocess.PIPE,
472 env=newenv)
473 self.assertEqual(p.stdout.read().strip(), "apple")
474
475 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000476 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000477 f, fname = self.mkstemp()
478 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000479 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
480 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000481 os.close(f)
482 os.chmod(fname, 0700)
483 rc = subprocess.call(fname)
484 self.assertEqual(rc, 47)
485
Tim Peterse718f612004-10-12 21:51:32 +0000486
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000487 #
488 # Windows tests
489 #
490 if mswindows:
491 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000492 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000493 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000494 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495 STARTF_USESHOWWINDOW = 1
496 SW_MAXIMIZE = 3
497 startupinfo = subprocess.STARTUPINFO()
498 startupinfo.dwFlags = STARTF_USESHOWWINDOW
499 startupinfo.wShowWindow = SW_MAXIMIZE
500 # Since Python is a console process, it won't be affected
501 # by wShowWindow, but the argument should be silently
502 # ignored
503 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
504 startupinfo=startupinfo)
505
506 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000507 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000508 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000509 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000510 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000511 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000512 creationflags=CREATE_NEW_CONSOLE)
513
514 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000515 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000516 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000517 [sys.executable,
518 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000519 preexec_fn=lambda: 1)
520 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000521 [sys.executable,
522 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523 close_fds=True)
524
525 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000526 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527 newenv = os.environ.copy()
528 newenv["FRUIT"] = "physalis"
529 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000530 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000531 env=newenv)
532 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
533
534 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000535 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000536 newenv = os.environ.copy()
537 newenv["FRUIT"] = "physalis"
538 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000539 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000540 env=newenv)
541 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
542
543 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000544 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000545 rc = subprocess.call(sys.executable +
546 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000547 self.assertEqual(rc, 47)
548
549
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000550def test_main():
551 test_support.run_unittest(ProcessTestCase)
552
553if __name__ == "__main__":
554 test_main()