runtests.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. import argparse
  3. import os
  4. import shutil
  5. import sys
  6. import warnings
  7. from django.core.management import execute_from_command_line
  8. os.environ["DJANGO_SETTINGS_MODULE"] = "wagtail.test.settings"
  9. def make_parser():
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument(
  12. "--deprecation",
  13. choices=["all", "pending", "imminent", "none"],
  14. default="imminent",
  15. )
  16. parser.add_argument("--postgres", action="store_true")
  17. parser.add_argument("--elasticsearch7", action="store_true")
  18. parser.add_argument("--elasticsearch8", action="store_true")
  19. parser.add_argument("--emailuser", action="store_true")
  20. parser.add_argument("--disabletimezone", action="store_true")
  21. parser.add_argument("--bench", action="store_true")
  22. return parser
  23. def parse_args(args=None):
  24. return make_parser().parse_known_args(args)
  25. def runtests():
  26. args, rest = parse_args()
  27. only_wagtail = r"^wagtail(\.|$)"
  28. if args.deprecation == "all":
  29. # Show all deprecation warnings from all packages
  30. warnings.simplefilter("default", DeprecationWarning)
  31. warnings.simplefilter("default", PendingDeprecationWarning)
  32. elif args.deprecation == "pending":
  33. # Show all deprecation warnings from wagtail
  34. warnings.filterwarnings(
  35. "default", category=DeprecationWarning, module=only_wagtail
  36. )
  37. warnings.filterwarnings(
  38. "default", category=PendingDeprecationWarning, module=only_wagtail
  39. )
  40. elif args.deprecation == "imminent":
  41. # Show only imminent deprecation warnings from wagtail
  42. warnings.filterwarnings(
  43. "default", category=DeprecationWarning, module=only_wagtail
  44. )
  45. elif args.deprecation == "none":
  46. # Deprecation warnings are ignored by default
  47. pass
  48. if args.postgres:
  49. os.environ["DATABASE_ENGINE"] = "django.db.backends.postgresql"
  50. elif args.elasticsearch7:
  51. os.environ.setdefault("ELASTICSEARCH_URL", "http://localhost:9200")
  52. os.environ.setdefault("ELASTICSEARCH_VERSION", "7")
  53. elif args.elasticsearch8:
  54. os.environ.setdefault("ELASTICSEARCH_URL", "http://localhost:9200")
  55. os.environ.setdefault("ELASTICSEARCH_VERSION", "8")
  56. elif "ELASTICSEARCH_URL" in os.environ:
  57. # forcibly delete the ELASTICSEARCH_URL setting to skip those tests
  58. del os.environ["ELASTICSEARCH_URL"]
  59. if args.emailuser:
  60. os.environ["USE_EMAIL_USER_MODEL"] = "1"
  61. if args.disabletimezone:
  62. os.environ["DISABLE_TIMEZONE"] = "1"
  63. if args.bench:
  64. benchmarks = [
  65. "wagtail.admin.tests.benches",
  66. ]
  67. argv = [sys.argv[0], "test", "-v2"] + benchmarks + rest
  68. else:
  69. argv = [sys.argv[0], "test"] + rest
  70. try:
  71. execute_from_command_line(argv)
  72. finally:
  73. from wagtail.test.settings import MEDIA_ROOT, STATIC_ROOT
  74. shutil.rmtree(STATIC_ROOT, ignore_errors=True)
  75. shutil.rmtree(MEDIA_ROOT, ignore_errors=True)
  76. if __name__ == "__main__":
  77. runtests()