runtests.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. args, kwargs = parse_args()
  9. setup_testproject()
  10. run_django(*args, **kwargs)
  11. def parse_args():
  12. args = []
  13. kwargs = {
  14. 'verbosity': 1,
  15. 'noinput': True,
  16. }
  17. sys_args = sys.argv
  18. if __name__ == '__main__':
  19. # strip argv with script name
  20. sys_args = sys_args[1:]
  21. for arg in sys_args:
  22. if arg == '--verbose':
  23. kwargs['verbosity'] = 2
  24. else:
  25. args.append(arg)
  26. return args, kwargs
  27. def setup_testproject():
  28. test_runner_path = os.path.dirname(os.path.abspath(__file__))
  29. project_template_path = os.path.join(test_runner_path, 'misago/project_template')
  30. project_package_path = os.path.join(test_runner_path, 'misago/project_template/project_name')
  31. test_project_path = os.path.join(test_runner_path, "testproject")
  32. if os.path.exists(test_project_path):
  33. shutil.rmtree(test_project_path)
  34. shutil.copytree(project_template_path, test_project_path)
  35. module_init_path = os.path.join(test_project_path, '__init__.py')
  36. with open(module_init_path, "w") as py_file:
  37. py_file.write('')
  38. settings_path = os.path.join(
  39. test_project_path, 'project_name', 'settings.py')
  40. with open(settings_path, "r") as py_file:
  41. settings_file = py_file.read()
  42. # Do some configuration magic
  43. settings_file = settings_file.replace('{{ project_name }}', 'testproject.project_name')
  44. settings_file = settings_file.replace('{{ secret_key }}', 't3stpr0j3ct')
  45. settings_file += """
  46. # disable account validation via API's
  47. MISAGO_NEW_REGISTRATIONS_VALIDATORS = ()
  48. # store mails in memory
  49. EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
  50. # use in-memory cache
  51. CACHES = {
  52. 'default': {
  53. 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  54. 'LOCATION': 'uniqu3-sn0wf14k3'
  55. }
  56. }
  57. # Use MD5 password hashing to speed up test suite
  58. PASSWORD_HASHERS = (
  59. 'django.contrib.auth.hashers.MD5PasswordHasher',
  60. )
  61. """
  62. if os.environ.get('TRAVIS'):
  63. settings_file += """
  64. DATABASES = {
  65. 'default': {
  66. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  67. 'NAME': 'travis_ci_test',
  68. 'USER': 'postgres',
  69. 'PASSWORD': '',
  70. 'HOST': '127.0.0.1',
  71. 'PORT': '',
  72. }
  73. }
  74. TEST_NAME = 'travis_ci_test'
  75. """
  76. else:
  77. settings_file += """
  78. DATABASES = {
  79. 'default': {
  80. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  81. 'NAME': 'misago_test',
  82. 'USER': '%s',
  83. 'PASSWORD': '',
  84. 'HOST': '',
  85. 'PORT': '',
  86. }
  87. }
  88. """ % pwd.getpwuid(os.getuid())[0]
  89. with open(settings_path, "w") as py_file:
  90. py_file.write(settings_file)
  91. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.project_name.settings")
  92. def run_django(*args, **kwargs):
  93. setup()
  94. setup_test_environment()
  95. from django.core.management.commands import test
  96. sys.exit(test.Command().execute(*args, **kwargs))
  97. if __name__ == '__main__':
  98. runtests()