File: test_exception_handler.py

package info (click to toggle)
dials 3.25.0%2Bdfsg3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,112 kB
  • sloc: python: 134,740; cpp: 34,526; makefile: 160; sh: 142
file content (62 lines) | stat: -rw-r--r-- 1,727 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

import os
import sys

import pytest

import dials.util


class _SomeError(RuntimeError):
    pass


def test_crash_with_plain_text():
    with pytest.raises(_SomeError) as exc:
        with dials.util.show_mail_on_error():
            raise _SomeError("This is a string")
    assert "report this error" in str(exc.value)
    assert "This is a string" in str(exc.value)


def test_crash_with_unicode():
    with pytest.raises(_SomeError) as exc:
        with dials.util.show_mail_on_error():
            raise _SomeError("This is a 👹♔  𝓕คnc¥ uηเᶜόD𝔼  🏆🍔 string")
    assert "report this error" in str(exc.value)
    assert "This is a" in str(exc.value)


def test_crash_with_bytestring():
    with pytest.raises(_SomeError) as exc:
        with dials.util.show_mail_on_error():
            raise _SomeError(b"This is a byte string")
    assert "report this error" in repr(exc.value)
    assert "This is a" in repr(exc.value)


def test_coloured_exit(monkeypatch):
    with pytest.raises(SystemExit) as e:
        with dials.util.make_sys_exit_red():
            sys.exit("Ohno")
    assert e.value.code == "Ohno"

    class _AttySysErr:
        def isatty(self):
            return True

    # Force colouring on
    monkeypatch.setattr(sys, "stderr", _AttySysErr())

    with pytest.raises(SystemExit) as e:
        with dials.util.make_sys_exit_red():
            sys.exit("Ohno")
    assert str(e.value.code).startswith("\033[")

    # and off again...
    monkeypatch.setitem(os.environ, "NO_COLOR", "1")
    with pytest.raises(SystemExit) as e:
        with dials.util.make_sys_exit_red():
            sys.exit("Ohno")
    assert e.value.code == "Ohno"