runtests.py 2.8 KB

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