File: test_errorreport.py

package info (click to toggle)
quodlibet 4.6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,016 kB
  • sloc: python: 85,817; sh: 385; xml: 110; makefile: 91
file content (113 lines) | stat: -rw-r--r-- 3,284 bytes parent folder | download | duplicates (3)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright 2016 Christoph Reiter
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import sys
import os
import shutil

from gi.repository import Gtk

from quodlibet.errorreport import faulthandling, enable_errorhook, errorhook
from quodlibet.errorreport.faulthandling import FaultHandlerCrash
from quodlibet.errorreport.logdump import dump_to_disk
from quodlibet.errorreport.ui import ErrorDialog, SubmitErrorDialog
from quodlibet.errorreport.main import get_sentry
from quodlibet.errorreport.sentrywrapper import SentryError, CapturedException

from . import TestCase, mkdtemp
from .helper import temp_filename


class Tfaulthandling(TestCase):

    def test_basic(self):
        with temp_filename() as filename:
            faulthandling.enable(filename)
            faulthandling.raise_and_clear_error()
            faulthandling.disable()

    def test_error(self):
        with temp_filename() as filename:
            with open(filename, "wb") as h:
                h.write(b"something")
            faulthandling.enable(filename)
            with self.assertRaises(FaultHandlerCrash):
                faulthandling.raise_and_clear_error()
            faulthandling.disable()

    def test_stacktrace_grouping(self):
        stack1 = 'File "%s", line 486 in string_at' % (
            os.path.join("foo", "bar", "quux.py"),)
        stack2 = 'File "%s", line 350 in string_at' % (
            os.path.join("baz", "bar", "quux.py"),)
        stack3 = 'File "%s", line 350 in other' % (
            os.path.join("baz", "bar", "quux.py"),)

        key1 = FaultHandlerCrash(stack1).get_grouping_key()
        key2 = FaultHandlerCrash(stack2).get_grouping_key()
        key3 = FaultHandlerCrash(stack3).get_grouping_key()

        assert key1 and key2 and key3
        assert key1 == key2 != key3


class Tlogdump(TestCase):

    def test_main(self):
        temp_dir = mkdtemp()
        try:
            dump_dir = os.path.join(temp_dir, "dump")

            try:
                raise Exception("foo")
            except Exception:
                dump_to_disk(dump_dir, sys.exc_info())

            assert len(os.listdir(dump_dir)) == 1
        finally:
            shutil.rmtree(temp_dir)


class Terrorui(TestCase):

    def test_main(self):
        w = Gtk.Window()
        ErrorDialog(w, u"foo").destroy()
        ErrorDialog(w, u"foo").destroy()
        SubmitErrorDialog(w, u"foo").destroy()


class Terrorreport(TestCase):

    def test_enable(self):
        enable_errorhook(True)
        enable_errorhook(False)
        try:
            raise Exception
        except Exception:
            errorhook()


class Tsentrywrapper(TestCase):

    def test_main(self):
        sentry = get_sentry()
        try:
            raise Exception
        except Exception:
            exc_info = sys.exc_info()

        try:
            err = sentry.capture(exc_info)
        except SentryError:
            return

        assert isinstance(err, CapturedException)
        assert isinstance(err.get_report(), str)

        err.set_comment(u"foo")
        err.set_comment(u"bar")