File: test_error.py

package info (click to toggle)
python-py 1.11.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,512 kB
  • sloc: python: 11,660; makefile: 119; sh: 7
file content (76 lines) | stat: -rw-r--r-- 2,017 bytes parent folder | download | duplicates (31)
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

import py

import errno
import sys
import subprocess


def test_error_classes():
    for name in errno.errorcode.values():
        x = getattr(py.error, name)
        assert issubclass(x, py.error.Error)
        assert issubclass(x, EnvironmentError)


def test_has_name():
    assert py.error.__name__ == 'py.error'


def test_picklability_issue1():
    import pickle
    e1 = py.error.ENOENT()
    s = pickle.dumps(e1)
    e2 = pickle.loads(s)
    assert isinstance(e2, py.error.ENOENT)


def test_unknown_error():
    num = 3999
    cls = py.error._geterrnoclass(num)
    assert cls.__name__ == 'UnknownErrno%d' % (num,)
    assert issubclass(cls, py.error.Error)
    assert issubclass(cls, EnvironmentError)
    cls2 = py.error._geterrnoclass(num)
    assert cls is cls2


def test_error_conversion_enotdir(testdir):
    p = testdir.makepyfile("")
    excinfo = py.test.raises(py.error.Error, py.error.checked_call, p.listdir)
    assert isinstance(excinfo.value, EnvironmentError)
    assert isinstance(excinfo.value, py.error.Error)
    assert "ENOTDIR" in repr(excinfo.value)


def test_checked_call_supports_kwargs(tmpdir):
    import tempfile
    py.error.checked_call(tempfile.mkdtemp, dir=str(tmpdir))


def test_error_importable():
    """Regression test for #179"""
    subprocess.check_call(
        [sys.executable, '-c', 'from py.error import ENOENT'])


try:
    import unittest
    unittest.TestCase.assertWarns
except (ImportError, AttributeError):
    pass  # required interface not available
else:
    import sys
    import warnings

    class Case(unittest.TestCase):
        def test_assert_warns(self):
            # Clear everything "py.*" from sys.modules and re-import py
            # as a fresh start
            for mod in tuple(sys.modules.keys()):
                if mod and (mod == 'py' or mod.startswith('py.')):
                    del sys.modules[mod]
            __import__('py')

            with self.assertWarns(UserWarning):
                warnings.warn('this should work')