2 # This file is part of GNUnet.
3 # (C) 2010, 2018 Christian Grothoff (and other contributing authors)
5 # GNUnet is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as published
7 # by the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # GNUnet is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
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/>.
18 # Testcase for gnunet-peerinfo
19 from __future__ import print_function
28 class pexpect (object):
30 super(pexpect, self).__init__()
32 def spawn(self, stdin, arglist, *pargs, **kwargs):
33 env = kwargs.pop('env', None)
35 env = os.environ.copy()
36 # This messes up some testcases, disable log redirection
37 env.pop('GNUNET_FORCE_LOGFILE', None)
38 self.proc = subprocess.Popen(arglist, *pargs, env=env, **kwargs)
40 print("Failed to spawn a process {0}".format(arglist))
43 self.stdo, self.stde = self.proc.communicate(stdin)
45 self.stdo, self.stde = self.proc.communicate()
48 def expect(self, s, r, flags=0):
49 stream = self.stdo if s == 'stdout' else self.stde
50 if isinstance(r, str):
55 print("Failed to find `{1}' in {0}, which is `{2}' ({3})".format(s, r, stream, len(stream)))
57 raise ValueError("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format(r))
58 m = r.search(stream.decode(), flags)
60 print("Failed to find `{1}' in {0}, which is is `{2}'".format(s, r.pattern, stream))
62 stream = stream[m.end():]
69 def read(self, s, size=-1):
70 stream = self.stdo if s == 'stdout' else self.stde
76 result = stream[0:size]
77 new_stream = stream[size:]
79 self.stdo = new_stream
81 self.stde = new_stream