run_tests.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. import sys
  3. import shutil
  4. import tempfile
  5. try:
  6. import django
  7. except ImportError:
  8. print("Error: missing test dependency:")
  9. print(" django library is needed to run test suite")
  10. print(" you can install it with 'pip install django'")
  11. print(" or use tox to automatically handle test dependencies")
  12. sys.exit(1)
  13. try:
  14. import shortuuid
  15. except ImportError:
  16. print("Error: missing test dependency:")
  17. print(" shortuuid library is needed to run test suite")
  18. print(" you can install it with 'pip install shortuuid'")
  19. print(" or use tox to automatically handle test dependencies")
  20. sys.exit(1)
  21. try:
  22. import dateutil
  23. except ImportError:
  24. print("Error: missing test dependency:")
  25. print(" dateutil library is needed to run test suite")
  26. print(" you can install it with 'pip install python-dateutil'")
  27. print(" or use tox to automatically handle test dependencies")
  28. sys.exit(1)
  29. try:
  30. import six
  31. except ImportError:
  32. print("Error: missing test dependency:")
  33. print(" six library is needed to run test suite")
  34. print(" you can install it with 'pip install six'")
  35. print(" or use tox to automatically handle test dependencies")
  36. sys.exit(1)
  37. __test_libs__ = [
  38. django,
  39. shortuuid,
  40. dateutil,
  41. six
  42. ]
  43. from django.conf import settings
  44. def main():
  45. # Dynamically configure the Django settings with the minimum necessary to
  46. # get Django running tests.
  47. KEY_LOCS = {}
  48. try:
  49. try:
  50. # If KeyCzar is available, set up the environment.
  51. from keyczar import keyczart, keyinfo
  52. # Create an RSA private key.
  53. keys_dir = tempfile.mkdtemp("django_extensions_tests_keyzcar_rsa_dir")
  54. keyczart.Create(keys_dir, "test", keyinfo.DECRYPT_AND_ENCRYPT, asymmetric=True)
  55. keyczart.AddKey(keys_dir, "PRIMARY", size=4096)
  56. KEY_LOCS['DECRYPT_AND_ENCRYPT'] = keys_dir
  57. # Create an RSA public key.
  58. pub_dir = tempfile.mkdtemp("django_extensions_tests_keyzcar_pub_dir")
  59. keyczart.PubKey(keys_dir, pub_dir)
  60. KEY_LOCS['ENCRYPT'] = pub_dir
  61. except ImportError:
  62. pass
  63. settings.configure(
  64. INSTALLED_APPS=[
  65. 'django.contrib.auth',
  66. 'django.contrib.contenttypes',
  67. 'django.contrib.admin',
  68. 'django.contrib.sessions',
  69. 'django_extensions.tests.testapp',
  70. 'django_extensions',
  71. ],
  72. # Django replaces this, but it still wants it. *shrugs*
  73. DATABASE_ENGINE='django.db.backends.sqlite3',
  74. DATABASES={
  75. 'default': {
  76. 'ENGINE': 'django.db.backends.sqlite3',
  77. 'NAME': ':memory:',
  78. }
  79. },
  80. MEDIA_ROOT='/tmp/django_extensions_test_media/',
  81. MEDIA_PATH='/media/',
  82. ROOT_URLCONF='django_extensions.tests.urls',
  83. DEBUG=True,
  84. TEMPLATE_DEBUG=True,
  85. ENCRYPTED_FIELD_KEYS_DIR=KEY_LOCS,
  86. )
  87. if django.VERSION[:2] >= (1, 7):
  88. django.setup()
  89. apps = ['django_extensions']
  90. if django.VERSION[:2] >= (1, 6):
  91. apps.append('django_extensions.tests.testapp')
  92. apps.append('django_extensions.tests')
  93. from django.core.management import call_command
  94. from django.test.utils import get_runner
  95. try:
  96. from django.contrib.auth import get_user_model
  97. except ImportError:
  98. USERNAME_FIELD = "username"
  99. else:
  100. USERNAME_FIELD = get_user_model().USERNAME_FIELD
  101. DjangoTestRunner = get_runner(settings)
  102. class TestRunner(DjangoTestRunner):
  103. def setup_databases(self, *args, **kwargs):
  104. result = super(TestRunner, self).setup_databases(*args, **kwargs)
  105. kwargs = {
  106. "interactive": False,
  107. "email": "admin@doesnotexit.com",
  108. USERNAME_FIELD: "admin",
  109. }
  110. call_command("createsuperuser", **kwargs)
  111. return result
  112. failures = TestRunner(verbosity=2, interactive=True).run_tests(apps)
  113. sys.exit(failures)
  114. finally:
  115. for name, path in KEY_LOCS.items():
  116. # cleanup crypto key temp dirs
  117. shutil.rmtree(path)
  118. if __name__ == '__main__':
  119. main()