test_windows.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # coding=utf-8
  2. import errno
  3. import os
  4. import pytest
  5. import six
  6. import library.python.strings
  7. import library.python.windows
  8. def gen_error_access_denied():
  9. if library.python.windows.on_win():
  10. err = WindowsError()
  11. err.errno = errno.EACCES
  12. err.strerror = ''
  13. err.winerror = library.python.windows.ERRORS['ACCESS_DENIED']
  14. else:
  15. err = OSError()
  16. err.errno = errno.EACCES
  17. err.strerror = os.strerror(err.errno)
  18. err.filename = 'unknown/file'
  19. raise err
  20. def test_errorfix_buggy():
  21. @library.python.windows.errorfix
  22. def erroneous_func():
  23. gen_error_access_denied()
  24. with pytest.raises(OSError) as errinfo:
  25. erroneous_func()
  26. assert errinfo.value.errno == errno.EACCES
  27. assert errinfo.value.filename == 'unknown/file'
  28. assert isinstance(errinfo.value.strerror, six.string_types)
  29. assert errinfo.value.strerror
  30. def test_errorfix_explicit():
  31. @library.python.windows.errorfix
  32. def erroneous_func():
  33. if library.python.windows.on_win():
  34. err = WindowsError()
  35. err.winerror = library.python.windows.ERRORS['ACCESS_DENIED']
  36. else:
  37. err = OSError()
  38. err.errno = errno.EACCES
  39. err.strerror = 'Some error description'
  40. err.filename = 'unknown/file'
  41. raise err
  42. with pytest.raises(OSError) as errinfo:
  43. erroneous_func()
  44. assert errinfo.value.errno == errno.EACCES
  45. assert errinfo.value.filename == 'unknown/file'
  46. assert errinfo.value.strerror == 'Some error description'
  47. def test_errorfix_decoding_cp1251():
  48. @library.python.windows.errorfix
  49. def erroneous_func():
  50. model_msg = u'Какое-то описание ошибки'
  51. if library.python.windows.on_win():
  52. err = WindowsError()
  53. err.strerror = library.python.strings.to_str(model_msg, 'cp1251')
  54. else:
  55. err = OSError()
  56. err.strerror = library.python.strings.to_str(model_msg)
  57. raise err
  58. with pytest.raises(OSError) as errinfo:
  59. erroneous_func()
  60. error_msg = errinfo.value.strerror
  61. if not isinstance(errinfo.value.strerror, six.text_type):
  62. error_msg = library.python.strings.to_unicode(error_msg)
  63. assert error_msg == u'Какое-то описание ошибки'
  64. def test_diehard():
  65. @library.python.windows.diehard(library.python.windows.ERRORS['ACCESS_DENIED'], tries=5)
  66. def erroneous_func(errors):
  67. try:
  68. gen_error_access_denied()
  69. except Exception as e:
  70. errors.append(e)
  71. raise
  72. raised_errors = []
  73. with pytest.raises(OSError) as errinfo:
  74. erroneous_func(raised_errors)
  75. assert errinfo.value.errno == errno.EACCES
  76. assert any(e.errno == errno.EACCES for e in raised_errors)
  77. assert raised_errors and errinfo.value == raised_errors[-1]
  78. if library.python.windows.on_win():
  79. assert len(raised_errors) == 5
  80. else:
  81. assert len(raised_errors) == 1