comparison sat/test/helpers.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT: a jabber client 4 # SAT: a jabber client
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
26 from sat.core.log import getLogger 26 from sat.core.log import getLogger
27 getLogger().setLevel(logging.WARNING) # put this to DEBUG when needed 27 getLogger().setLevel(logging.WARNING) # put this to DEBUG when needed
28 28
29 from sat.core import exceptions 29 from sat.core import exceptions
30 from sat.tools import config as tools_config 30 from sat.tools import config as tools_config
31 from constants import Const as C 31 from .constants import Const as C
32 from wokkel.xmppim import RosterItem 32 from wokkel.xmppim import RosterItem
33 from wokkel.generic import parseXml 33 from wokkel.generic import parseXml
34 from sat.core.xmpp import SatRosterProtocol 34 from sat.core.xmpp import SatRosterProtocol
35 from sat.memory.memory import Params, Memory 35 from sat.memory.memory import Params, Memory
36 from twisted.trial.unittest import FailTest 36 from twisted.trial.unittest import FailTest
47 """Convert a bool to a unicode string used in bridge 47 """Convert a bool to a unicode string used in bridge
48 @param value: boolean value 48 @param value: boolean value
49 @return: unicode conversion, according to bridge convention 49 @return: unicode conversion, according to bridge convention
50 50
51 """ 51 """
52 return u"True" if value else u"False" 52 return "True" if value else "False"
53 53
54 54
55 def muteLogging(): 55 def muteLogging():
56 """Temporarily set the logging level to CRITICAL to not pollute the output with expected errors.""" 56 """Temporarily set the logging level to CRITICAL to not pollute the output with expected errors."""
57 logger = getLogger() 57 logger = getLogger()
230 self.expected_calls[name].append((check_args, check_kwargs)) 230 self.expected_calls[name].append((check_args, check_kwargs))
231 return 231 return
232 232
233 def checkCall(*args, **kwargs): 233 def checkCall(*args, **kwargs):
234 if args != check_args or kwargs != check_kwargs: 234 if args != check_args or kwargs != check_kwargs:
235 print "\n\n--------------------" 235 print("\n\n--------------------")
236 print "Args are not equals:" 236 print("Args are not equals:")
237 print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args) 237 print("args\n----\n%s (sent)\n%s (wanted)" % (args, check_args))
238 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs) 238 print("kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs))
239 print "--------------------\n\n" 239 print("--------------------\n\n")
240 raise DifferentArgsException 240 raise DifferentArgsException
241 delattr(self, name) 241 delattr(self, name)
242 242
243 if name in self.expected_calls: # register the next call 243 if name in self.expected_calls: # register the next call
244 args, kwargs = self.expected_calls[name].pop(0) 244 args, kwargs = self.expected_calls[name].pop(0)
246 del self.expected_calls[name] 246 del self.expected_calls[name]
247 self.expectCall(name, *args, **kwargs) 247 self.expectCall(name, *args, **kwargs)
248 248
249 setattr(self, name, checkCall) 249 setattr(self, name, checkCall)
250 250
251 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc=None): 251 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async_=False, doc=None):
252 pass 252 pass
253 253
254 def addSignal(self, name, int_suffix, signature): 254 def addSignal(self, name, int_suffix, signature):
255 pass 255 pass
256 256
384 """Save the sent messages to compare them later. 384 """Save the sent messages to compare them later.
385 385
386 @param obj (domish.Element, str or unicode): message to send 386 @param obj (domish.Element, str or unicode): message to send
387 """ 387 """
388 if not isinstance(obj, domish.Element): 388 if not isinstance(obj, domish.Element):
389 assert(isinstance(obj, str) or isinstance(obj, unicode)) 389 assert(isinstance(obj, str) or isinstance(obj, str))
390 obj = parseXml(obj) 390 obj = parseXml(obj)
391 391
392 if obj.name == 'iq': 392 if obj.name == 'iq':
393 # IQ request expects an answer, return the request itself so 393 # IQ request expects an answer, return the request itself so
394 # you can check if it has been well built by your plugin. 394 # you can check if it has been well built by your plugin.
430 value = value.strip() or None 430 value = value.strip() or None
431 except AttributeError: 431 except AttributeError:
432 value = None 432 value = None
433 setattr(elt, attr, value) 433 setattr(elt, attr, value)
434 if (got_elt.tag != exp_elt.tag): 434 if (got_elt.tag != exp_elt.tag):
435 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) 435 print("XML are not equals (elt %s/%s):" % (got_elt, exp_elt))
436 print "tag: got [%s] expected: [%s]" % (got_elt.tag, exp_elt.tag) 436 print("tag: got [%s] expected: [%s]" % (got_elt.tag, exp_elt.tag))
437 return False 437 return False
438 if (got_elt.attrib != exp_elt.attrib): 438 if (got_elt.attrib != exp_elt.attrib):
439 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) 439 print("XML are not equals (elt %s/%s):" % (got_elt, exp_elt))
440 print "attribs: got %s expected %s" % (got_elt.attrib, exp_elt.attrib) 440 print("attribs: got %s expected %s" % (got_elt.attrib, exp_elt.attrib))
441 return False 441 return False
442 if (got_elt.tail != exp_elt.tail or got_elt.text != exp_elt.text): 442 if (got_elt.tail != exp_elt.tail or got_elt.text != exp_elt.text):
443 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) 443 print("XML are not equals (elt %s/%s):" % (got_elt, exp_elt))
444 print "text: got [%s] expected: [%s]" % (got_elt.text, exp_elt.text) 444 print("text: got [%s] expected: [%s]" % (got_elt.text, exp_elt.text))
445 print "tail: got [%s] expected: [%s]" % (got_elt.tail, exp_elt.tail) 445 print("tail: got [%s] expected: [%s]" % (got_elt.tail, exp_elt.tail))
446 return False 446 return False
447 if (len(got_elt) != len(exp_elt)): 447 if (len(got_elt) != len(exp_elt)):
448 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) 448 print("XML are not equals (elt %s/%s):" % (got_elt, exp_elt))
449 print "children len: got %d expected: %d" % (len(got_elt), len(exp_elt)) 449 print("children len: got %d expected: %d" % (len(got_elt), len(exp_elt)))
450 return False 450 return False
451 for idx, child in enumerate(got_elt): 451 for idx, child in enumerate(got_elt):
452 if not equalElt(child, exp_elt[idx]): 452 if not equalElt(child, exp_elt[idx]):
453 return False 453 return False
454 return True 454 return True
459 459
460 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml) 460 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml)
461 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected) 461 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected)
462 462
463 if not equalElt(xml_elt, expected_elt): 463 if not equalElt(xml_elt, expected_elt):
464 print "---" 464 print("---")
465 print "XML are not equals:" 465 print("XML are not equals:")
466 print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8') 466 print("got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8'))
467 print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8') 467 print("was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8'))
468 print "---" 468 print("---")
469 raise DifferentXMLException 469 raise DifferentXMLException
470 470
471 def assertEqualUnsortedList(self, a, b, msg): 471 def assertEqualUnsortedList(self, a, b, msg):
472 counter_a = Counter(a) 472 counter_a = Counter(a)
473 counter_b = Counter(b) 473 counter_b = Counter(b)
474 if counter_a != counter_b: 474 if counter_a != counter_b:
475 print "---" 475 print("---")
476 print "Unsorted lists are not equals:" 476 print("Unsorted lists are not equals:")
477 print "got : %s" % counter_a 477 print("got : %s" % counter_a)
478 print "was expecting: %s" % counter_b 478 print("was expecting: %s" % counter_b)
479 if msg: 479 if msg:
480 print msg 480 print(msg)
481 print "---" 481 print("---")
482 raise DifferentListException 482 raise DifferentListException