runtests.py 2.6 KB

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