runtests.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import os
  2. import pwd
  3. import shutil
  4. import sys
  5. from django import setup
  6. from django.test.utils import setup_test_environment
  7. def runtests():
  8. test_runner_path = os.path.dirname(__file__)
  9. project_template_path = os.path.join(
  10. test_runner_path, 'misago/project_template/project_name')
  11. avatars_store_path = os.path.join(
  12. test_runner_path, 'misago/project_template/avatar_store')
  13. media_path = os.path.join(
  14. test_runner_path, 'misago/project_template/media')
  15. test_project_path = os.path.join(test_runner_path, "testproject")
  16. test_project_avatars_path = os.path.join(test_project_path, "avatar_store")
  17. test_project_media_path = os.path.join(test_project_path, "media")
  18. if not os.path.exists(test_project_path):
  19. shutil.copytree(project_template_path, test_project_path)
  20. shutil.copytree(avatars_store_path, test_project_avatars_path)
  21. shutil.copytree(media_path, test_project_media_path)
  22. settings_path = os.path.join(test_project_path, "settings.py")
  23. with open(settings_path, "r") as py_file:
  24. settings_file = py_file.read()
  25. # Do some configuration magic
  26. settings_file = settings_file.replace("{{ project_name }}",
  27. "testproject")
  28. settings_file = settings_file.replace("{{ secret_key }}",
  29. "t3stpr0j3ct")
  30. settings_file += """
  31. MISAGO_NEW_REGISTRATIONS_VALIDATORS = ()
  32. EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
  33. CACHES = {
  34. 'default': {
  35. 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  36. 'LOCATION': 'uniqu3-sn0wf14k3'
  37. }
  38. }
  39. """
  40. settings_file += """
  41. PASSWORD_HASHERS = (
  42. 'django.contrib.auth.hashers.MD5PasswordHasher',
  43. )
  44. """
  45. if os.environ.get('TRAVIS'):
  46. settings_file += """
  47. DATABASES = {
  48. 'default': {
  49. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  50. 'NAME': 'travis_ci_test',
  51. 'USER': 'postgres',
  52. 'PASSWORD': '',
  53. 'HOST': '127.0.0.1',
  54. 'PORT': '',
  55. }
  56. }
  57. TEST_NAME = 'travis_ci_test'
  58. """
  59. else:
  60. settings_file += """
  61. DATABASES = {
  62. 'default': {
  63. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  64. 'NAME': 'misago_postgres',
  65. 'USER': '%s',
  66. 'PASSWORD': '',
  67. 'HOST': '',
  68. 'PORT': '',
  69. }
  70. }
  71. """ % pwd.getpwuid(os.getuid())[0]
  72. with open(settings_path, "w") as py_file:
  73. py_file.write(settings_file)
  74. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
  75. setup()
  76. setup_test_environment()
  77. if __name__ == '__main__':
  78. args = sys.argv[1:]
  79. else:
  80. args = []
  81. from django.core.management.commands import test
  82. sys.exit(test.Command().execute(*args, verbosity=1))
  83. if __name__ == '__main__':
  84. runtests()