runtests.py 3.0 KB

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