bootstrap.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # testing/plugin/bootstrap.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """
  8. Bootstrapper for test framework plugins.
  9. The entire rationale for this system is to get the modules in plugin/
  10. imported without importing all of the supporting library, so that we can
  11. set up things for testing before coverage starts.
  12. The rationale for all of plugin/ being *in* the supporting library in the
  13. first place is so that the testing and plugin suite is available to other
  14. libraries, mainly external SQLAlchemy and Alembic dialects, to make use
  15. of the same test environment and standard suites available to
  16. SQLAlchemy/Alembic themselves without the need to ship/install a separate
  17. package outside of SQLAlchemy.
  18. """
  19. import os
  20. import sys
  21. bootstrap_file = locals()["bootstrap_file"]
  22. to_bootstrap = locals()["to_bootstrap"]
  23. def load_file_as_module(name):
  24. path = os.path.join(os.path.dirname(bootstrap_file), "%s.py" % name)
  25. if sys.version_info >= (3, 5):
  26. import importlib.util
  27. spec = importlib.util.spec_from_file_location(name, path)
  28. assert spec is not None
  29. assert spec.loader is not None
  30. mod = importlib.util.module_from_spec(spec)
  31. spec.loader.exec_module(mod)
  32. else:
  33. import imp
  34. mod = imp.load_source(name, path)
  35. return mod
  36. if to_bootstrap == "pytest":
  37. sys.modules["sqla_plugin_base"] = load_file_as_module("plugin_base")
  38. sys.modules["sqla_plugin_base"].bootstrapped_as_sqlalchemy = True
  39. if sys.version_info < (3, 0):
  40. sys.modules["sqla_reinvent_fixtures"] = load_file_as_module(
  41. "reinvent_fixtures_py2k"
  42. )
  43. sys.modules["sqla_pytestplugin"] = load_file_as_module("pytestplugin")
  44. else:
  45. raise Exception("unknown bootstrap: %s" % to_bootstrap) # noqa