bootstrap.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. Bootstrapper for nose/pytest 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. NOTE: copied/adapted from SQLAlchemy master for backwards compatibility;
  13. this should be removable when Alembic targets SQLAlchemy 1.0.0.
  14. """
  15. import os
  16. import sys
  17. bootstrap_file = locals()['bootstrap_file']
  18. to_bootstrap = locals()['to_bootstrap']
  19. def load_file_as_module(name):
  20. path = os.path.join(os.path.dirname(bootstrap_file), "%s.py" % name)
  21. if sys.version_info >= (3, 3):
  22. from importlib import machinery
  23. mod = machinery.SourceFileLoader(name, path).load_module()
  24. else:
  25. import imp
  26. mod = imp.load_source(name, path)
  27. return mod
  28. if to_bootstrap == "pytest":
  29. sys.modules["alembic_plugin_base"] = load_file_as_module("plugin_base")
  30. sys.modules["alembic_pytestplugin"] = load_file_as_module("pytestplugin")
  31. elif to_bootstrap == "nose":
  32. sys.modules["alembic_plugin_base"] = load_file_as_module("plugin_base")
  33. sys.modules["alembic_noseplugin"] = load_file_as_module("noseplugin")
  34. else:
  35. raise Exception("unknown bootstrap: %s" % to_bootstrap) # noqa