runtests.py 2.9 KB

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