aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/windows/ut/test_windows.py
blob: beaa5fc3aa3a262ca61be1e09e480bcc09a08d54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# coding=utf-8

import errno
import os
import pytest
import six

import library.python.strings
import library.python.windows


def gen_error_access_denied():
    if library.python.windows.on_win():
        err = WindowsError()
        err.errno = errno.EACCES
        err.strerror = ''
        err.winerror = library.python.windows.ERRORS['ACCESS_DENIED']
    else:
        err = OSError()
        err.errno = errno.EACCES
        err.strerror = os.strerror(err.errno)
    err.filename = 'unknown/file'
    raise err


def test_errorfix_buggy():
    @library.python.windows.errorfix
    def erroneous_func():
        gen_error_access_denied()

    with pytest.raises(OSError) as errinfo:
        erroneous_func()
    assert errinfo.value.errno == errno.EACCES
    assert errinfo.value.filename == 'unknown/file'
    assert isinstance(errinfo.value.strerror, six.string_types)
    assert errinfo.value.strerror


def test_errorfix_explicit():
    @library.python.windows.errorfix
    def erroneous_func():
        if library.python.windows.on_win():
            err = WindowsError()
            err.winerror = library.python.windows.ERRORS['ACCESS_DENIED']
        else:
            err = OSError()
        err.errno = errno.EACCES
        err.strerror = 'Some error description'
        err.filename = 'unknown/file'
        raise err

    with pytest.raises(OSError) as errinfo:
        erroneous_func()
    assert errinfo.value.errno == errno.EACCES
    assert errinfo.value.filename == 'unknown/file'
    assert errinfo.value.strerror == 'Some error description'


def test_errorfix_decoding_cp1251():
    @library.python.windows.errorfix
    def erroneous_func():
        model_msg = u'Какое-то описание ошибки'
        if library.python.windows.on_win():
            err = WindowsError()
            err.strerror = library.python.strings.to_str(model_msg, 'cp1251')
        else:
            err = OSError()
            err.strerror = library.python.strings.to_str(model_msg)
        raise err

    with pytest.raises(OSError) as errinfo:
        erroneous_func()
    error_msg = errinfo.value.strerror
    if not isinstance(errinfo.value.strerror, six.text_type):
        error_msg = library.python.strings.to_unicode(error_msg)
    assert error_msg == u'Какое-то описание ошибки'


def test_diehard():
    @library.python.windows.diehard(library.python.windows.ERRORS['ACCESS_DENIED'], tries=5)
    def erroneous_func(errors):
        try:
            gen_error_access_denied()
        except Exception as e:
            errors.append(e)
            raise

    raised_errors = []
    with pytest.raises(OSError) as errinfo:
        erroneous_func(raised_errors)
    assert errinfo.value.errno == errno.EACCES
    assert any(e.errno == errno.EACCES for e in raised_errors)
    assert raised_errors and errinfo.value == raised_errors[-1]
    if library.python.windows.on_win():
        assert len(raised_errors) == 5
    else:
        assert len(raised_errors) == 1