blob: 1eb06b4695b5ce6cc7d45b8f4495124f852256e2 [file] [log] [blame]
Guido van Rossum59e4f371999-03-11 13:26:23 +00001#! /usr/bin/env python
Collin Winter1b4145d2007-03-16 21:13:35 +00002"""Test script for popen2.py"""
Guido van Rossum59e4f371999-03-11 13:26:23 +00003
Fred Drake31f182e2000-08-28 17:20:05 +00004import os
Moshe Zadkafc3fc332001-01-30 18:35:32 +00005import sys
Collin Winter1b4145d2007-03-16 21:13:35 +00006import unittest
7import popen2
Fred Drake31f182e2000-08-28 17:20:05 +00008
Collin Winter1b4145d2007-03-16 21:13:35 +00009from test.test_support import TestSkipped, run_unittest, reap_children
Guido van Rossum59e4f371999-03-11 13:26:23 +000010
Collin Winter1b4145d2007-03-16 21:13:35 +000011if sys.platform[:4] == 'beos' or sys.platform[:6] == 'atheos':
12 # Locks get messed up or something. Generally we're supposed
13 # to avoid mixing "posix" fork & exec with native threads, and
14 # they may be right about that after all.
15 raise TestSkipped("popen2() doesn't work on " + sys.platform)
Guido van Rossum59e4f371999-03-11 13:26:23 +000016
Collin Winter1b4145d2007-03-16 21:13:35 +000017# if we don't have os.popen, check that
18# we have os.fork. if not, skip the test
19# (by raising an ImportError)
20try:
21 from os import popen
22 del popen
23except ImportError:
24 from os import fork
25 del fork
Guido van Rossum59e4f371999-03-11 13:26:23 +000026
Collin Winter1b4145d2007-03-16 21:13:35 +000027class Popen2Test(unittest.TestCase):
28 cmd = "cat"
Fred Drake31f182e2000-08-28 17:20:05 +000029 if os.name == "nt":
30 cmd = "more"
Collin Winter1b4145d2007-03-16 21:13:35 +000031 teststr = "ab cd\n"
Tim Peters36208572000-09-01 20:38:55 +000032 # "more" doesn't act the same way across Windows flavors,
33 # sometimes adding an extra newline at the start or the
34 # end. So we strip whitespace off both ends for comparison.
35 expected = teststr.strip()
Fred Drake31f182e2000-08-28 17:20:05 +000036
Collin Winter1b4145d2007-03-16 21:13:35 +000037 def setUp(self):
38 popen2._cleanup()
39 # When the test runs, there shouldn't be any open pipes
40 self.assertFalse(popen2._active, "Active pipes when test starts" +
41 repr([c.cmd for c in popen2._active]))
42
43 def tearDown(self):
44 for inst in popen2._active:
45 inst.wait()
46 popen2._cleanup()
47 self.assertFalse(popen2._active, "_active not empty")
48 reap_children()
49
50 def validate_output(self, teststr, expected_out, r, w, e=None):
51 w.write(teststr)
52 w.close()
53 got = r.read()
54 self.assertEquals(expected_out, got.strip(), "wrote %r read %r" %
55 (teststr, got))
56
57 if e is not None:
58 got = e.read()
59 self.assertFalse(got, "unexpected %r on stderr" % got)
60
61 def test_popen2(self):
62 r, w = popen2.popen2(self.cmd)
63 self.validate_output(self.teststr, self.expected, r, w)
64
65 def test_popen3(self):
66 if os.name == 'posix':
67 r, w, e = popen2.popen3([self.cmd])
68 self.validate_output(self.teststr, self.expected, r, w, e)
69
70 r, w, e = popen2.popen3(self.cmd)
71 self.validate_output(self.teststr, self.expected, r, w, e)
72
73 def test_os_popen2(self):
74 # same test as test_popen2(), but using the os.popen*() API
75 w, r = os.popen2(self.cmd)
76 self.validate_output(self.teststr, self.expected, r, w)
77
78 def test_os_popen3(self):
79 # same test as test_popen3(), but using the os.popen*() API
80 if os.name == 'posix':
81 w, r, e = os.popen3([self.cmd])
82 self.validate_output(self.teststr, self.expected, r, w, e)
83
84 w, r, e = os.popen3(self.cmd)
85 self.validate_output(self.teststr, self.expected, r, w, e)
86
87
88def test_main():
89 run_unittest(Popen2Test)
90
91if __name__ == "__main__":
92 test_main()