test_traceback.py 1.7 KB

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