runtests.py 3.1 KB

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