runtests.py 3.1 KB

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