1 #!/usr/bin/env python2.3
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
15 def pipe_through_command(command
, src
, dst
):
16 processes
.PipeThroughCommand(command
, src
, dst
).wait()
18 class TestProcesses(unittest
.TestCase
):
20 tmp_file
= processes
._Tmp
()
21 tmp_file
.write('Hello')
22 print >>tmp_file
, ' ',
24 os
.write(tmp_file
.fileno(), 'World')
27 assert tmp_file
.read() == 'Hello World'
29 def testInvalidCommand(self
):
31 pipe_through_command('bad_command_1234', None, None)
33 except processes
.ChildError
, ex
:
38 def testValidCommand(self
):
39 pipe_through_command('exit 0', None, None)
41 def testNonFileno(self
):
43 pipe_through_command('echo Hello', None, a
)
44 assert a
.getvalue() == 'Hello\n'
46 def testStringIO(self
):
48 pipe_through_command('echo Hello', None, a
)
49 tmp_file
= processes
._Tmp
()
50 tmp_file
.write('Hello World')
52 pipe_through_command('cat', tmp_file
, a
)
53 assert a
.getvalue() == 'Hello\nHello World'
55 def testWriteFileno(self
):
56 tmp_file
= processes
._Tmp
()
59 pipe_through_command('echo Foo', None, tmp_file
)
61 assert tmp_file
.read() == 'Foo\n'
64 tmp_file
= processes
._Tmp
()
65 tmp_file
.write('Hello World')
66 src
= processes
._Tmp
()
71 pipe_through_command('cat', src
, tmp_file
)
73 assert tmp_file
.read() == '123'
75 def testNonZeroExit(self
):
77 pipe_through_command('exit 1', None, None)
78 except processes
.ChildError
:
85 pipe_through_command('echo one >&2; sleep 2; echo two >&2', None, None)
86 except processes
.ChildError
:
92 tmp_file
= processes
._Tmp
()
94 assert os
.path
.exists(name
)
97 assert not os
.path
.exists(name
)
99 def testKillRunaway(self
):
100 ptc
= processes
.PipeThroughCommand('sleep 100; exit 1', None, None)
103 gobject
.timeout_add(2000, stop
)
107 except processes
.ChildKilled
:
110 suite
= unittest
.makeSuite(TestProcesses
)
111 if __name__
== '__main__':
112 sys
.argv
.append('-v')