runtests.py 2.3 KB

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