conftest.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. import subprocess
  3. import sys
  4. import time
  5. from django.conf import settings
  6. from sentry.testutils.pytest.sentry import configure_split_db
  7. from sentry.utils import json
  8. def pytest_configure(config):
  9. """
  10. Generate frontend assets before running any acceptance tests
  11. TODO: There is a bug if you run `py.test` with `-f` -- the built
  12. assets will trigger another `py.test` run.
  13. """
  14. # Do not build in CI because tests are run w/ `make test-acceptance` which builds assets
  15. # Can also skip with the env var `SKIP_ACCEPTANCE_UI_BUILD`
  16. # `CI` is a default env var in GHA (https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables)
  17. if os.environ.get("CI") or os.environ.get("SKIP_ACCEPTANCE_UI_BUILD"):
  18. return
  19. try:
  20. with open("./.webpack.meta") as f:
  21. data = json.load(f)
  22. # If built within last hour, do not build again
  23. last_built = int(time.time()) - data["built"]
  24. if last_built <= 3600:
  25. print( # noqa: S002
  26. f"""
  27. ###################
  28. #
  29. # Frontend assets last built {last_built} seconds ago, skipping rebuilds for another {3600 - last_built} seconds.
  30. # Delete the file: `.webpack.meta` to rebuild.
  31. #
  32. ###################
  33. """,
  34. file=sys.stderr,
  35. )
  36. return
  37. except OSError:
  38. pass
  39. except Exception:
  40. pass
  41. print( # noqa: S002
  42. """
  43. ###################
  44. #
  45. # Running webpack to compile frontend assets - this will take awhile
  46. #
  47. ###################
  48. """
  49. )
  50. try:
  51. status = subprocess.call(
  52. ["yarn", "build-acceptance"],
  53. env={"PATH": os.environ["PATH"], "NODE_OPTIONS": "--max-old-space-size=4096"},
  54. )
  55. if status != 0:
  56. raise Exception(
  57. "Unable to run `webpack` -- make sure your development environment is setup correctly: https://docs.sentry.io/development/contribute/environment/#macos---nodejs"
  58. )
  59. except OSError:
  60. raise Exception(
  61. "Unable to run `yarn` -- make sure your development environment is setup correctly: https://docs.sentry.io/development/contribute/environment/#macos---nodejs"
  62. )
  63. configure_split_db(settings)