Debian Codegraph @

python/dolfin_utils/test/fixtures.py

raw
1 # -*- coding: utf-8 -*-
2 """Shared fixtures for unit tests involving dolfin."""
3
4 # Copyright (C) 2014-2014 Martin Sandve Alnæs and Aslak Wigdahl Bergersen
5 #
6 # This file is part of DOLFIN.
7 #
8 # DOLFIN is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # DOLFIN is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
20
21
22 import pytest
23 import os
24 import shutil
25 import tempfile
26 import gc
27 import platform
28 import decorator
29
30 from dolfin import parameters
31 from dolfin import *
32
33 # --- Test fixtures (use as is or as examples): ---
34
35 def fixture(func):
36 """Decorator for creating module scope fixture.
37 Also take care of garbage collection of temporaries.
38
39 NOTE: Probably does not work with yield fixtures or
40 maybe just ignores post-yield code
41
42 This is the preferred decorator for writing fixtures involving
43 objects which might have collective destructors. If in a need for
44 using ``pytest.fixture`` directly, do::
45
46 yield result_of_fixture
47
48 gc.collect()
49 MPI.barrier(MPI.comm_world)
50
51 """
52
53 def wrapper(func, *args, **kwargs):
54
55 # Run function
56 rv = func(*args, **kwargs)
57
58 # Collect garbage of temporaries of the function
59 # and return collectivelly
60 gc.collect()
61 MPI.barrier(MPI.comm_world)
62
63 return rv
64
65 # FIXME: Need also to find a way how to clean-up fixture
66 # return value; yield does not work here
67
68 # Decorate function with the wrapper
69 wrapped = decorator.decorator(wrapper, func)
70
71 # Mark as fixture and return
72 return pytest.fixture(scope='module')(wrapped)
73
74
75 def gc_barrier():
76 """Internal utility to easily switch on and off calls to gc.collect()
77 and MPI.barrier(world) in all fixtures here. Helps make the tests
78 deterministic when debugging.
79
80 """
81 gc.collect()
82 if MPI.size(MPI.comm_world) > 1:
83 MPI.barrier(MPI.comm_world)
84
85 def worker_id(request):
86 """Returns thread id when running with pytest-xdist in parallel."""
87 if hasattr(request.config, 'slaveinput'):
88 return request.config.slaveinput['slaveid']
89 else:
90 return 'master'
91
92 @pytest.fixture
93 def worker_id_fixture(request):
94 return worker_id(request)
95
96 @pytest.yield_fixture(scope="function")
97 def gc_barrier_fixture():
98 """Function decorator to call gc.collect() and
99 MPI.barrier(world) before and after a test. Helps
100 make the tests deterministic when debugging.
101
102 NOTE: This decorator is not needed now for writing tests, as there
103 is ``gc.collect()`` call in ``conftest.py`` on teardown of every
104 test.
105
106 """
107 gc_barrier()
108 yield
109 gc_barrier()
110
111 use_gc_barrier = pytest.mark.usefixtures("gc_barrier_fixture")
112
113
114 @pytest.fixture(params=[False, True])
115 def true_false_fixture(request):
116 "A fixture setting the values true and false."
117 gc_barrier()
118 return request.param
119
120
121 @pytest.fixture(scope="module")
122 def filedir(request):
123 "Return the directory of the test module."
124 gc_barrier()
125 d = os.path.dirname(os.path.abspath(request.module.__file__))
126 return d
127
128
129 @pytest.fixture(scope="module")
130 def rootdir(request):
131 """Return the root directory of the repository. Assumes run from
132 within repository filetree.
133
134 """
135 gc_barrier()
136 d = os.path.dirname(os.path.abspath(request.module.__file__))
137 t = ''
138 while t != "test":
139 d, t = os.path.split(d)
140 return d
141
142
143 @pytest.fixture(scope="module")
144 def datadir(request):
145 """Return the directory of the shared test data. Assumes run from
146 within repository filetree.
147
148 """
149 d = os.path.dirname(os.path.abspath(request.module.__file__))
150 t = os.path.join(d, "data")
151 while not os.path.isdir(t):
152 d, t = os.path.split(d)
153 t = os.path.join(d, "data")
154 return t
155
156
157 def _create_tempdir(request):
158 # Get directory name of test_foo.py file
159 testfile = request.module.__file__
160 testfiledir = os.path.dirname(os.path.abspath(testfile))
161
162 # Construct name test_foo_tempdir from name test_foo.py
163 testfilename = os.path.basename(testfile)
164 outputname = testfilename.replace(".py",
165 "_tempdir_{}".format(worker_id(request)))
166
167 # Get function name test_something from test_foo.py
168 function = request.function.__name__
169
170 # Join all of these to make a unique path for this test function
171 basepath = os.path.join(testfiledir, outputname)
172 path = os.path.join(basepath, function)
173
174 # Add a sequence number to avoid collisions when tests are otherwise parameterized
175 if MPI.rank(MPI.comm_world) == 0:
176 _create_tempdir._sequencenumber[path] += 1
177 sequencenumber = _create_tempdir._sequencenumber[path]
178 sequencenumber = MPI.sum(MPI.comm_world, sequencenumber)
179 else:
180 sequencenumber = MPI.sum(MPI.comm_world, 0)
181 path += "__" + str(sequencenumber)
182
183 # Delete and re-create directory on root node
184 if MPI.rank(MPI.comm_world) == 0:
185 # First time visiting this basepath, delete the old and create
186 # a new
187 if basepath not in _create_tempdir._basepaths:
188 _create_tempdir._basepaths.add(basepath)
189 if os.path.exists(basepath):
190 shutil.rmtree(basepath)
191 # Make sure we have the base path test_foo_tempdir for
192 # this test_foo.py file
193 if not os.path.exists(basepath):
194 os.mkdir(basepath)
195
196 # Delete path from old test run
197 if os.path.exists(path):
198 shutil.rmtree(path)
199 # Make sure we have the path for this test execution:
200 # e.g. test_foo_tempdir/test_something__3
201 if not os.path.exists(path):
202 os.mkdir(path)
203 MPI.barrier(MPI.comm_world)
204
205 return path
206 from collections import defaultdict
207 _create_tempdir._sequencenumber = defaultdict(int)
208 _create_tempdir._basepaths = set()
209
210
211 @pytest.fixture(scope="function")
212 def tempdir(request):
213 """Return a unique directory name for this test function instance.
214
215 Deletes and re-creates directory from previous test runs but lets
216 the directory stay after the test run for eventual inspection.
217
218 Returns the directory name, derived from the test file and
219 function plus a sequence number to work with parameterized tests.
220
221 Does NOT change the current directory.
222
223 MPI safe (assuming MPI.comm_world context).
224
225 """
226 gc_barrier()
227 return _create_tempdir(request)
228
229
230 @pytest.yield_fixture(scope="function")
231 def cd_tempdir(request):
232 """Return a unique directory name for this test function instance.
233
234 Deletes and re-creates directory from previous test runs but lets
235 the directory stay after the test run for eventual inspection.
236
237 Returns the directory name, derived from the test file and
238 function plus a sequence number to work with parameterized tests.
239
240 Changes the current directory to the tempdir and resets cwd
241 afterwards.
242
243 MPI safe (assuming MPI.comm_world context).
244
245 """
246 gc_barrier()
247 cwd = os.getcwd()
248 path = _create_tempdir(request)
249 os.chdir(path)
250 yield path
251 os.chdir(cwd)
252
253
254 @pytest.yield_fixture
255 def pushpop_parameters():
256 global parameters
257 gc_barrier()
258 prev = parameters.copy()
259 yield parameters.copy()
260 parameters.assign(prev)
261
262
263 # TODO: Rename set_parameters_fixture to e.g. use_parameter_values
264 def set_parameters_fixture(paramname, values, key=lambda x: x):
265 """Return a fixture that sets and resets a global parameter to each of
266 a list of values before and after each test run. Allows
267 paramname="foo.bar.var" meaning parameters["foo"]["bar"]["var"].
268
269 Usage:
270 repr = set_parameters_fixture("form_compiler.representation", ["quadrature", "uflacs"])
271 my_fixture1 = set_parameters_fixture("linear_algebra_backend", ["PETSc"])
272 my_fixture2 = set_parameters_fixture("linear_algebra_backend", [("Eigen", "")], key=lambda x: x[0])
273
274 def test_something0(repr):
275 assert repr in ("quadrature", "uflacs")
276 assert parameters["form_compiler"]["representation"] == repr
277
278 def test_something1(my_fixture1):
279 assert my_fixture1 in ("PETSc")
280 assert parameters["linear_algebra_backend"] == my_fixture1
281
282 def test_something2(my_fixture2):
283 assert my_fixture2[0] in ("Eigen")
284 assert parameters["linear_algebra_backend"] == my_fixture2[0]
285
286 Try it and see.
287
288 """
289 global parameters
290 def _pushpop(request):
291 gc_barrier()
292 if '.' in paramname:
293 names = paramname.split('.')
294 if len(names) == 2:
295 prev = parameters[names[0]][names[1]] # Remember original value
296 parameters[names[0]][names[1]] = key(request.param) # Set value
297 yield request.param # Let test run
298 parameters[names[0]][names[1]] = prev # Reset value
299 elif len(names) == 3:
300 prev = parameters[names[0]][names[1]][names[2]] # Remember original value
301 parameters[names[0]][names[1]][names[2]] = key(request.param) # Set value
302 yield request.param # Let test run
303 parameters[names[0]][names[1]][names[2]] = prev # Reset value
304 else:
305 prev = parameters[paramname] # Remember original value
306 parameters[paramname] = key(request.param) # Set value
307 yield request.param # Let test run
308 parameters[paramname] = prev # Reset value
309
310 return pytest.yield_fixture(scope="function", params=values)(_pushpop)