@@ -89,14 +89,18 @@ def test_load_libgmt_with_a_bad_library_path(monkeypatch):
89
89
assert check_libgmt (load_libgmt ()) is None
90
90
91
91
92
- def test_load_libgmt_with_broken_libraries ( monkeypatch ) :
92
+ class TestLibgmtBrokenLibs :
93
93
"""
94
- Test load_libgmt still works when a broken library is found.
94
+ Test that load_libgmt still works when a broken library is found.
95
95
"""
96
+
96
97
# load the GMT library before mocking the ctypes.CDLL function
97
98
loaded_libgmt = load_libgmt ()
99
+ invalid_path = "/invalid/path/to/libgmt.so"
100
+ faked_libgmt1 = FakedLibGMT ("/path/to/faked/libgmt1.so" )
101
+ faked_libgmt2 = FakedLibGMT ("/path/to/faked/libgmt2.so" )
98
102
99
- def mock_ctypes_cdll_return ( libname ):
103
+ def _mock_ctypes_cdll_return ( self , libname ):
100
104
"""
101
105
Mock the return value of ctypes.CDLL.
102
106
@@ -119,54 +123,72 @@ def mock_ctypes_cdll_return(libname):
119
123
# raise OSError like the original ctypes.CDLL
120
124
raise OSError (f"Unable to find '{ libname } '" )
121
125
# libname is a loaded GMT library
122
- return loaded_libgmt
126
+ return self . loaded_libgmt
123
127
124
- with monkeypatch .context () as mpatch :
125
- # pylint: disable=protected-access
126
- # mock the ctypes.CDLL function using mock_ctypes_cdll_return()
127
- mpatch .setattr (ctypes , "CDLL" , mock_ctypes_cdll_return )
128
+ @pytest .fixture
129
+ def mock_ctypes (self , monkeypatch ):
130
+ monkeypatch .setattr (ctypes , "CDLL" , self ._mock_ctypes_cdll_return )
128
131
129
- faked_libgmt1 = FakedLibGMT ("/path/to/faked/libgmt1.so" )
130
- faked_libgmt2 = FakedLibGMT ("/path/to/faked/libgmt2.so" )
132
+ def test_two_broken_libraries (self , mock_ctypes ):
133
+ """
134
+ Case 1: two broken libraries.
131
135
132
- # case 1: two broken libraries
133
- # Raise the GMTCLibNotFoundError exception
134
- # The error message should contain information of both libraries
135
- lib_fullnames = [faked_libgmt1 , faked_libgmt2 ]
136
+ Raise the GMTCLibNotFoundError exception. Error message should contain
137
+ information of both libraries that failed to load properly.
138
+ """
139
+ lib_fullnames = [self . faked_libgmt1 , self . faked_libgmt2 ]
136
140
msg_regex = (
137
- fr"Error loading GMT shared library at '{ faked_libgmt1 ._name } '.\n"
138
- fr"Error loading '{ faked_libgmt1 ._name } '. Couldn't access.*\n"
139
- fr"Error loading GMT shared library at '{ faked_libgmt2 ._name } '.\n"
140
- fr "Error loading '{ faked_libgmt2 ._name } '. Couldn't access.*"
141
+ fr"Error loading GMT shared library at '{ self . faked_libgmt1 ._name } '.\n"
142
+ fr"Error loading '{ self . faked_libgmt1 ._name } '. Couldn't access.*\n"
143
+ fr"Error loading GMT shared library at '{ self . faked_libgmt2 ._name } '.\n"
144
+ f "Error loading '{ self . faked_libgmt2 ._name } '. Couldn't access.*"
141
145
)
142
146
with pytest .raises (GMTCLibNotFoundError , match = msg_regex ):
143
147
load_libgmt (lib_fullnames = lib_fullnames )
144
148
145
- # case 2: broken library + invalid path
146
- lib_fullnames = [faked_libgmt1 , "/invalid/path/to/libgmt.so" ]
149
+ def test_load_brokenlib_invalidpath (self , mock_ctypes ):
150
+ """
151
+ Case 2: broken library + invalid path.
152
+
153
+ Raise the GMTCLibNotFoundError exception. Error message should contain
154
+ information of one library that failed to load and one invalid path.
155
+ """
156
+ lib_fullnames = [self .faked_libgmt1 , self .invalid_path ]
147
157
msg_regex = (
148
- fr"Error loading GMT shared library at '{ faked_libgmt1 ._name } '.\n"
149
- fr"Error loading '{ faked_libgmt1 ._name } '. Couldn't access.*\n"
150
- "Error loading GMT shared library at '/invalid/path/to/libgmt.so '.\n "
151
- "Unable to find '/invalid/path/to/libgmt.so '"
158
+ fr"Error loading GMT shared library at '{ self . faked_libgmt1 ._name } '.\n"
159
+ fr"Error loading '{ self . faked_libgmt1 ._name } '. Couldn't access.*\n"
160
+ fr "Error loading GMT shared library at '{ self . invalid_path } '.\n"
161
+ f "Unable to find '{ self . invalid_path } '"
152
162
)
153
163
with pytest .raises (GMTCLibNotFoundError , match = msg_regex ):
154
164
load_libgmt (lib_fullnames = lib_fullnames )
155
165
156
- # case 3: broken library + invalid path + working library
157
- lib_fullnames = [faked_libgmt1 , "/invalid/path/to/libgmt.so" , loaded_libgmt ]
166
+ def test_brokenlib_invalidpath_workinglib (self , mock_ctypes ):
167
+ """
168
+ Case 3: broken library + invalid path + working library.
169
+ """
170
+ lib_fullnames = [self .faked_libgmt1 , self .invalid_path , self .loaded_libgmt ]
158
171
assert check_libgmt (load_libgmt (lib_fullnames = lib_fullnames )) is None
159
172
160
- # case 4: invalid path + broken library + working library
161
- lib_fullnames = ["/invalid/path/to/libgmt.so" , faked_libgmt1 , loaded_libgmt ]
173
+ def test_invalidpath_brokenlib_workinglib (self , mock_ctypes ):
174
+ """
175
+ Case 4: invalid path + broken library + working library.
176
+ """
177
+ lib_fullnames = [self .invalid_path , self .faked_libgmt1 , self .loaded_libgmt ]
162
178
assert check_libgmt (load_libgmt (lib_fullnames = lib_fullnames )) is None
163
179
164
- # case 5: working library + broken library + invalid path
165
- lib_fullnames = [loaded_libgmt , faked_libgmt1 , "/invalid/path/to/libgmt.so" ]
180
+ def test_workinglib_brokenlib_invalidpath (self , mock_ctypes ):
181
+ """
182
+ Case 5: working library + broken library + invalid path.
183
+ """
184
+ lib_fullnames = [self .loaded_libgmt , self .faked_libgmt1 , self .invalid_path ]
166
185
assert check_libgmt (load_libgmt (lib_fullnames = lib_fullnames )) is None
167
186
168
- # case 6: repeating broken libraries + working library
169
- lib_fullnames = [faked_libgmt1 , faked_libgmt1 , loaded_libgmt ]
187
+ def test_brokenlib_brokenlib_workinglib (self , mock_ctypes ):
188
+ """
189
+ Case 6: repeating broken libraries + working library.
190
+ """
191
+ lib_fullnames = [self .faked_libgmt1 , self .faked_libgmt1 , self .loaded_libgmt ]
170
192
assert check_libgmt (load_libgmt (lib_fullnames = lib_fullnames )) is None
171
193
172
194
0 commit comments