blob: 5875bc2e1105ec294cf3e962b7d6905e59a86c5b [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
341 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000342 # Make sure we leak no resources
Fredrik Lundh9e29fc52004-10-13 07:54:54 +0000343 max_handles = 1026 # too much for most UNIX systems
344 if mswindows:
345 max_handles = 65 # a full test is too slow on Windows
346 for i in range(max_handles):
Tim Peters3b01a702004-10-12 22:19:32 +0000347 p = subprocess.Popen([sys.executable, "-c",
348 "import sys;sys.stdout.write(sys.stdin.read())"],
349 stdin=subprocess.PIPE,
350 stdout=subprocess.PIPE,
351 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000352 data = p.communicate("lime")[0]
353 self.assertEqual(data, "lime")
354
355
356 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000357 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
358 '"a b c" d e')
359 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
360 'ab\\"c \\ d')
361 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
362 'a\\\\\\b "de fg" h')
363 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
364 'a\\\\\\"b c d')
365 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
366 '"a\\\\b c" d e')
367 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
368 '"a\\\\b\\ c" d e')
369
370
371 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000372 p = subprocess.Popen([sys.executable,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000373 "-c", "import time; time.sleep(1)"])
374 count = 0
375 while p.poll() is None:
376 time.sleep(0.1)
377 count += 1
378 # We expect that the poll loop probably went around about 10 times,
379 # but, based on system scheduling we can't control, it's possible
380 # poll() never returned None. It "should be" very rare that it
381 # didn't go around at least twice.
382 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000383 # Subsequent invocations should just return the returncode
384 self.assertEqual(p.poll(), 0)
385
386
387 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000388 p = subprocess.Popen([sys.executable,
389 "-c", "import time; time.sleep(2)"])
390 self.assertEqual(p.wait(), 0)
391 # Subsequent invocations should just return the returncode
392 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000393
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 #
395 # POSIX tests
396 #
397 if not mswindows:
398 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000399 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000400 try:
401 p = subprocess.Popen([sys.executable, "-c", ""],
402 cwd="/this/path/does/not/exist")
403 except OSError, e:
404 # The attribute child_traceback should contain "os.chdir"
405 # somewhere.
406 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
407 else:
408 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000409
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000410 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000411 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000412 p = subprocess.Popen([sys.executable,
413 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 p.wait()
415 self.assertEqual(-p.returncode, signal.SIGABRT)
416
417 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000418 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000419 p = subprocess.Popen([sys.executable, "-c",
420 'import sys,os;' \
421 'sys.stdout.write(os.getenv("FRUIT"))'],
422 stdout=subprocess.PIPE,
423 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
424 self.assertEqual(p.stdout.read(), "apple")
425
426 def test_close_fds(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000427 # Make sure we have some fds open
428 os.pipe()
429 p = subprocess.Popen([sys.executable, "-c",
430 'import sys,os;' \
431 'sys.stdout.write(str(os.dup(0)))'],
432 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000433 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000434 self.assertEqual(p.stdout.read(), "3")
435
436 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000437 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000438 f, fname = self.mkstemp()
439 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000440 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
441 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000442 os.close(f)
443 os.chmod(fname, 0700)
444 p = subprocess.Popen(fname)
445 p.wait()
446 self.assertEqual(p.returncode, 47)
447 os.remove(fname)
448
449 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000450 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000451 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000452 [sys.executable,
453 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000454 startupinfo=47)
455 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000456 [sys.executable,
457 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458 creationflags=47)
459
460 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000461 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000462 newenv = os.environ.copy()
463 newenv["FRUIT"] = "apple"
464 p = subprocess.Popen(["echo $FRUIT"], shell=1,
465 stdout=subprocess.PIPE,
466 env=newenv)
467 self.assertEqual(p.stdout.read().strip(), "apple")
468
469 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000470 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000471 newenv = os.environ.copy()
472 newenv["FRUIT"] = "apple"
473 p = subprocess.Popen("echo $FRUIT", shell=1,
474 stdout=subprocess.PIPE,
475 env=newenv)
476 self.assertEqual(p.stdout.read().strip(), "apple")
477
478 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000479 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000480 f, fname = self.mkstemp()
481 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000482 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
483 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000484 os.close(f)
485 os.chmod(fname, 0700)
486 rc = subprocess.call(fname)
487 self.assertEqual(rc, 47)
488
Tim Peterse718f612004-10-12 21:51:32 +0000489
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000490 #
491 # Windows tests
492 #
493 if mswindows:
494 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000495 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000497 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000498 STARTF_USESHOWWINDOW = 1
499 SW_MAXIMIZE = 3
500 startupinfo = subprocess.STARTUPINFO()
501 startupinfo.dwFlags = STARTF_USESHOWWINDOW
502 startupinfo.wShowWindow = SW_MAXIMIZE
503 # Since Python is a console process, it won't be affected
504 # by wShowWindow, but the argument should be silently
505 # ignored
506 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
507 startupinfo=startupinfo)
508
509 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000510 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000511 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000512 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000513 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000514 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000515 creationflags=CREATE_NEW_CONSOLE)
516
517 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000518 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000519 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000520 [sys.executable,
521 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000522 preexec_fn=lambda: 1)
523 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000524 [sys.executable,
525 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000526 close_fds=True)
527
528 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000529 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000530 newenv = os.environ.copy()
531 newenv["FRUIT"] = "physalis"
532 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000533 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000534 env=newenv)
535 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
536
537 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000538 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539 newenv = os.environ.copy()
540 newenv["FRUIT"] = "physalis"
541 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000542 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000543 env=newenv)
544 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
545
546 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000547 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000548 rc = subprocess.call(sys.executable +
549 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000550 self.assertEqual(rc, 47)
551
552
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000553def test_main():
554 test_support.run_unittest(ProcessTestCase)
555
556if __name__ == "__main__":
557 test_main()