2 # This file is part of GNUnet.
3 # (C) 2010 Christian Grothoff (and other contributing authors)
5 # GNUnet is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 2, or (at your
8 # 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 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with GNUnet; see the file COPYING. If not, write to the
17 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 # Boston, MA 02110-1301, USA.
20 # Testcase for gnunet-peerinfo
21 from __future__ import print_function
29 class pexpect (object):
31 super (pexpect, self).__init__ ()
33 def spawn (self, stdin, arglist, *pargs, **kwargs):
34 env = kwargs.pop ('env', None)
36 env = os.environ.copy ()
37 # This messes up some testcases, disable log redirection
38 env.pop ('GNUNET_FORCE_LOGFILE', None)
39 self.proc = subprocess.Popen (arglist, *pargs, env=env, **kwargs)
41 print ("Failed to spawn a process {0}".format (arglist))
44 self.stdo, self.stde = self.proc.communicate (stdin)
46 self.stdo, self.stde = self.proc.communicate ()
49 def expect (self, s, r, flags=0):
50 stream = self.stdo if s == 'stdout' else self.stde
51 if isinstance (r, str):
56 print ("Failed to find `{1}' in {0}, which is `{2}' ({3})".format (s, r, stream, len (stream)))
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.decode(), flags)
61 print ("Failed to find `{1}' in {0}, which is is `{2}'".format (s, r.pattern, stream))
63 stream = stream[m.end ():]
70 def read (self, s, size=-1):
71 stream = self.stdo if s == 'stdout' else self.stde
77 result = stream[0:size]
78 new_stream = stream[size:]
80 self.stdo = new_stream
82 self.stde = new_stream