forked from fedora-modularity/libmodulemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
124 lines (107 loc) · 4.3 KB
/
base.py
File metadata and controls
124 lines (107 loc) · 4.3 KB
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
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python3
# This file is part of libmodulemd
# Copyright (C) 2018 Red Hat, Inc.
#
# Fedora-License-Identifier: MIT
# SPDX-2.0-License-Identifier: MIT
# SPDX-3.0-License-Identifier: MIT
#
# This program is free software.
# For more information on the license, see COPYING.
# For more information on free software, see
# <https://www.gnu.org/philosophy/free-sw.en.html>.
from contextlib import contextmanager
import signal
import os
import unittest
class TestBase(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestBase, self).__init__(*args, **kwargs)
self._caught_signal = False
@property
def source_root(self):
return os.getenv("MESON_SOURCE_ROOT")
@property
def test_data_path(self):
return os.getenv("TEST_DATA_PATH")
def _catch_signal(self, *sigargs):
if self._caught_signal:
raise AssertionError("Multiple signals were caught")
self._caught_signal = True
@contextmanager
def expect_signal(
self, expected_signal=signal.SIGTRAP, only_on_fatal_warnings=False
):
expect_signal = (not only_on_fatal_warnings) or self.warnings_fatal
self._caught_signal = False
saved_signal = signal.signal(expected_signal, self._catch_signal)
yield None
signal.signal(expected_signal, saved_signal)
if not self._caught_signal and expect_signal:
raise AssertionError("No signal got caught")
elif self._caught_signal and not expect_signal:
raise AssertionError("Signal caught in non-warning state")
def assertProcessFailure(self, callable, *args):
"""Calls the callable in a subprocess and checks whether the process was
killed with a signal depending on Glib warning fatality."""
pid = os.fork()
if pid == 0:
callable(*args)
os._exit(0)
_, status = os.waitpid(pid, 0)
if self.warnings_fatal:
if not os.WIFSIGNALED(status):
raise AssertionError("Child process was not aborted")
else:
if os.WIFSIGNALED(status):
raise AssertionError("Child process was unexpectedly aborted")
def assertTypeExceptionOrProcessFailure(self, callable, *args):
"""Calls the callable in a subprocess and checks that the process
raised a TypeError exception, or was killed depending on Glib warning
fatality.
Since pygobject-3.55.0 setting a G_PARAM_CONSTRUCT_ONLY property
raises a Python exception. Old pygobject continues down to Glib
which kills the process if Glib warnings a fatal, otherwise Glib
warning is printed and the code continues.
"""
pid = os.fork()
if pid == 0:
try:
callable(*args)
except TypeError:
os._exit(1)
os._exit(0)
_, status = os.waitpid(pid, 0)
if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 1:
return
if self.warnings_fatal:
if not os.WIFSIGNALED(status):
raise AssertionError(
"Child process did not raise TypeError "
"exception or was not aborted"
)
else:
if os.WIFSIGNALED(status):
raise AssertionError("Child process was unexpectedly aborted")
@property
def warnings_fatal(self):
gdebug = os.getenv("G_DEBUG", "").split(",")
return "fatal-warnings" in gdebug
def assertRaisesRegex(self, *args, **kwargs):
"""Asserts that the message in a raised exception matches a regex.
Args:
The same as unittest.TestCase.assertRaisesRegex().
"""
try:
return super(TestBase, self).assertRaisesRegex(*args, **kwargs)
except AttributeError:
return self.assertRaisesRegexp(*args, **kwargs)
def assertRaisesRegexOrDies(self, callable, *args, **kwargs):
"""Checks that the callable terminates a process if Glib warnings are
fatal. Otherwise, that the callable raised a given exception type with
the given value matching a regular expression."""
if self.warnings_fatal:
self.assertProcessFailure(callable)
else:
with self.assertRaisesRegex(*args, **kwargs):
callable()