conftest.py 3.4 KB

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