Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | import subprocess |
| 4 | import sys |
| 5 | import signal |
| 6 | import os |
| 7 | import tempfile |
| 8 | import time |
| 9 | |
| 10 | mswindows = (sys.platform == "win32") |
| 11 | |
| 12 | # |
| 13 | # Depends on the following external programs: Python |
| 14 | # |
| 15 | |
| 16 | if mswindows: |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 17 | SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' |
| 18 | 'os.O_BINARY);') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 19 | else: |
| 20 | SETBINARY = '' |
| 21 | |
| 22 | class 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 30 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 31 | # |
| 32 | # Generic tests |
| 33 | # |
| 34 | def test_call_seq(self): |
| 35 | """call() function with sequence argument""" |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 36 | rc = subprocess.call([sys.executable, "-c", |
| 37 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 38 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 73 | p = subprocess.Popen(["somethingyoudonthave", |
| 74 | "-c", "import sys; sys.exit(47)"], |
| 75 | executable=sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 76 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 91 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 92 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 121 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 122 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 132 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 133 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 149 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 150 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 160 | tf = tempfile.TemporaryFile() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 161 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 221 | stdin=subprocess.PIPE, |
| 222 | stdout=subprocess.PIPE, |
| 223 | stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 224 | (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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 230 | p = subprocess.Popen([sys.executable, "-c", |
| 231 | "import sys; sys.exit(47)"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 232 | (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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 239 | # communicate() does not work properly. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 240 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 248 | 'import sys,os;' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 249 | 'sys.stdout.write(sys.stdin.read(47));' \ |
| 250 | 'sys.stderr.write("xyz"*%d);' \ |
| 251 | 'sys.stdout.write(sys.stdin.read())' % pipe_buf], |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 252 | stdin=subprocess.PIPE, |
| 253 | stdout=subprocess.PIPE, |
| 254 | stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 255 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 264 | stdin=subprocess.PIPE, |
| 265 | stdout=subprocess.PIPE, |
| 266 | stderr=subprocess.PIPE) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 267 | p.stdin.write("banana") |
| 268 | (stdout, stderr) = p.communicate("split") |
| 269 | self.assertEqual(stdout, "bananasplit") |
| 270 | self.assertEqual(stderr, "") |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 271 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 272 | def test_universal_newlines(self): |
| 273 | """universal newlines""" |
| 274 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 275 | '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 Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 284 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 285 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 286 | '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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 292 | self.assertEqual(stdout, |
| 293 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 294 | else: |
| 295 | # Interpreter without universal newline support |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 296 | self.assertEqual(stdout, |
| 297 | "line1\nline2\rline3\r\nline4\r\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 298 | |
| 299 | def test_universal_newlines_communicate(self): |
| 300 | """universal newlines through communicate()""" |
| 301 | p = subprocess.Popen([sys.executable, "-c", |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 302 | '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 Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 311 | 'sys.stdout.write("\\nline5");' |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 312 | 'sys.stdout.flush();' |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 313 | '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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 319 | self.assertEqual(stdout, |
| 320 | "line1\nline2\nline3\nline4\nline5\nline6") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 321 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 328 | 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 Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 333 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 373 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 374 | # |
| 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 389 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 390 | def test_run_abort(self): |
| 391 | """returncode handles signal termination""" |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 392 | p = subprocess.Popen([sys.executable, |
| 393 | "-c", "import os; os.abort()"]) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 394 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 414 | # When all fds are closed, the next free fd should be 3. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 415 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 421 | os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" % |
| 422 | sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 423 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 433 | [sys.executable, |
| 434 | "-c", "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 435 | startupinfo=47) |
| 436 | self.assertRaises(ValueError, subprocess.call, |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 437 | [sys.executable, |
| 438 | "-c", "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 439 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 463 | os.write(f, "exec %s -c 'import sys; sys.exit(47)'\n" % |
| 464 | sys.executable) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 465 | os.close(f) |
| 466 | os.chmod(fname, 0700) |
| 467 | rc = subprocess.call(fname) |
| 468 | self.assertEqual(rc, 47) |
| 469 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 470 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 471 | # |
| 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 478 | # depend on win32all. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 479 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 493 | subprocess.call(sys.executable + |
| 494 | ' -c "import time; time.sleep(2)"', |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 495 | creationflags=CREATE_NEW_CONSOLE) |
| 496 | |
| 497 | def test_invalid_args(self): |
| 498 | """invalid arguments should raise ValueError""" |
| 499 | self.assertRaises(ValueError, subprocess.call, |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 500 | [sys.executable, |
| 501 | "-c", "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 502 | preexec_fn=lambda: 1) |
| 503 | self.assertRaises(ValueError, subprocess.call, |
Tim Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 504 | [sys.executable, |
| 505 | "-c", "import sys; sys.exit(47)"], |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 506 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 513 | stdout=subprocess.PIPE, |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 514 | 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 Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 522 | stdout=subprocess.PIPE, |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 523 | 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 Peters | 3b01a70 | 2004-10-12 22:19:32 +0000 | [diff] [blame^] | 528 | rc = subprocess.call(sys.executable + |
| 529 | ' -c "import sys; sys.exit(47)"') |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 530 | self.assertEqual(rc, 47) |
| 531 | |
| 532 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 533 | def test_main(): |
| 534 | test_support.run_unittest(ProcessTestCase) |
| 535 | |
| 536 | if __name__ == "__main__": |
| 537 | test_main() |