File: test_os_error.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 2,043 bytes parent folder | download
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
import random
import pytest
import friendly_traceback


@pytest.mark.skipif(random.randint(0, 50) < 59, reason="very long test")
def test_Urllib_error():
    from urllib import request, error
    try:
        request.urlopen("http://does_not_exist")
    except error.URLError as e:
        message = str(e)
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output()

    assert "URLError" in result
    if friendly_traceback.get_lang() == "en":
        assert "An exception of type `URLError` is a subclass of `OSError`." in result
        assert "I suspect that you are trying to connect to a server" in result
    if friendly_traceback._writing_docs:
        return result, message


def test_no_information():
    # simulate an unknown OSError
    # We silence the message about a new case to consider
    old_debug = friendly_traceback.debug_helper.DEBUG
    friendly_traceback.debug_helper.DEBUG = False
    try:
        raise OSError("Some unknown message")
    except OSError as e:
        message = str(e)
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output()

    lines = friendly_traceback.ft_gettext.no_information().split("\n")
    if friendly_traceback.get_lang() == "en":
        for line in lines:
            assert line in result
    friendly_traceback.debug_helper.DEBUG = old_debug
    if friendly_traceback._writing_docs:
        return result, message


def test_invalid_argument():
    import os
    if os.name != "nt":
        return "Windows test only", "No result"
    try:
        open("c:\test.txt")
    except OSError as e:
        message = str(e)
        friendly_traceback.explain_traceback(redirect="capture")

    result = friendly_traceback.get_output()
    assert "Invalid argument" in result
    if friendly_traceback.get_lang() == "en":
        assert "front of the filename or path, or replace all single backslash" in result
    if friendly_traceback._writing_docs:
        return result, message