test_utils.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) 2013-2014 Google, Inc.
  2. # Copyright (c) 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  3. # Copyright (c) 2015-2016, 2018-2020 Claudiu Popa <pcmanticore@gmail.com>
  4. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com>
  5. # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
  6. # Copyright (c) 2018 Anthony Sottile <asottile@umich.edu>
  7. # Copyright (c) 2020-2021 hippo91 <guillaume.peillex@gmail.com>
  8. # Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  9. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  10. # Copyright (c) 2021 Andrew Haigh <hello@nelf.in>
  11. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  12. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  13. """Utility functions for test code that uses astroid ASTs as input."""
  14. import contextlib
  15. import functools
  16. import sys
  17. import warnings
  18. from typing import Callable, Tuple
  19. import pytest
  20. from astroid import manager, nodes, transforms
  21. def require_version(minver: str = "0.0.0", maxver: str = "4.0.0") -> Callable:
  22. """Compare version of python interpreter to the given one.
  23. Skip the test if older.
  24. """
  25. def parse(python_version: str) -> Tuple[int, ...]:
  26. try:
  27. return tuple(int(v) for v in python_version.split("."))
  28. except ValueError as e:
  29. msg = f"{python_version} is not a correct version : should be X.Y[.Z]."
  30. raise ValueError(msg) from e
  31. min_version = parse(minver)
  32. max_version = parse(maxver)
  33. def check_require_version(f):
  34. current: Tuple[int, int, int] = sys.version_info[:3]
  35. if min_version < current <= max_version:
  36. return f
  37. version: str = ".".join(str(v) for v in sys.version_info)
  38. @functools.wraps(f)
  39. def new_f(*args, **kwargs):
  40. if minver != "0.0.0":
  41. pytest.skip(f"Needs Python > {minver}. Current version is {version}.")
  42. elif maxver != "4.0.0":
  43. pytest.skip(f"Needs Python <= {maxver}. Current version is {version}.")
  44. return new_f
  45. return check_require_version
  46. def get_name_node(start_from, name, index=0):
  47. return [n for n in start_from.nodes_of_class(nodes.Name) if n.name == name][index]
  48. @contextlib.contextmanager
  49. def enable_warning(warning):
  50. warnings.simplefilter("always", warning)
  51. try:
  52. yield
  53. finally:
  54. # Reset it to default value, so it will take
  55. # into account the values from the -W flag.
  56. warnings.simplefilter("default", warning)
  57. def brainless_manager():
  58. m = manager.AstroidManager()
  59. # avoid caching into the AstroidManager borg since we get problems
  60. # with other tests :
  61. m.__dict__ = {}
  62. m._failed_import_hooks = []
  63. m.astroid_cache = {}
  64. m._mod_file_cache = {}
  65. m._transform = transforms.TransformVisitor()
  66. m.extension_package_whitelist = {}
  67. return m