runtests.py 3.1 KB

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