runtests.py 2.7 KB

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