runtests.py 2.1 KB

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