1 #!/usr/bin/env python2.2
2 from __future__
import generators
6 from os
.path
import dirname
, abspath
, join
7 from cStringIO
import StringIO
9 rox_lib
= dirname(dirname(dirname(abspath(sys
.argv
[0]))))
10 sys
.path
.insert(0, join(rox_lib
, 'python'))
12 from rox
import processes
, g
14 def pipe_through_command(command
, src
, dst
):
15 processes
.PipeThroughCommand(command
, src
, dst
).wait()
17 class TestProcesses(unittest
.TestCase
):
19 tmp_file
= processes
._Tmp
()
20 tmp_file
.write('Hello')
21 print >>tmp_file
, ' ',
23 os
.write(tmp_file
.fileno(), 'World')
26 assert tmp_file
.read() == 'Hello World'
28 def testInvalidCommand(self
):
30 pipe_through_command('bad_command_1234', None, None)
32 except processes
.ChildError
, ex
:
37 def testValidCommand(self
):
38 pipe_through_command('exit 0', None, None)
40 def testNonFileno(self
):
42 pipe_through_command('echo Hello', None, a
)
43 assert a
.getvalue() == 'Hello\n'
45 def testStringIO(self
):
47 pipe_through_command('echo Hello', None, a
)
48 tmp_file
= processes
._Tmp
()
49 tmp_file
.write('Hello World')
51 pipe_through_command('cat', tmp_file
, a
)
52 assert a
.getvalue() == 'Hello\nHello World'
54 def testWriteFileno(self
):
55 tmp_file
= processes
._Tmp
()
58 pipe_through_command('echo Foo', None, tmp_file
)
60 assert tmp_file
.read() == 'Foo\n'
63 tmp_file
= processes
._Tmp
()
64 tmp_file
.write('Hello World')
65 src
= processes
._Tmp
()
70 pipe_through_command('cat', src
, tmp_file
)
72 assert tmp_file
.read() == '123'
74 def testNonZeroExit(self
):
76 pipe_through_command('exit 1', None, None)
77 except processes
.ChildError
:
84 pipe_through_command('echo one >&2; sleep 2; echo two >&2', None, None)
85 except processes
.ChildError
:
91 tmp_file
= processes
._Tmp
()
93 assert os
.path
.exists(name
)
96 assert not os
.path
.exists(name
)
98 def testKillRunaway(self
):
99 ptc
= processes
.PipeThroughCommand('sleep 100; exit 1', None, None)
102 g
.timeout_add(2000, stop
)
106 except processes
.ChildKilled
:
109 suite
= unittest
.makeSuite(TestProcesses
)
110 if __name__
== '__main__':
111 sys
.argv
.append('-v')