runtests.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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')
  11. project_package_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. for filename in os.listdir(project_package_path):
  17. src_path = os.path.join(project_package_path, filename)
  18. dst_path = os.path.join(test_project_path, filename)
  19. shutil.copy2(src_path, dst_path)
  20. settings_path = os.path.join(test_project_path, "settings.py")
  21. with open(settings_path, "r") as py_file:
  22. settings_file = py_file.read()
  23. # Do some configuration magic
  24. settings_file = settings_file.replace(
  25. "os.path.dirname(os.path.dirname(__file__))",
  26. "os.path.dirname(__file__)")
  27. settings_file = settings_file.replace("{{ project_name }}",
  28. "testproject")
  29. settings_file = settings_file.replace("{{ secret_key }}",
  30. "t3stpr0j3ct")
  31. settings_file += """
  32. # disable account validation via API's
  33. MISAGO_NEW_REGISTRATIONS_VALIDATORS = ()
  34. # store mails in memory
  35. EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
  36. # use in-memory cache
  37. CACHES = {
  38. 'default': {
  39. 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  40. 'LOCATION': 'uniqu3-sn0wf14k3'
  41. }
  42. }
  43. # Use MD5 password hashing to speed up test suite
  44. PASSWORD_HASHERS = (
  45. 'django.contrib.auth.hashers.MD5PasswordHasher',
  46. )
  47. """
  48. if os.environ.get('TRAVIS'):
  49. settings_file += """
  50. DATABASES = {
  51. 'default': {
  52. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  53. 'NAME': 'travis_ci_test',
  54. 'USER': 'postgres',
  55. 'PASSWORD': '',
  56. 'HOST': '127.0.0.1',
  57. 'PORT': '',
  58. }
  59. }
  60. TEST_NAME = 'travis_ci_test'
  61. """
  62. else:
  63. settings_file += """
  64. DATABASES = {
  65. 'default': {
  66. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  67. 'NAME': 'misago_postgres',
  68. 'USER': '%s',
  69. 'PASSWORD': '',
  70. 'HOST': '',
  71. 'PORT': '',
  72. }
  73. }
  74. """ % pwd.getpwuid(os.getuid())[0]
  75. with open(settings_path, "w") as py_file:
  76. py_file.write(settings_file)
  77. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
  78. setup()
  79. setup_test_environment()
  80. if __name__ == '__main__':
  81. args = sys.argv[1:]
  82. else:
  83. args = []
  84. from django.core.management.commands import test
  85. sys.exit(test.Command().execute(*args, verbosity=1, noinput=True))
  86. if __name__ == '__main__':
  87. runtests()