runtests.py 2.9 KB

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