runtests.py 2.4 KB

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