blob: 6f498b822f27c6d3b09edef1b46175542f076ebb [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):
Tim Peters7b759da2004-10-12 22:29:54 +000035 # 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):
Tim Peters7b759da2004-10-12 22:29:54 +000041 # call() function with keyword args
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000042 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):
Tim Peters7b759da2004-10-12 22:29:54 +000051 # .stdin is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000052 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):
Tim Peters7b759da2004-10-12 22:29:54 +000058 # .stdout is None when not redirected
Tim Peters4052fe52004-10-13 03:29:54 +000059 p = subprocess.Popen([sys.executable, "-c",
60 'print " this bit of output is from a '
61 'test of stdout in a different '
62 'process ..."'],
63 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000064 p.wait()
65 self.assertEqual(p.stdout, None)
66
67 def test_stderr_none(self):
Tim Peters7b759da2004-10-12 22:29:54 +000068 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000069 p = subprocess.Popen([sys.executable, "-c", 'print "banana"'],
70 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
71 p.wait()
72 self.assertEqual(p.stderr, None)
73
74 def test_executable(self):
Tim Peters3b01a702004-10-12 22:19:32 +000075 p = subprocess.Popen(["somethingyoudonthave",
76 "-c", "import sys; sys.exit(47)"],
77 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000078 p.wait()
79 self.assertEqual(p.returncode, 47)
80
81 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +000082 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000083 p = subprocess.Popen([sys.executable, "-c",
84 'import sys; sys.exit(sys.stdin.read() == "pear")'],
85 stdin=subprocess.PIPE)
86 p.stdin.write("pear")
87 p.stdin.close()
88 p.wait()
89 self.assertEqual(p.returncode, 1)
90
91 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +000092 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +000093 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000094 d = tf.fileno()
95 os.write(d, "pear")
96 os.lseek(d, 0, 0)
97 p = subprocess.Popen([sys.executable, "-c",
98 'import sys; sys.exit(sys.stdin.read() == "pear")'],
99 stdin=d)
100 p.wait()
101 self.assertEqual(p.returncode, 1)
102
103 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000104 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000105 tf = tempfile.TemporaryFile()
106 tf.write("pear")
107 tf.seek(0)
108 p = subprocess.Popen([sys.executable, "-c",
109 'import sys; sys.exit(sys.stdin.read() == "pear")'],
110 stdin=tf)
111 p.wait()
112 self.assertEqual(p.returncode, 1)
113
114 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000115 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000116 p = subprocess.Popen([sys.executable, "-c",
117 'import sys; sys.stdout.write("orange")'],
118 stdout=subprocess.PIPE)
119 self.assertEqual(p.stdout.read(), "orange")
120
121 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000122 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000123 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000124 d = tf.fileno()
125 p = subprocess.Popen([sys.executable, "-c",
126 'import sys; sys.stdout.write("orange")'],
127 stdout=d)
128 p.wait()
129 os.lseek(d, 0, 0)
130 self.assertEqual(os.read(d, 1024), "orange")
131
132 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000133 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000134 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000135 p = subprocess.Popen([sys.executable, "-c",
136 'import sys; sys.stdout.write("orange")'],
137 stdout=tf)
138 p.wait()
139 tf.seek(0)
140 self.assertEqual(tf.read(), "orange")
141
142 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000143 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000144 p = subprocess.Popen([sys.executable, "-c",
145 'import sys; sys.stderr.write("strawberry")'],
146 stderr=subprocess.PIPE)
147 self.assertEqual(p.stderr.read(), "strawberry")
148
149 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000150 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000151 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000152 d = tf.fileno()
153 p = subprocess.Popen([sys.executable, "-c",
154 'import sys; sys.stderr.write("strawberry")'],
155 stderr=d)
156 p.wait()
157 os.lseek(d, 0, 0)
158 self.assertEqual(os.read(d, 1024), "strawberry")
159
160 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000161 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000162 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000163 p = subprocess.Popen([sys.executable, "-c",
164 'import sys; sys.stderr.write("strawberry")'],
165 stderr=tf)
166 p.wait()
167 tf.seek(0)
168 self.assertEqual(tf.read(), "strawberry")
169
170 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000171 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000172 p = subprocess.Popen([sys.executable, "-c",
173 'import sys;' \
174 'sys.stdout.write("apple");' \
175 'sys.stdout.flush();' \
176 'sys.stderr.write("orange")'],
177 stdout=subprocess.PIPE,
178 stderr=subprocess.STDOUT)
179 self.assertEqual(p.stdout.read(), "appleorange")
180
181 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000182 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000183 tf = tempfile.TemporaryFile()
184 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=tf,
190 stderr=tf)
191 p.wait()
192 tf.seek(0)
193 self.assertEqual(tf.read(), "appleorange")
194
195 def test_cwd(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000196 tmpdir = os.getenv("TEMP", "/tmp")
197 tmpdir = os.path.realpath(tmpdir)
198 p = subprocess.Popen([sys.executable, "-c",
199 'import sys,os;' \
200 'sys.stdout.write(os.getcwd())'],
201 stdout=subprocess.PIPE,
202 cwd=tmpdir)
203 self.assertEqual(p.stdout.read(), tmpdir)
204
205 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000206 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):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000216 p = subprocess.Popen([sys.executable, "-c",
217 'import sys,os;' \
218 'sys.stderr.write("pineapple");' \
219 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000220 stdin=subprocess.PIPE,
221 stdout=subprocess.PIPE,
222 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000223 (stdout, stderr) = p.communicate("banana")
224 self.assertEqual(stdout, "banana")
225 self.assertEqual(stderr, "pineapple")
226
227 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000228 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000229 p = subprocess.Popen([sys.executable, "-c",
230 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000231 (stdout, stderr) = p.communicate()
232 self.assertEqual(stdout, None)
233 self.assertEqual(stderr, None)
234
235 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000236 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000237 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000238 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000239 x, y = os.pipe()
240 if mswindows:
241 pipe_buf = 512
242 else:
243 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
244 os.close(x)
245 os.close(y)
246 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000247 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000248 'sys.stdout.write(sys.stdin.read(47));' \
249 'sys.stderr.write("xyz"*%d);' \
250 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000251 stdin=subprocess.PIPE,
252 stdout=subprocess.PIPE,
253 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000254 string_to_write = "abc"*pipe_buf
255 (stdout, stderr) = p.communicate(string_to_write)
256 self.assertEqual(stdout, string_to_write)
257
258 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000259 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000260 p = subprocess.Popen([sys.executable, "-c",
261 'import sys,os;' \
262 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000263 stdin=subprocess.PIPE,
264 stdout=subprocess.PIPE,
265 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000266 p.stdin.write("banana")
267 (stdout, stderr) = p.communicate("split")
268 self.assertEqual(stdout, "bananasplit")
269 self.assertEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000270
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000271 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000272 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000273 'import sys,os;' + SETBINARY +
274 'sys.stdout.write("line1\\n");'
275 'sys.stdout.flush();'
276 'sys.stdout.write("line2\\r");'
277 'sys.stdout.flush();'
278 'sys.stdout.write("line3\\r\\n");'
279 'sys.stdout.flush();'
280 'sys.stdout.write("line4\\r");'
281 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000282 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000283 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000284 'sys.stdout.write("\\nline6");'],
285 stdout=subprocess.PIPE,
286 universal_newlines=1)
287 stdout = p.stdout.read()
288 if hasattr(open, 'newlines'):
289 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000290 self.assertEqual(stdout,
291 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000292 else:
293 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000294 self.assertEqual(stdout,
295 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000296
297 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000298 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000300 'import sys,os;' + SETBINARY +
301 'sys.stdout.write("line1\\n");'
302 'sys.stdout.flush();'
303 'sys.stdout.write("line2\\r");'
304 'sys.stdout.flush();'
305 'sys.stdout.write("line3\\r\\n");'
306 'sys.stdout.flush();'
307 'sys.stdout.write("line4\\r");'
308 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000309 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000310 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000311 'sys.stdout.write("\\nline6");'],
312 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
313 universal_newlines=1)
314 (stdout, stderr) = p.communicate()
315 if hasattr(open, 'newlines'):
316 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000317 self.assertEqual(stdout,
318 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000319 else:
320 # Interpreter without universal newline support
321 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
322
Tim Petersf73cc972004-10-13 03:14:40 +0000323 # XXX test_no_leaking takes > a minute to run on a high-end WinXP Pro box
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000324 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000325 # Make sure we leak no resources
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000326 for i in range(1026):
Tim Peters3b01a702004-10-12 22:19:32 +0000327 p = subprocess.Popen([sys.executable, "-c",
328 "import sys;sys.stdout.write(sys.stdin.read())"],
329 stdin=subprocess.PIPE,
330 stdout=subprocess.PIPE,
331 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000332 data = p.communicate("lime")[0]
333 self.assertEqual(data, "lime")
334
335
336 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000337 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
338 '"a b c" d e')
339 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
340 'ab\\"c \\ d')
341 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
342 'a\\\\\\b "de fg" h')
343 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
344 'a\\\\\\"b c d')
345 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
346 '"a\\\\b c" d e')
347 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
348 '"a\\\\b\\ c" d e')
349
350
351 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000352 p = subprocess.Popen([sys.executable,
353 "-c", "import time; time.sleep(4)"])
354 while p.poll() == None:
355 sys.stdout.write(".")
356 sys.stdout.flush()
357 time.sleep(0.5)
358 # Subsequent invocations should just return the returncode
359 self.assertEqual(p.poll(), 0)
360
361
362 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000363 p = subprocess.Popen([sys.executable,
364 "-c", "import time; time.sleep(2)"])
365 self.assertEqual(p.wait(), 0)
366 # Subsequent invocations should just return the returncode
367 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000368
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000369 #
370 # POSIX tests
371 #
372 if not mswindows:
373 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000374 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000375 try:
376 p = subprocess.Popen([sys.executable, "-c", ""],
377 cwd="/this/path/does/not/exist")
378 except OSError, e:
379 # The attribute child_traceback should contain "os.chdir"
380 # somewhere.
381 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
382 else:
383 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000384
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000385 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000386 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000387 p = subprocess.Popen([sys.executable,
388 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000389 p.wait()
390 self.assertEqual(-p.returncode, signal.SIGABRT)
391
392 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000393 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000394 p = subprocess.Popen([sys.executable, "-c",
395 'import sys,os;' \
396 'sys.stdout.write(os.getenv("FRUIT"))'],
397 stdout=subprocess.PIPE,
398 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
399 self.assertEqual(p.stdout.read(), "apple")
400
401 def test_close_fds(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000402 # Make sure we have some fds open
403 os.pipe()
404 p = subprocess.Popen([sys.executable, "-c",
405 'import sys,os;' \
406 'sys.stdout.write(str(os.dup(0)))'],
407 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000408 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000409 self.assertEqual(p.stdout.read(), "3")
410
411 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000412 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000413 f, fname = self.mkstemp()
414 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000415 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
416 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000417 os.close(f)
418 os.chmod(fname, 0700)
419 p = subprocess.Popen(fname)
420 p.wait()
421 self.assertEqual(p.returncode, 47)
422 os.remove(fname)
423
424 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000425 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000426 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000427 [sys.executable,
428 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000429 startupinfo=47)
430 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000431 [sys.executable,
432 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 creationflags=47)
434
435 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000436 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000437 newenv = os.environ.copy()
438 newenv["FRUIT"] = "apple"
439 p = subprocess.Popen(["echo $FRUIT"], shell=1,
440 stdout=subprocess.PIPE,
441 env=newenv)
442 self.assertEqual(p.stdout.read().strip(), "apple")
443
444 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000445 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000446 newenv = os.environ.copy()
447 newenv["FRUIT"] = "apple"
448 p = subprocess.Popen("echo $FRUIT", shell=1,
449 stdout=subprocess.PIPE,
450 env=newenv)
451 self.assertEqual(p.stdout.read().strip(), "apple")
452
453 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000454 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455 f, fname = self.mkstemp()
456 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000457 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
458 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000459 os.close(f)
460 os.chmod(fname, 0700)
461 rc = subprocess.call(fname)
462 self.assertEqual(rc, 47)
463
Tim Peterse718f612004-10-12 21:51:32 +0000464
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000465 #
466 # Windows tests
467 #
468 if mswindows:
469 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000470 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000471 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000472 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000473 STARTF_USESHOWWINDOW = 1
474 SW_MAXIMIZE = 3
475 startupinfo = subprocess.STARTUPINFO()
476 startupinfo.dwFlags = STARTF_USESHOWWINDOW
477 startupinfo.wShowWindow = SW_MAXIMIZE
478 # Since Python is a console process, it won't be affected
479 # by wShowWindow, but the argument should be silently
480 # ignored
481 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
482 startupinfo=startupinfo)
483
484 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000485 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000486 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000487 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000488 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000489 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000490 creationflags=CREATE_NEW_CONSOLE)
491
492 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000493 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000495 [sys.executable,
496 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000497 preexec_fn=lambda: 1)
498 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000499 [sys.executable,
500 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000501 close_fds=True)
502
503 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000504 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000505 newenv = os.environ.copy()
506 newenv["FRUIT"] = "physalis"
507 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000508 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000509 env=newenv)
510 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
511
512 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000513 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000514 newenv = os.environ.copy()
515 newenv["FRUIT"] = "physalis"
516 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000517 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000518 env=newenv)
519 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
520
521 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000522 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000523 rc = subprocess.call(sys.executable +
524 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000525 self.assertEqual(rc, 47)
526
527
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000528def test_main():
529 test_support.run_unittest(ProcessTestCase)
530
531if __name__ == "__main__":
532 test_main()