runtests.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. import atexit
  4. import os
  5. import shutil
  6. import sys
  7. import django
  8. from django.core.management import call_command
  9. def main(package="mezzanine", args=()):
  10. """
  11. This is the main test function called via ``python setup.py test``.
  12. It's responsible for hacking the ``project_template`` dir into
  13. an actual project to test against.
  14. """
  15. from mezzanine.utils.importing import path_for_import
  16. package_path = path_for_import(package)
  17. project_path = os.path.join(package_path, "project_template")
  18. os.environ["DJANGO_SETTINGS_MODULE"] = "project_name.test_settings"
  19. project_app_path = os.path.join(project_path, "project_name")
  20. local_settings_path = os.path.join(project_app_path, "local_settings.py")
  21. test_settings_path = os.path.join(project_app_path, "test_settings.py")
  22. sys.path.insert(0, package_path)
  23. sys.path.insert(0, project_path)
  24. if not os.path.exists(test_settings_path):
  25. shutil.copy(local_settings_path + ".template", test_settings_path)
  26. with open(test_settings_path, "r") as f:
  27. local_settings = f.read()
  28. with open(test_settings_path, "w") as f:
  29. test_settings = """
  30. from . import settings
  31. globals().update(i for i in settings.__dict__.items() if i[0].isupper())
  32. # Require the mezzanine.accounts app. We use settings.INSTALLED_APPS here so
  33. # the syntax test doesn't complain about an undefined name.
  34. if "mezzanine.accounts" not in settings.INSTALLED_APPS:
  35. INSTALLED_APPS = list(settings.INSTALLED_APPS) + ["mezzanine.accounts"]
  36. # Use the MD5 password hasher by default for quicker test runs.
  37. PASSWORD_HASHERS = ('django.contrib.auth.hashers.MD5PasswordHasher',)
  38. """
  39. f.write(test_settings + local_settings)
  40. def cleanup_test_settings():
  41. import os # Outer scope sometimes unavailable in atexit functions.
  42. for fn in [test_settings_path, test_settings_path + 'c']:
  43. try:
  44. os.remove(fn)
  45. except OSError:
  46. pass
  47. atexit.register(cleanup_test_settings)
  48. django.setup()
  49. from django.core.management.commands import test
  50. if django.VERSION < (1, 10):
  51. sys.exit(test.Command().execute(*args, verbosity=1))
  52. sys.exit(call_command(test.Command(), *args, verbosity=1))
  53. if __name__ == "__main__":
  54. main(args=sys.argv[1:])