blob: ec8afa123d2172db14e0be0c3fb6711ffdab2efe [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
9
10mswindows = (sys.platform == "win32")
11
12#
13# Depends on the following external programs: Python
14#
15
16if mswindows:
Tim Peters3b01a702004-10-12 22:19:32 +000017 SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), '
18 'os.O_BINARY);')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000019else:
20 SETBINARY = ''
21
22class ProcessTestCase(unittest.TestCase):
23 def mkstemp(self):
24 """wrapper for mkstemp, calling mktemp if mkstemp is not available"""
25 if hasattr(tempfile, "mkstemp"):
26 return tempfile.mkstemp()
27 else:
28 fname = tempfile.mktemp()
29 return os.open(fname, os.O_RDWR|os.O_CREAT), fname
Tim Peterse718f612004-10-12 21:51:32 +000030
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000031 #
32 # Generic tests
33 #
34 def test_call_seq(self):
35 """call() function with sequence argument"""
Tim Peters3b01a702004-10-12 22:19:32 +000036 rc = subprocess.call([sys.executable, "-c",
37 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000038 self.assertEqual(rc, 47)
39
40 def test_call_kwargs(self):
41 """call() function with keyword args"""
42 newenv = os.environ.copy()
43 newenv["FRUIT"] = "banana"
44 rc = subprocess.call([sys.executable, "-c",
45 'import sys, os;' \
46 'sys.exit(os.getenv("FRUIT")=="banana")'],
47 env=newenv)
48 self.assertEqual(rc, 1)
49
50 def test_stdin_none(self):
51 """.stdin is None when not redirected"""
52 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
53 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
54 p.wait()
55 self.assertEqual(p.stdin, None)
56
57 def test_stdout_none(self):
58 """.stdout is None when not redirected"""
59 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
60 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
61 p.wait()
62 self.assertEqual(p.stdout, None)
63
64 def test_stderr_none(self):
65 """.stderr is None when not redirected"""
66 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
67 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
68 p.wait()
69 self.assertEqual(p.stderr, None)
70
71 def test_executable(self):
72 """executable"""
Tim Peters3b01a702004-10-12 22:19:32 +000073 p = subprocess.Popen(["somethingyoudonthave",
74 "-c", "import sys; sys.exit(47)"],
75 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000076 p.wait()
77 self.assertEqual(p.returncode, 47)
78
79 def test_stdin_pipe(self):
80 """stdin redirection"""
81 p = subprocess.Popen([sys.executable, "-c",
82 'import sys; sys.exit(sys.stdin.read() == "pear")'],
83 stdin=subprocess.PIPE)
84 p.stdin.write("pear")
85 p.stdin.close()
86 p.wait()
87 self.assertEqual(p.returncode, 1)
88
89 def test_stdin_filedes(self):
90 """stdin is set to open file descriptor"""
Tim Peterse718f612004-10-12 21:51:32 +000091 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000092 d = tf.fileno()
93 os.write(d, "pear")
94 os.lseek(d, 0, 0)
95 p = subprocess.Popen([sys.executable, "-c",
96 'import sys; sys.exit(sys.stdin.read() == "pear")'],
97 stdin=d)
98 p.wait()
99 self.assertEqual(p.returncode, 1)
100
101 def test_stdin_fileobj(self):
102 """stdin is set to open file object"""
103 tf = tempfile.TemporaryFile()
104 tf.write("pear")
105 tf.seek(0)
106 p = subprocess.Popen([sys.executable, "-c",
107 'import sys; sys.exit(sys.stdin.read() == "pear")'],
108 stdin=tf)
109 p.wait()
110 self.assertEqual(p.returncode, 1)
111
112 def test_stdout_pipe(self):
113 """stdout redirection"""
114 p = subprocess.Popen([sys.executable, "-c",
115 'import sys; sys.stdout.write("orange")'],
116 stdout=subprocess.PIPE)
117 self.assertEqual(p.stdout.read(), "orange")
118
119 def test_stdout_filedes(self):
120 """stdout is set to open file descriptor"""
Tim Peterse718f612004-10-12 21:51:32 +0000121 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000122 d = tf.fileno()
123 p = subprocess.Popen([sys.executable, "-c",
124 'import sys; sys.stdout.write("orange")'],
125 stdout=d)
126 p.wait()
127 os.lseek(d, 0, 0)
128 self.assertEqual(os.read(d, 1024), "orange")
129
130 def test_stdout_fileobj(self):
131 """stdout is set to open file object"""
Tim Peterse718f612004-10-12 21:51:32 +0000132 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000133 p = subprocess.Popen([sys.executable, "-c",
134 'import sys; sys.stdout.write("orange")'],
135 stdout=tf)
136 p.wait()
137 tf.seek(0)
138 self.assertEqual(tf.read(), "orange")
139
140 def test_stderr_pipe(self):
141 """stderr redirection"""
142 p = subprocess.Popen([sys.executable, "-c",
143 'import sys; sys.stderr.write("strawberry")'],
144 stderr=subprocess.PIPE)
145 self.assertEqual(p.stderr.read(), "strawberry")
146
147 def test_stderr_filedes(self):
148 """stderr is set to open file descriptor"""
Tim Peterse718f612004-10-12 21:51:32 +0000149 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000150 d = tf.fileno()
151 p = subprocess.Popen([sys.executable, "-c",
152 'import sys; sys.stderr.write("strawberry")'],
153 stderr=d)
154 p.wait()
155 os.lseek(d, 0, 0)
156 self.assertEqual(os.read(d, 1024), "strawberry")
157
158 def test_stderr_fileobj(self):
159 """stderr is set to open file object"""
Tim Peterse718f612004-10-12 21:51:32 +0000160 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000161 p = subprocess.Popen([sys.executable, "-c",
162 'import sys; sys.stderr.write("strawberry")'],
163 stderr=tf)
164 p.wait()
165 tf.seek(0)
166 self.assertEqual(tf.read(), "strawberry")
167
168 def test_stdout_stderr_pipe(self):
169 """capture stdout and stderr to the same pipe"""
170 p = subprocess.Popen([sys.executable, "-c",
171 'import sys;' \
172 'sys.stdout.write("apple");' \
173 'sys.stdout.flush();' \
174 'sys.stderr.write("orange")'],
175 stdout=subprocess.PIPE,
176 stderr=subprocess.STDOUT)
177 self.assertEqual(p.stdout.read(), "appleorange")
178
179 def test_stdout_stderr_file(self):
180 """capture stdout and stderr to the same open file"""
181 tf = tempfile.TemporaryFile()
182 p = subprocess.Popen([sys.executable, "-c",
183 'import sys;' \
184 'sys.stdout.write("apple");' \
185 'sys.stdout.flush();' \
186 'sys.stderr.write("orange")'],
187 stdout=tf,
188 stderr=tf)
189 p.wait()
190 tf.seek(0)
191 self.assertEqual(tf.read(), "appleorange")
192
193 def test_cwd(self):
194 """cwd"""
195 tmpdir = os.getenv("TEMP", "/tmp")
196 tmpdir = os.path.realpath(tmpdir)
197 p = subprocess.Popen([sys.executable, "-c",
198 'import sys,os;' \
199 'sys.stdout.write(os.getcwd())'],
200 stdout=subprocess.PIPE,
201 cwd=tmpdir)
202 self.assertEqual(p.stdout.read(), tmpdir)
203
204 def test_env(self):
205 """env"""
206 newenv = os.environ.copy()
207 newenv["FRUIT"] = "orange"
208 p = subprocess.Popen([sys.executable, "-c",
209 'import sys,os;' \
210 'sys.stdout.write(os.getenv("FRUIT"))'],
211 stdout=subprocess.PIPE,
212 env=newenv)
213 self.assertEqual(p.stdout.read(), "orange")
214
215 def test_communicate(self):
216 """communicate()"""
217 p = subprocess.Popen([sys.executable, "-c",
218 'import sys,os;' \
219 'sys.stderr.write("pineapple");' \
220 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000221 stdin=subprocess.PIPE,
222 stdout=subprocess.PIPE,
223 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000224 (stdout, stderr) = p.communicate("banana")
225 self.assertEqual(stdout, "banana")
226 self.assertEqual(stderr, "pineapple")
227
228 def test_communicate_returns(self):
229 """communicate() should return None if no redirection is active"""
Tim Peters3b01a702004-10-12 22:19:32 +0000230 p = subprocess.Popen([sys.executable, "-c",
231 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000232 (stdout, stderr) = p.communicate()
233 self.assertEqual(stdout, None)
234 self.assertEqual(stderr, None)
235
236 def test_communicate_pipe_buf(self):
237 """communicate() with writes larger than pipe_buf"""
238 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000239 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000240 x, y = os.pipe()
241 if mswindows:
242 pipe_buf = 512
243 else:
244 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
245 os.close(x)
246 os.close(y)
247 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000248 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000249 'sys.stdout.write(sys.stdin.read(47));' \
250 'sys.stderr.write("xyz"*%d);' \
251 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000252 stdin=subprocess.PIPE,
253 stdout=subprocess.PIPE,
254 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000255 string_to_write = "abc"*pipe_buf
256 (stdout, stderr) = p.communicate(string_to_write)
257 self.assertEqual(stdout, string_to_write)
258
259 def test_writes_before_communicate(self):
260 """stdin.write before communicate()"""
261 p = subprocess.Popen([sys.executable, "-c",
262 'import sys,os;' \
263 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000264 stdin=subprocess.PIPE,
265 stdout=subprocess.PIPE,
266 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000267 p.stdin.write("banana")
268 (stdout, stderr) = p.communicate("split")
269 self.assertEqual(stdout, "bananasplit")
270 self.assertEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000271
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000272 def test_universal_newlines(self):
273 """universal newlines"""
274 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000275 'import sys,os;' + SETBINARY +
276 'sys.stdout.write("line1\\n");'
277 'sys.stdout.flush();'
278 'sys.stdout.write("line2\\r");'
279 'sys.stdout.flush();'
280 'sys.stdout.write("line3\\r\\n");'
281 'sys.stdout.flush();'
282 'sys.stdout.write("line4\\r");'
283 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000284 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000285 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000286 'sys.stdout.write("\\nline6");'],
287 stdout=subprocess.PIPE,
288 universal_newlines=1)
289 stdout = p.stdout.read()
290 if hasattr(open, 'newlines'):
291 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000292 self.assertEqual(stdout,
293 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000294 else:
295 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000296 self.assertEqual(stdout,
297 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000298
299 def test_universal_newlines_communicate(self):
300 """universal newlines through communicate()"""
301 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000302 'import sys,os;' + SETBINARY +
303 'sys.stdout.write("line1\\n");'
304 'sys.stdout.flush();'
305 'sys.stdout.write("line2\\r");'
306 'sys.stdout.flush();'
307 'sys.stdout.write("line3\\r\\n");'
308 'sys.stdout.flush();'
309 'sys.stdout.write("line4\\r");'
310 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000311 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000312 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000313 'sys.stdout.write("\\nline6");'],
314 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
315 universal_newlines=1)
316 (stdout, stderr) = p.communicate()
317 if hasattr(open, 'newlines'):
318 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000319 self.assertEqual(stdout,
320 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000321 else:
322 # Interpreter without universal newline support
323 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
324
325 def test_no_leaking(self):
326 """Make sure we leak no resources"""
327 for i in range(1026):
Tim Peters3b01a702004-10-12 22:19:32 +0000328 p = subprocess.Popen([sys.executable, "-c",
329 "import sys;sys.stdout.write(sys.stdin.read())"],
330 stdin=subprocess.PIPE,
331 stdout=subprocess.PIPE,
332 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333 data = p.communicate("lime")[0]
334 self.assertEqual(data, "lime")
335
336
337 def test_list2cmdline(self):
338 """list2cmdline"""
339
340 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
341 '"a b c" d e')
342 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
343 'ab\\"c \\ d')
344 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
345 'a\\\\\\b "de fg" h')
346 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
347 'a\\\\\\"b c d')
348 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
349 '"a\\\\b c" d e')
350 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
351 '"a\\\\b\\ c" d e')
352
353
354 def test_poll(self):
355 """poll"""
356 p = subprocess.Popen([sys.executable,
357 "-c", "import time; time.sleep(4)"])
358 while p.poll() == None:
359 sys.stdout.write(".")
360 sys.stdout.flush()
361 time.sleep(0.5)
362 # Subsequent invocations should just return the returncode
363 self.assertEqual(p.poll(), 0)
364
365
366 def test_wait(self):
367 """wait"""
368 p = subprocess.Popen([sys.executable,
369 "-c", "import time; time.sleep(2)"])
370 self.assertEqual(p.wait(), 0)
371 # Subsequent invocations should just return the returncode
372 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000373
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000374 #
375 # POSIX tests
376 #
377 if not mswindows:
378 def test_exceptions(self):
379 """catched & re-raised exceptions"""
380 try:
381 p = subprocess.Popen([sys.executable, "-c", ""],
382 cwd="/this/path/does/not/exist")
383 except OSError, e:
384 # The attribute child_traceback should contain "os.chdir"
385 # somewhere.
386 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
387 else:
388 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000389
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000390 def test_run_abort(self):
391 """returncode handles signal termination"""
Tim Peters3b01a702004-10-12 22:19:32 +0000392 p = subprocess.Popen([sys.executable,
393 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 p.wait()
395 self.assertEqual(-p.returncode, signal.SIGABRT)
396
397 def test_preexec(self):
398 """preexec function"""
399 p = subprocess.Popen([sys.executable, "-c",
400 'import sys,os;' \
401 'sys.stdout.write(os.getenv("FRUIT"))'],
402 stdout=subprocess.PIPE,
403 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
404 self.assertEqual(p.stdout.read(), "apple")
405
406 def test_close_fds(self):
407 """close_fds"""
408 # Make sure we have some fds open
409 os.pipe()
410 p = subprocess.Popen([sys.executable, "-c",
411 'import sys,os;' \
412 'sys.stdout.write(str(os.dup(0)))'],
413 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000414 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000415 self.assertEqual(p.stdout.read(), "3")
416
417 def test_args_string(self):
418 """args is a string"""
419 f, fname = self.mkstemp()
420 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000421 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
422 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000423 os.close(f)
424 os.chmod(fname, 0700)
425 p = subprocess.Popen(fname)
426 p.wait()
427 self.assertEqual(p.returncode, 47)
428 os.remove(fname)
429
430 def test_invalid_args(self):
431 """invalid arguments should raise ValueError"""
432 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000433 [sys.executable,
434 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435 startupinfo=47)
436 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000437 [sys.executable,
438 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000439 creationflags=47)
440
441 def test_shell_sequence(self):
442 """Run command through the shell (sequence)"""
443 newenv = os.environ.copy()
444 newenv["FRUIT"] = "apple"
445 p = subprocess.Popen(["echo $FRUIT"], shell=1,
446 stdout=subprocess.PIPE,
447 env=newenv)
448 self.assertEqual(p.stdout.read().strip(), "apple")
449
450 def test_shell_string(self):
451 """Run command through the shell (string)"""
452 newenv = os.environ.copy()
453 newenv["FRUIT"] = "apple"
454 p = subprocess.Popen("echo $FRUIT", shell=1,
455 stdout=subprocess.PIPE,
456 env=newenv)
457 self.assertEqual(p.stdout.read().strip(), "apple")
458
459 def test_call_string(self):
460 """call() function with string argument on UNIX"""
461 f, fname = self.mkstemp()
462 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000463 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
464 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000465 os.close(f)
466 os.chmod(fname, 0700)
467 rc = subprocess.call(fname)
468 self.assertEqual(rc, 47)
469
Tim Peterse718f612004-10-12 21:51:32 +0000470
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000471 #
472 # Windows tests
473 #
474 if mswindows:
475 def test_startupinfo(self):
476 """startupinfo argument"""
477 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000478 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000479 STARTF_USESHOWWINDOW = 1
480 SW_MAXIMIZE = 3
481 startupinfo = subprocess.STARTUPINFO()
482 startupinfo.dwFlags = STARTF_USESHOWWINDOW
483 startupinfo.wShowWindow = SW_MAXIMIZE
484 # Since Python is a console process, it won't be affected
485 # by wShowWindow, but the argument should be silently
486 # ignored
487 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
488 startupinfo=startupinfo)
489
490 def test_creationflags(self):
491 """creationflags argument"""
492 CREATE_NEW_CONSOLE = 16
Tim Peters3b01a702004-10-12 22:19:32 +0000493 subprocess.call(sys.executable +
494 ' -c "import time; time.sleep(2)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495 creationflags=CREATE_NEW_CONSOLE)
496
497 def test_invalid_args(self):
498 """invalid arguments should raise ValueError"""
499 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000500 [sys.executable,
501 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000502 preexec_fn=lambda: 1)
503 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000504 [sys.executable,
505 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000506 close_fds=True)
507
508 def test_shell_sequence(self):
509 """Run command through the shell (sequence)"""
510 newenv = os.environ.copy()
511 newenv["FRUIT"] = "physalis"
512 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000513 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000514 env=newenv)
515 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
516
517 def test_shell_string(self):
518 """Run command through the shell (string)"""
519 newenv = os.environ.copy()
520 newenv["FRUIT"] = "physalis"
521 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000522 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523 env=newenv)
524 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
525
526 def test_call_string(self):
527 """call() function with string argument on Windows"""
Tim Peters3b01a702004-10-12 22:19:32 +0000528 rc = subprocess.call(sys.executable +
529 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000530 self.assertEqual(rc, 47)
531
532
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000533def test_main():
534 test_support.run_unittest(ProcessTestCase)
535
536if __name__ == "__main__":
537 test_main()