aboutsummaryrefslogtreecommitdiff
path: root/src/integration-tests/gnunet_pyexpect.py.in
diff options
context:
space:
mode:
Diffstat (limited to 'src/integration-tests/gnunet_pyexpect.py.in')
-rw-r--r--src/integration-tests/gnunet_pyexpect.py.in115
1 files changed, 63 insertions, 52 deletions
diff --git a/src/integration-tests/gnunet_pyexpect.py.in b/src/integration-tests/gnunet_pyexpect.py.in
index d757634a5..aad84e4f7 100644
--- a/src/integration-tests/gnunet_pyexpect.py.in
+++ b/src/integration-tests/gnunet_pyexpect.py.in
@@ -11,7 +11,7 @@
11# WITHOUT ANY WARRANTY; without even the implied warranty of 11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# Affero General Public License for more details. 13# Affero General Public License for more details.
14# 14#
15# You should have received a copy of the GNU Affero General Public License 15# You should have received a copy of the GNU Affero General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>. 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17# 17#
@@ -26,58 +26,69 @@ import sys
26import shutil 26import shutil
27import time 27import time
28 28
29class pexpect (object):
30 def __init__ (self):
31 super (pexpect, self).__init__ ()
32 29
33 def spawn (self, stdin, arglist, *pargs, **kwargs): 30class pexpect(object):
34 env = kwargs.pop ('env', None) 31 def __init__(self):
35 if env is None: 32 super(pexpect, self).__init__()
36 env = os.environ.copy () 33
37 # This messes up some testcases, disable log redirection 34 def spawn(self, stdin, arglist, *pargs, **kwargs):
38 env.pop ('GNUNET_FORCE_LOGFILE', None) 35 env = kwargs.pop('env', None)
39 self.proc = subprocess.Popen (arglist, *pargs, env=env, **kwargs) 36 if env is None:
40 if self.proc is None: 37 env = os.environ.copy()
41 print ("Failed to spawn a process {0}".format (arglist)) 38 # This messes up some testcases, disable log redirection
42 sys.exit (1) 39 env.pop('GNUNET_FORCE_LOGFILE', None)
43 if stdin is not None: 40 self.proc = subprocess.Popen(arglist, *pargs, env=env, **kwargs)
44 self.stdo, self.stde = self.proc.communicate (stdin) 41 if self.proc is None:
45 else: 42 print("Failed to spawn a process {0}".format(arglist))
46 self.stdo, self.stde = self.proc.communicate () 43 sys.exit(1)
47 return self.proc 44 if stdin is not None:
45 self.stdo, self.stde = self.proc.communicate(stdin)
46 else:
47 self.stdo, self.stde = self.proc.communicate()
48 return self.proc
48 49
49 def expect (self, s, r, flags=0): 50 def expect(self, s, r, flags=0):
50 stream = self.stdo if s == 'stdout' else self.stde 51 stream = self.stdo if s == 'stdout' else self.stde
51 if isinstance (r, str): 52 if isinstance(r, str):
52 if r == "EOF": 53 if r == "EOF":
53 if len (stream) == 0: 54 if len(stream) == 0:
54 return True 55 return True
56 else:
57 print(
58 "Failed to find `{1}' in {0}, which is `{2}' ({3})".
59 format(s, r, stream, len(stream))
60 )
61 sys.exit(2)
62 raise ValueError(
63 "Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'"
64 .format(r)
65 )
66 m = r.search(stream, flags)
67 if not m:
68 print(
69 "Failed to find `{1}' in {0}, which is is `{2}'".format(
70 s, r.pattern, stream
71 )
72 )
73 sys.exit(2)
74 stream = stream[m.end():]
75 if s == 'stdout':
76 self.stdo = stream
55 else: 77 else:
56 print ("Failed to find `{1}' in {0}, which is `{2}' ({3})".format (s, r, stream, len (stream))) 78 self.stde = stream
57 sys.exit (2) 79 return m
58 raise ValueError ("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format (r))
59 m = r.search (stream, flags)
60 if not m:
61 print ("Failed to find `{1}' in {0}, which is is `{2}'".format (s, r.pattern, stream))
62 sys.exit (2)
63 stream = stream[m.end ():]
64 if s == 'stdout':
65 self.stdo = stream
66 else:
67 self.stde = stream
68 return m
69 80
70 def read (self, s, size=-1): 81 def read(self, s, size=-1):
71 stream = self.stdo if s == 'stdout' else self.stde 82 stream = self.stdo if s == 'stdout' else self.stde
72 result = "" 83 result = ""
73 if size < 0: 84 if size < 0:
74 result = stream 85 result = stream
75 new_stream = "" 86 new_stream = ""
76 else: 87 else:
77 result = stream[0:size] 88 result = stream[0:size]
78 new_stream = stream[size:] 89 new_stream = stream[size:]
79 if s == 'stdout': 90 if s == 'stdout':
80 self.stdo = new_stream 91 self.stdo = new_stream
81 else: 92 else:
82 self.stde = new_stream 93 self.stde = new_stream
83 return result 94 return result