blob: c4bc3d0ed4023d169f249450c0ef25537549ca90 [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 Peters29b6b4f2004-10-13 03:43:40 +000059 p = subprocess.Popen([sys.executable, "-c",
Tim Peters4052fe52004-10-13 03:29:54 +000060 '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,
Tim Peters29b6b4f2004-10-13 03:43:40 +0000353 "-c", "import time; time.sleep(1)"])
354 count = 0
355 while p.poll() is None:
356 time.sleep(0.1)
357 count += 1
358 # We expect that the poll loop probably went around about 10 times,
359 # but, based on system scheduling we can't control, it's possible
360 # poll() never returned None. It "should be" very rare that it
361 # didn't go around at least twice.
362 self.assert_(count >= 2)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000363 # Subsequent invocations should just return the returncode
364 self.assertEqual(p.poll(), 0)
365
366
367 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000368 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):
Tim Peters7b759da2004-10-12 22:29:54 +0000379 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000380 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):
Tim Peters7b759da2004-10-12 22:29:54 +0000391 # 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):
Tim Peters7b759da2004-10-12 22:29:54 +0000398 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000399 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):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000407 # Make sure we have some fds open
408 os.pipe()
409 p = subprocess.Popen([sys.executable, "-c",
410 'import sys,os;' \
411 'sys.stdout.write(str(os.dup(0)))'],
412 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000413 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414 self.assertEqual(p.stdout.read(), "3")
415
416 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000417 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000418 f, fname = self.mkstemp()
419 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000420 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
421 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000422 os.close(f)
423 os.chmod(fname, 0700)
424 p = subprocess.Popen(fname)
425 p.wait()
426 self.assertEqual(p.returncode, 47)
427 os.remove(fname)
428
429 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000430 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000431 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000432 [sys.executable,
433 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000434 startupinfo=47)
435 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000436 [sys.executable,
437 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000438 creationflags=47)
439
440 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000441 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000442 newenv = os.environ.copy()
443 newenv["FRUIT"] = "apple"
444 p = subprocess.Popen(["echo $FRUIT"], shell=1,
445 stdout=subprocess.PIPE,
446 env=newenv)
447 self.assertEqual(p.stdout.read().strip(), "apple")
448
449 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000450 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000451 newenv = os.environ.copy()
452 newenv["FRUIT"] = "apple"
453 p = subprocess.Popen("echo $FRUIT", shell=1,
454 stdout=subprocess.PIPE,
455 env=newenv)
456 self.assertEqual(p.stdout.read().strip(), "apple")
457
458 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000459 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000460 f, fname = self.mkstemp()
461 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000462 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
463 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000464 os.close(f)
465 os.chmod(fname, 0700)
466 rc = subprocess.call(fname)
467 self.assertEqual(rc, 47)
468
Tim Peterse718f612004-10-12 21:51:32 +0000469
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000470 #
471 # Windows tests
472 #
473 if mswindows:
474 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000475 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000476 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000477 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478 STARTF_USESHOWWINDOW = 1
479 SW_MAXIMIZE = 3
480 startupinfo = subprocess.STARTUPINFO()
481 startupinfo.dwFlags = STARTF_USESHOWWINDOW
482 startupinfo.wShowWindow = SW_MAXIMIZE
483 # Since Python is a console process, it won't be affected
484 # by wShowWindow, but the argument should be silently
485 # ignored
486 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
487 startupinfo=startupinfo)
488
489 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000490 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000491 CREATE_NEW_CONSOLE = 16
Tim Peters876c4322004-10-13 03:21:35 +0000492 sys.stderr.write(" a DOS box should flash briefly ...\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000493 subprocess.call(sys.executable +
Tim Peters876c4322004-10-13 03:21:35 +0000494 ' -c "import time; time.sleep(0.25)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000495 creationflags=CREATE_NEW_CONSOLE)
496
497 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000498 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000499 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):
Tim Peters7b759da2004-10-12 22:29:54 +0000509 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000510 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):
Tim Peters7b759da2004-10-12 22:29:54 +0000518 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000519 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):
Tim Peters7b759da2004-10-12 22:29:54 +0000527 # 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()