conftest.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import os
  2. import sys
  3. from collections import OrderedDict
  4. import pytest
  5. pytest_plugins = ["sentry.utils.pytest"]
  6. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
  7. def pytest_configure(config):
  8. import warnings
  9. # XXX(dcramer): Kombu throws a warning due to transaction.commit_manually
  10. # being used
  11. warnings.filterwarnings("error", "", Warning, r"^(?!(|kombu|raven|sentry))")
  12. # XXX: The below code is vendored code from https://github.com/utgwkk/pytest-github-actions-annotate-failures
  13. # so that we can add support for pytest_rerunfailures
  14. # retried tests will no longer be annotated in GHA
  15. #
  16. # Reference:
  17. # https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks
  18. # https://docs.pytest.org/en/latest/writing_plugins.html#hook-function-ordering-call-example
  19. # https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_runtest_makereport
  20. #
  21. # Inspired by:
  22. # https://github.com/pytest-dev/pytest/blob/master/src/_pytest/terminal.py
  23. @pytest.hookimpl(tryfirst=True, hookwrapper=True)
  24. def pytest_runtest_makereport(item, call):
  25. # execute all other hooks to obtain the report object
  26. outcome = yield
  27. report = outcome.get_result()
  28. # enable only in a workflow of GitHub Actions
  29. # ref: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
  30. if os.environ.get("GITHUB_ACTIONS") != "true":
  31. return
  32. # If we have the pytest_rerunfailures plugin,
  33. # and there are still retries to be run,
  34. # then do not return the error
  35. if hasattr(item, "execution_count"):
  36. import pytest_rerunfailures
  37. if item.execution_count <= pytest_rerunfailures.get_reruns_count(item):
  38. return
  39. if report.when == "call" and report.failed:
  40. # collect information to be annotated
  41. filesystempath, lineno, _ = report.location
  42. # try to convert to absolute path in GitHub Actions
  43. workspace = os.environ.get("GITHUB_WORKSPACE")
  44. if workspace:
  45. full_path = os.path.abspath(filesystempath)
  46. try:
  47. rel_path = os.path.relpath(full_path, workspace)
  48. except ValueError:
  49. # os.path.relpath() will raise ValueError on Windows
  50. # when full_path and workspace have different mount points.
  51. # https://github.com/utgwkk/pytest-github-actions-annotate-failures/issues/20
  52. rel_path = filesystempath
  53. if not rel_path.startswith(".."):
  54. filesystempath = rel_path
  55. if lineno is not None:
  56. # 0-index to 1-index
  57. lineno += 1
  58. # get the name of the current failed test, with parametrize info
  59. longrepr = report.head_line or item.name
  60. # get the error message and line number from the actual error
  61. try:
  62. longrepr += "\n\n" + report.longrepr.reprcrash.message
  63. lineno = report.longrepr.reprcrash.lineno
  64. except AttributeError:
  65. pass
  66. print(_error_workflow_command(filesystempath, lineno, longrepr)) # noqa: B314
  67. def _error_workflow_command(filesystempath, lineno, longrepr):
  68. # Build collection of arguments. Ordering is strict for easy testing
  69. details_dict = OrderedDict()
  70. details_dict["file"] = filesystempath
  71. if lineno is not None:
  72. details_dict["line"] = lineno
  73. details = ",".join(f"{k}={v}" for k, v in details_dict.items())
  74. if longrepr is None:
  75. return f"\n::error {details}"
  76. else:
  77. longrepr = _escape(longrepr)
  78. return f"\n::error {details}::{longrepr}"
  79. def _escape(s):
  80. return s.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")