bootstrap.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # testing/plugin/bootstrap.py
  2. # Copyright (C) 2005-2025 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. # mypy: ignore-errors
  8. """
  9. Bootstrapper for test framework plugins.
  10. The entire rationale for this system is to get the modules in plugin/
  11. imported without importing all of the supporting library, so that we can
  12. set up things for testing before coverage starts.
  13. The rationale for all of plugin/ being *in* the supporting library in the
  14. first place is so that the testing and plugin suite is available to other
  15. libraries, mainly external SQLAlchemy and Alembic dialects, to make use
  16. of the same test environment and standard suites available to
  17. SQLAlchemy/Alembic themselves without the need to ship/install a separate
  18. package outside of SQLAlchemy.
  19. """
  20. import importlib.util
  21. import os
  22. import sys
  23. bootstrap_file = locals()["bootstrap_file"]
  24. to_bootstrap = locals()["to_bootstrap"]
  25. def load_file_as_module(name):
  26. path = os.path.join(os.path.dirname(bootstrap_file), "%s.py" % name)
  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. return mod
  33. if to_bootstrap == "pytest":
  34. sys.modules["sqla_plugin_base"] = load_file_as_module("plugin_base")
  35. sys.modules["sqla_plugin_base"].bootstrapped_as_sqlalchemy = True
  36. sys.modules["sqla_pytestplugin"] = load_file_as_module("pytestplugin")
  37. else:
  38. raise Exception("unknown bootstrap: %s" % to_bootstrap) # noqa