bootstrap.py 1.7 KB

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