test_traceback.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. import re
  3. import pytest
  4. import yatest.common as yc
  5. def clean_traceback(traceback):
  6. traceback = re.sub(rb"\033\[(\d|;)+?m", b"", traceback) # strip ANSI codes
  7. traceback = re.sub(rb" at 0x[0-9a-fA-F]+", b"", traceback) # remove object ids
  8. return traceback
  9. @pytest.mark.parametrize(
  10. "mode",
  11. [
  12. "default",
  13. "ultratb_color",
  14. "ultratb_verbose",
  15. ],
  16. )
  17. @pytest.mark.parametrize(
  18. "entry_point",
  19. [
  20. "main",
  21. "custom",
  22. ],
  23. )
  24. def test_traceback(mode, entry_point):
  25. tb_tool = yc.build_path("library/python/runtime_py3/test/traceback/traceback")
  26. stdout_path = yc.test_output_path("stdout_raw.txt")
  27. stderr_path = yc.test_output_path("stderr_raw.txt")
  28. filtered_stdout_path = yc.test_output_path("stdout.txt")
  29. filtered_stderr_path = yc.test_output_path("stderr.txt")
  30. env = os.environ.copy()
  31. env.pop("PYTHONPATH", None) # Do not let program peek into its sources on filesystem
  32. if entry_point == "custom":
  33. env["Y_PYTHON_ENTRY_POINT"] = "library.python.runtime_py3.test.traceback.crash:main"
  34. proc = yc.execute(
  35. command=[tb_tool, mode],
  36. env=env,
  37. stdout=stdout_path,
  38. stderr=stderr_path,
  39. check_exit_code=False,
  40. )
  41. with open(filtered_stdout_path, "wb") as f:
  42. f.write(clean_traceback(proc.std_out))
  43. with open(filtered_stderr_path, "wb") as f:
  44. f.write(clean_traceback(proc.std_err))
  45. return {
  46. "stdout": yc.canonical_file(
  47. filtered_stdout_path,
  48. local=True,
  49. ),
  50. "stderr": yc.canonical_file(
  51. filtered_stderr_path,
  52. local=True,
  53. ),
  54. }