blob: ba8ffa21992991a2aff7c6d6661f4db53ada8cbc [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
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000059 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):
Tim Peters7b759da2004-10-12 22:29:54 +000065 # .stderr is None when not redirected
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000066 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):
Tim Peters3b01a702004-10-12 22:19:32 +000072 p = subprocess.Popen(["somethingyoudonthave",
73 "-c", "import sys; sys.exit(47)"],
74 executable=sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000075 p.wait()
76 self.assertEqual(p.returncode, 47)
77
78 def test_stdin_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +000079 # stdin redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000080 p = subprocess.Popen([sys.executable, "-c",
81 'import sys; sys.exit(sys.stdin.read() == "pear")'],
82 stdin=subprocess.PIPE)
83 p.stdin.write("pear")
84 p.stdin.close()
85 p.wait()
86 self.assertEqual(p.returncode, 1)
87
88 def test_stdin_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +000089 # stdin is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +000090 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000091 d = tf.fileno()
92 os.write(d, "pear")
93 os.lseek(d, 0, 0)
94 p = subprocess.Popen([sys.executable, "-c",
95 'import sys; sys.exit(sys.stdin.read() == "pear")'],
96 stdin=d)
97 p.wait()
98 self.assertEqual(p.returncode, 1)
99
100 def test_stdin_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000101 # stdin is set to open file object
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000102 tf = tempfile.TemporaryFile()
103 tf.write("pear")
104 tf.seek(0)
105 p = subprocess.Popen([sys.executable, "-c",
106 'import sys; sys.exit(sys.stdin.read() == "pear")'],
107 stdin=tf)
108 p.wait()
109 self.assertEqual(p.returncode, 1)
110
111 def test_stdout_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000112 # stdout redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000113 p = subprocess.Popen([sys.executable, "-c",
114 'import sys; sys.stdout.write("orange")'],
115 stdout=subprocess.PIPE)
116 self.assertEqual(p.stdout.read(), "orange")
117
118 def test_stdout_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000119 # stdout is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000120 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000121 d = tf.fileno()
122 p = subprocess.Popen([sys.executable, "-c",
123 'import sys; sys.stdout.write("orange")'],
124 stdout=d)
125 p.wait()
126 os.lseek(d, 0, 0)
127 self.assertEqual(os.read(d, 1024), "orange")
128
129 def test_stdout_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000130 # stdout is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000131 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000132 p = subprocess.Popen([sys.executable, "-c",
133 'import sys; sys.stdout.write("orange")'],
134 stdout=tf)
135 p.wait()
136 tf.seek(0)
137 self.assertEqual(tf.read(), "orange")
138
139 def test_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000140 # stderr redirection
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000141 p = subprocess.Popen([sys.executable, "-c",
142 'import sys; sys.stderr.write("strawberry")'],
143 stderr=subprocess.PIPE)
144 self.assertEqual(p.stderr.read(), "strawberry")
145
146 def test_stderr_filedes(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000147 # stderr is set to open file descriptor
Tim Peterse718f612004-10-12 21:51:32 +0000148 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000149 d = tf.fileno()
150 p = subprocess.Popen([sys.executable, "-c",
151 'import sys; sys.stderr.write("strawberry")'],
152 stderr=d)
153 p.wait()
154 os.lseek(d, 0, 0)
155 self.assertEqual(os.read(d, 1024), "strawberry")
156
157 def test_stderr_fileobj(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000158 # stderr is set to open file object
Tim Peterse718f612004-10-12 21:51:32 +0000159 tf = tempfile.TemporaryFile()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000160 p = subprocess.Popen([sys.executable, "-c",
161 'import sys; sys.stderr.write("strawberry")'],
162 stderr=tf)
163 p.wait()
164 tf.seek(0)
165 self.assertEqual(tf.read(), "strawberry")
166
167 def test_stdout_stderr_pipe(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000168 # capture stdout and stderr to the same pipe
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000169 p = subprocess.Popen([sys.executable, "-c",
170 'import sys;' \
171 'sys.stdout.write("apple");' \
172 'sys.stdout.flush();' \
173 'sys.stderr.write("orange")'],
174 stdout=subprocess.PIPE,
175 stderr=subprocess.STDOUT)
176 self.assertEqual(p.stdout.read(), "appleorange")
177
178 def test_stdout_stderr_file(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000179 # capture stdout and stderr to the same open file
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000180 tf = tempfile.TemporaryFile()
181 p = subprocess.Popen([sys.executable, "-c",
182 'import sys;' \
183 'sys.stdout.write("apple");' \
184 'sys.stdout.flush();' \
185 'sys.stderr.write("orange")'],
186 stdout=tf,
187 stderr=tf)
188 p.wait()
189 tf.seek(0)
190 self.assertEqual(tf.read(), "appleorange")
191
192 def test_cwd(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000193 tmpdir = os.getenv("TEMP", "/tmp")
194 tmpdir = os.path.realpath(tmpdir)
195 p = subprocess.Popen([sys.executable, "-c",
196 'import sys,os;' \
197 'sys.stdout.write(os.getcwd())'],
198 stdout=subprocess.PIPE,
199 cwd=tmpdir)
200 self.assertEqual(p.stdout.read(), tmpdir)
201
202 def test_env(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000203 newenv = os.environ.copy()
204 newenv["FRUIT"] = "orange"
205 p = subprocess.Popen([sys.executable, "-c",
206 'import sys,os;' \
207 'sys.stdout.write(os.getenv("FRUIT"))'],
208 stdout=subprocess.PIPE,
209 env=newenv)
210 self.assertEqual(p.stdout.read(), "orange")
211
212 def test_communicate(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000213 p = subprocess.Popen([sys.executable, "-c",
214 'import sys,os;' \
215 'sys.stderr.write("pineapple");' \
216 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000217 stdin=subprocess.PIPE,
218 stdout=subprocess.PIPE,
219 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000220 (stdout, stderr) = p.communicate("banana")
221 self.assertEqual(stdout, "banana")
222 self.assertEqual(stderr, "pineapple")
223
224 def test_communicate_returns(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000225 # communicate() should return None if no redirection is active
Tim Peters3b01a702004-10-12 22:19:32 +0000226 p = subprocess.Popen([sys.executable, "-c",
227 "import sys; sys.exit(47)"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000228 (stdout, stderr) = p.communicate()
229 self.assertEqual(stdout, None)
230 self.assertEqual(stderr, None)
231
232 def test_communicate_pipe_buf(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000233 # communicate() with writes larger than pipe_buf
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000234 # This test will probably deadlock rather than fail, if
Tim Peterse718f612004-10-12 21:51:32 +0000235 # communicate() does not work properly.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000236 x, y = os.pipe()
237 if mswindows:
238 pipe_buf = 512
239 else:
240 pipe_buf = os.fpathconf(x, "PC_PIPE_BUF")
241 os.close(x)
242 os.close(y)
243 p = subprocess.Popen([sys.executable, "-c",
Tim Peterse718f612004-10-12 21:51:32 +0000244 'import sys,os;'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000245 'sys.stdout.write(sys.stdin.read(47));' \
246 'sys.stderr.write("xyz"*%d);' \
247 'sys.stdout.write(sys.stdin.read())' % pipe_buf],
Tim Peters3b01a702004-10-12 22:19:32 +0000248 stdin=subprocess.PIPE,
249 stdout=subprocess.PIPE,
250 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000251 string_to_write = "abc"*pipe_buf
252 (stdout, stderr) = p.communicate(string_to_write)
253 self.assertEqual(stdout, string_to_write)
254
255 def test_writes_before_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000256 # stdin.write before communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000257 p = subprocess.Popen([sys.executable, "-c",
258 'import sys,os;' \
259 'sys.stdout.write(sys.stdin.read())'],
Tim Peters3b01a702004-10-12 22:19:32 +0000260 stdin=subprocess.PIPE,
261 stdout=subprocess.PIPE,
262 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000263 p.stdin.write("banana")
264 (stdout, stderr) = p.communicate("split")
265 self.assertEqual(stdout, "bananasplit")
266 self.assertEqual(stderr, "")
Tim Peterse718f612004-10-12 21:51:32 +0000267
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000268 def test_universal_newlines(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000269 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000270 'import sys,os;' + SETBINARY +
271 'sys.stdout.write("line1\\n");'
272 'sys.stdout.flush();'
273 'sys.stdout.write("line2\\r");'
274 'sys.stdout.flush();'
275 'sys.stdout.write("line3\\r\\n");'
276 'sys.stdout.flush();'
277 'sys.stdout.write("line4\\r");'
278 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000279 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000280 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000281 'sys.stdout.write("\\nline6");'],
282 stdout=subprocess.PIPE,
283 universal_newlines=1)
284 stdout = p.stdout.read()
285 if hasattr(open, 'newlines'):
286 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000287 self.assertEqual(stdout,
288 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000289 else:
290 # Interpreter without universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000291 self.assertEqual(stdout,
292 "line1\nline2\rline3\r\nline4\r\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000293
294 def test_universal_newlines_communicate(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000295 # universal newlines through communicate()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000296 p = subprocess.Popen([sys.executable, "-c",
Tim Peters3b01a702004-10-12 22:19:32 +0000297 'import sys,os;' + SETBINARY +
298 'sys.stdout.write("line1\\n");'
299 'sys.stdout.flush();'
300 'sys.stdout.write("line2\\r");'
301 'sys.stdout.flush();'
302 'sys.stdout.write("line3\\r\\n");'
303 'sys.stdout.flush();'
304 'sys.stdout.write("line4\\r");'
305 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000306 'sys.stdout.write("\\nline5");'
Tim Peters3b01a702004-10-12 22:19:32 +0000307 'sys.stdout.flush();'
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308 'sys.stdout.write("\\nline6");'],
309 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
310 universal_newlines=1)
311 (stdout, stderr) = p.communicate()
312 if hasattr(open, 'newlines'):
313 # Interpreter with universal newline support
Tim Peters3b01a702004-10-12 22:19:32 +0000314 self.assertEqual(stdout,
315 "line1\nline2\nline3\nline4\nline5\nline6")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000316 else:
317 # Interpreter without universal newline support
318 self.assertEqual(stdout, "line1\nline2\rline3\r\nline4\r\nline5\nline6")
319
320 def test_no_leaking(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000321 # Make sure we leak no resources
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000322 for i in range(1026):
Tim Peters3b01a702004-10-12 22:19:32 +0000323 p = subprocess.Popen([sys.executable, "-c",
324 "import sys;sys.stdout.write(sys.stdin.read())"],
325 stdin=subprocess.PIPE,
326 stdout=subprocess.PIPE,
327 stderr=subprocess.PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000328 data = p.communicate("lime")[0]
329 self.assertEqual(data, "lime")
330
331
332 def test_list2cmdline(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333 self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
334 '"a b c" d e')
335 self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
336 'ab\\"c \\ d')
337 self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
338 'a\\\\\\b "de fg" h')
339 self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
340 'a\\\\\\"b c d')
341 self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
342 '"a\\\\b c" d e')
343 self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
344 '"a\\\\b\\ c" d e')
345
346
347 def test_poll(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000348 p = subprocess.Popen([sys.executable,
349 "-c", "import time; time.sleep(4)"])
350 while p.poll() == None:
351 sys.stdout.write(".")
352 sys.stdout.flush()
353 time.sleep(0.5)
354 # Subsequent invocations should just return the returncode
355 self.assertEqual(p.poll(), 0)
356
357
358 def test_wait(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000359 p = subprocess.Popen([sys.executable,
360 "-c", "import time; time.sleep(2)"])
361 self.assertEqual(p.wait(), 0)
362 # Subsequent invocations should just return the returncode
363 self.assertEqual(p.wait(), 0)
Tim Peterse718f612004-10-12 21:51:32 +0000364
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000365 #
366 # POSIX tests
367 #
368 if not mswindows:
369 def test_exceptions(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000370 # catched & re-raised exceptions
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000371 try:
372 p = subprocess.Popen([sys.executable, "-c", ""],
373 cwd="/this/path/does/not/exist")
374 except OSError, e:
375 # The attribute child_traceback should contain "os.chdir"
376 # somewhere.
377 self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
378 else:
379 self.fail("Expected OSError")
Tim Peterse718f612004-10-12 21:51:32 +0000380
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000381 def test_run_abort(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000382 # returncode handles signal termination
Tim Peters3b01a702004-10-12 22:19:32 +0000383 p = subprocess.Popen([sys.executable,
384 "-c", "import os; os.abort()"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000385 p.wait()
386 self.assertEqual(-p.returncode, signal.SIGABRT)
387
388 def test_preexec(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000389 # preexec function
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000390 p = subprocess.Popen([sys.executable, "-c",
391 'import sys,os;' \
392 'sys.stdout.write(os.getenv("FRUIT"))'],
393 stdout=subprocess.PIPE,
394 preexec_fn=lambda: os.putenv("FRUIT", "apple"))
395 self.assertEqual(p.stdout.read(), "apple")
396
397 def test_close_fds(self):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000398 # Make sure we have some fds open
399 os.pipe()
400 p = subprocess.Popen([sys.executable, "-c",
401 'import sys,os;' \
402 'sys.stdout.write(str(os.dup(0)))'],
403 stdout=subprocess.PIPE, close_fds=1)
Tim Peterse718f612004-10-12 21:51:32 +0000404 # When all fds are closed, the next free fd should be 3.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000405 self.assertEqual(p.stdout.read(), "3")
406
407 def test_args_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000408 # args is a string
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000409 f, fname = self.mkstemp()
410 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000411 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
412 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000413 os.close(f)
414 os.chmod(fname, 0700)
415 p = subprocess.Popen(fname)
416 p.wait()
417 self.assertEqual(p.returncode, 47)
418 os.remove(fname)
419
420 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000421 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000422 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000423 [sys.executable,
424 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000425 startupinfo=47)
426 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 creationflags=47)
430
431 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000432 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433 newenv = os.environ.copy()
434 newenv["FRUIT"] = "apple"
435 p = subprocess.Popen(["echo $FRUIT"], shell=1,
436 stdout=subprocess.PIPE,
437 env=newenv)
438 self.assertEqual(p.stdout.read().strip(), "apple")
439
440 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000441 # Run command through the shell (string)
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_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000450 # call() function with string argument on UNIX
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000451 f, fname = self.mkstemp()
452 os.write(f, "#!/bin/sh\n")
Tim Peters3b01a702004-10-12 22:19:32 +0000453 os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" %
454 sys.executable)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455 os.close(f)
456 os.chmod(fname, 0700)
457 rc = subprocess.call(fname)
458 self.assertEqual(rc, 47)
459
Tim Peterse718f612004-10-12 21:51:32 +0000460
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000461 #
462 # Windows tests
463 #
464 if mswindows:
465 def test_startupinfo(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000466 # startupinfo argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000467 # We uses hardcoded constants, because we do not want to
Tim Peterse718f612004-10-12 21:51:32 +0000468 # depend on win32all.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469 STARTF_USESHOWWINDOW = 1
470 SW_MAXIMIZE = 3
471 startupinfo = subprocess.STARTUPINFO()
472 startupinfo.dwFlags = STARTF_USESHOWWINDOW
473 startupinfo.wShowWindow = SW_MAXIMIZE
474 # Since Python is a console process, it won't be affected
475 # by wShowWindow, but the argument should be silently
476 # ignored
477 subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"],
478 startupinfo=startupinfo)
479
480 def test_creationflags(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000481 # creationflags argument
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000482 CREATE_NEW_CONSOLE = 16
Tim Peters3b01a702004-10-12 22:19:32 +0000483 subprocess.call(sys.executable +
484 ' -c "import time; time.sleep(2)"',
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000485 creationflags=CREATE_NEW_CONSOLE)
486
487 def test_invalid_args(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000488 # invalid arguments should raise ValueError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000489 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000490 [sys.executable,
491 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492 preexec_fn=lambda: 1)
493 self.assertRaises(ValueError, subprocess.call,
Tim Peters3b01a702004-10-12 22:19:32 +0000494 [sys.executable,
495 "-c", "import sys; sys.exit(47)"],
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000496 close_fds=True)
497
498 def test_shell_sequence(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000499 # Run command through the shell (sequence)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000500 newenv = os.environ.copy()
501 newenv["FRUIT"] = "physalis"
502 p = subprocess.Popen(["set"], shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000503 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504 env=newenv)
505 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
506
507 def test_shell_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000508 # Run command through the shell (string)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000509 newenv = os.environ.copy()
510 newenv["FRUIT"] = "physalis"
511 p = subprocess.Popen("set", shell=1,
Tim Peterse718f612004-10-12 21:51:32 +0000512 stdout=subprocess.PIPE,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000513 env=newenv)
514 self.assertNotEqual(p.stdout.read().find("physalis"), -1)
515
516 def test_call_string(self):
Tim Peters7b759da2004-10-12 22:29:54 +0000517 # call() function with string argument on Windows
Tim Peters3b01a702004-10-12 22:19:32 +0000518 rc = subprocess.call(sys.executable +
519 ' -c "import sys; sys.exit(47)"')
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000520 self.assertEqual(rc, 47)
521
522
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000523def test_main():
524 test_support.run_unittest(ProcessTestCase)
525
526if __name__ == "__main__":
527 test_main()