Debian Codegraph @

tests/test_preexec_fn.py

raw symbols SCIP
1 #!/usr/bin/env python
2 '''
3 PEXPECT LICENSE
4
5 This license is approved by the OSI and FSF as GPL-compatible.
6 http://opensource.org/licenses/isc-license.txt
7
8 Copyright (c) 2012, Noah Spurrier <[email protected]>
9 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
10 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
11 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
12 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 '''
21 import unittest
22 import shutil
23 from ptyprocess import PtyProcess
24 import os
25 import tempfile
26
27 class PreexecFns(unittest.TestCase):
28 def test_preexec(self):
29 td = tempfile.mkdtemp()
30 filepath = os.path.join(td, 'foo')
31 def pef():
32 with open(filepath, 'w') as f:
33 f.write('bar')
34
35 try:
36 child = PtyProcess.spawn(['ls'], preexec_fn=pef)
37 child.close()
38 with open(filepath, 'r') as f:
39 assert f.read() == 'bar'
40
41 finally:
42 shutil.rmtree(td)
43
44 def test_preexec_error(self):
45 def func():
46 raise ValueError("Test error condition")
47
48 try:
49 child = PtyProcess.spawn(['ls'], preexec_fn=func)
50 # If we get here then an error was not raised
51 child.close()
52 raise AssertionError("ValueError was not raised")
53 except ValueError as err:
54 if str(err) != "Test error condition":
55 # Re-raise the original error to fail the test
56 raise
57
58