runtests.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if os.environ.get('TRAVIS'):
  41. settings_file += """
  42. DATABASES = {
  43. 'default': {
  44. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  45. 'NAME': 'travis_ci_test',
  46. 'USER': 'postgres',
  47. 'PASSWORD': '',
  48. 'HOST': '127.0.0.1',
  49. 'PORT': '',
  50. }
  51. }
  52. TEST_NAME = 'travis_ci_test'
  53. """
  54. else:
  55. settings_file += """
  56. DATABASES = {
  57. 'default': {
  58. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  59. 'NAME': 'misago_postgres',
  60. 'USER': '%s',
  61. 'PASSWORD': '',
  62. 'HOST': '',
  63. 'PORT': '',
  64. }
  65. }
  66. """ % pwd.getpwuid(os.getuid())[0]
  67. with open(settings_path, "w") as py_file:
  68. py_file.write(settings_file)
  69. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
  70. setup()
  71. setup_test_environment()
  72. if __name__ == '__main__':
  73. args = sys.argv[1:]
  74. else:
  75. args = []
  76. from django.core.management.commands import test
  77. sys.exit(test.Command().execute(*args, verbosity=1))
  78. if __name__ == '__main__':
  79. runtests()