Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_utils.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (c) 2013-2014 Google, Inc.
  2. # Copyright (c) 2015-2016 Cara Vinson <ceridwenv@gmail.com>
  3. # Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
  4. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  5. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  6. """Utility functions for test code that uses astroid ASTs as input."""
  7. import contextlib
  8. import functools
  9. import sys
  10. import warnings
  11. from astroid import nodes
  12. from astroid import util
  13. def require_version(minver=None, maxver=None):
  14. """ Compare version of python interpreter to the given one. Skip the test
  15. if older.
  16. """
  17. def parse(string, default=None):
  18. string = string or default
  19. try:
  20. return tuple(int(v) for v in string.split('.'))
  21. except ValueError:
  22. util.reraise(ValueError('%s is not a correct version : should be X.Y[.Z].' % string))
  23. def check_require_version(f):
  24. current = sys.version_info[:3]
  25. if parse(minver, "0") < current <= parse(maxver, "4"):
  26. return f
  27. str_version = '.'.join(str(v) for v in sys.version_info)
  28. @functools.wraps(f)
  29. def new_f(self, *args, **kwargs):
  30. if minver is not None:
  31. self.skipTest('Needs Python > %s. Current version is %s.'
  32. % (minver, str_version))
  33. elif maxver is not None:
  34. self.skipTest('Needs Python <= %s. Current version is %s.'
  35. % (maxver, str_version))
  36. return new_f
  37. return check_require_version
  38. def get_name_node(start_from, name, index=0):
  39. return [n for n in start_from.nodes_of_class(nodes.Name) if n.name == name][index]
  40. @contextlib.contextmanager
  41. def enable_warning(warning):
  42. warnings.simplefilter('always', warning)
  43. try:
  44. yield
  45. finally:
  46. # Reset it to default value, so it will take
  47. # into account the values from the -W flag.
  48. warnings.simplefilter('default', warning)