Debian Codegraph @

envisage/tests/support.py

raw ◀ ▶
1 Debian patch touch this file
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 """
12 Support utilities for tests in the Envisage test suite.
13
14 Note: this is intended to provide helpers for testing Envisage itself rather
15 than for external Envisage-based code. The helpers here should be considered
16 private to Envisage.
17 """
18
19 import contextlib
20 import pathlib
21 import sys
22 import tempfile
23 import unittest
24
25 from pyface.api import GUI
26 from traits.api import Int, List
27
28 from envisage.api import Application, ExtensionPoint, Plugin
29
30 # Skip decorator for tests that require a working GUI instance.
31 try:
32 GUI()
33 except NotImplementedError:
34 gui_available = False
35 else:
36 gui_available = True
37
38 requires_gui = unittest.skipUnless(
39 gui_available, "Test requires a non-null GUI backend"
40 )
41
42
43 # Test for PySide6 being installed
44 try:
45 import PySide6
46 except ImportError:
47 pyside6_available = False
48 pyside6_version = None
49 else:
50 pyside6_available = True
51 pyside6_version = PySide6.__version_info__
52 del PySide6
53
54
55 # Various useful context managers.
56
57
58 @contextlib.contextmanager
59 def temporary_directory():
60 """
61 Create and remove a temporary directory.
62
63 Yields a pathlib.Path instance.
64 """
65 with tempfile.TemporaryDirectory() as tempdir:
66 yield pathlib.Path(tempdir)
67
68
69 @contextlib.contextmanager
70 def restore_sys_path():
71 """
72 Save and restore `sys.path` state.
73
74 On entering the associated context, this context manager saves the state of
75 `sys.path`. Within the context, code may then make changes to that global
76 state (for example by adding distributions to the working set). On exiting
77 the context, `sys.path` is restored to its original state.
78 """
79 original_sys_path = sys.path[:]
80 try:
81 yield
82 finally:
83 sys.path[:] = original_sys_path
84
85
86 @contextlib.contextmanager
87 def restore_sys_modules():
88 """
89 Save and restore `sys.modules` state.
90
91 On entering the associated context, this context manager saves the state of
92 `sys.modules`. On exiting the context, any additional keys that were added
93 to `sys.modules` are removed.
94 """
95 original_modules = set(sys.modules)
96 try:
97 yield
98 finally:
99 for name in sys.modules.keys() - original_modules:
100 del sys.modules[name]
101
102
103 # Application class used in various tests.
104
105
106 class SimpleApplication(Application):
107 """The type of application used in the tests."""
108
109 id = "test"
110
111
112 # Plugins used in multiple tests.
113
114
115 class PluginA(Plugin):
116 """A plugin that offers an extension point."""
117
118 id = "A"
119 x = ExtensionPoint(List, id="a.x")
120
121
122 class PluginB(Plugin):
123 """A plugin that contributes to an extension point."""
124
125 id = "B"
126 x = List(Int, [1, 2, 3], contributes_to="a.x")
127
128
129 class PluginC(Plugin):
130 """Another plugin that contributes to an extension point!"""
131
132 id = "C"
133 x = List(Int, [98, 99, 100], contributes_to="a.x")