2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2019, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
35 """Configuration and fixtures for pytest."""
43 from contextlib import contextmanager
48 def pytest_addoption(parser):
49 """Add a command-line user option for the pytest invocation."""
54 choices=['always', 'never', 'success'],
55 help='Remove temporary directories "always", "never", or on "success".'
59 @pytest.fixture(scope='session')
60 def remove_tempdir(request):
61 """pytest fixture to get access to the --rm CLI option."""
62 return request.config.getoption('--rm')
66 def scoped_chdir(dir):
71 # If the `with` block using scoped_chdir produces an exception, it will
72 # be raised at this point in this function. We want the exception to
73 # propagate out of the `with` block, but first we want to restore the
74 # original working directory, so we skip `except` but provide a `finally`.
80 def _cleandir(remove_tempdir):
81 """Context manager for a clean temporary working directory.
84 remove_tempdir (str): whether to remove temporary directory "always",
85 "never", or on "success"
87 The context manager will issue a warning for each temporary directory that
91 newpath = tempfile.mkdtemp()
94 shutil.rmtree(newpath)
97 warnings.warn('Temporary directory not removed: {}'.format(newpath))
99 if remove_tempdir == 'always':
104 with scoped_chdir(newpath):
106 # If we get to this line, the `with` block using _cleandir did not throw.
107 # Clean up the temporary directory unless the user specified `--rm never`.
108 # I.e. If the user specified `--rm success`, then we need to toggle from `warn` to `remove`.
109 if remove_tempdir != 'never':
116 def cleandir(remove_tempdir):
117 """Provide a clean temporary working directory for a test.
124 @pytest.mark.usefixtures("cleandir")
125 def test_cwd_starts_empty():
126 assert os.listdir(os.getcwd()) == []
127 with open("myfile", "w") as f:
130 def test_cwd_also_starts_empty(cleandir):
131 assert os.listdir(os.getcwd()) == []
132 assert os.path.abspath(os.getcwd()) == os.path.abspath(cleandir)
133 with open("myfile", "w") as f:
136 @pytest.mark.usefixtures("cleandir")
137 class TestDirectoryInit(object):
138 def test_cwd_starts_empty(self):
139 assert os.listdir(os.getcwd()) == []
140 with open("myfile", "w") as f:
143 def test_cwd_also_starts_empty(self):
144 assert os.listdir(os.getcwd()) == []
145 with open("myfile", "w") as f:
148 Ref: https://docs.pytest.org/en/latest/fixture.html#using-fixtures-from-classes-modules-or-projects
150 with _cleandir(remove_tempdir) as newdir:
154 @pytest.fixture(scope='session')
156 # TODO: (#2896) Find a more canonical way to identify the GROMACS commandline wrapper binary.
157 # We should be able to get the GMXRC contents and related hints from a gmxapi
158 # package resource or from module attributes of a ``gromacs`` stub package.
159 allowed_command_names = ['gmx', 'gmx_mpi']
161 for command_name in allowed_command_names:
162 if command is not None:
164 command = shutil.which(command_name)
166 gmxbindir = os.getenv('GMXBIN')
167 if gmxbindir is None:
168 gromacsdir = os.getenv('GROMACS_DIR')
169 if gromacsdir is not None and gromacsdir != '':
170 gmxbindir = os.path.join(gromacsdir, 'bin')
171 if gmxbindir is None:
172 gmxapidir = os.getenv('gmxapi_DIR')
173 if gmxapidir is not None and gmxapidir != '':
174 gmxbindir = os.path.join(gmxapidir, 'bin')
175 if gmxbindir is not None:
176 gmxbindir = os.path.abspath(gmxbindir)
177 command = shutil.which(command_name, path=gmxbindir)
179 message = "Tests need 'gmx' command line tool, but could not find it on the path."
180 raise RuntimeError(message)
182 assert os.access(command, os.X_OK)
183 except Exception as E:
184 raise RuntimeError('"{}" is not an executable gmx wrapper program'.format(command)) from E
188 @pytest.fixture(scope='class')
189 def spc_water_box(gmxcli, remove_tempdir):
190 """Provide a TPR input file for a simple simulation.
192 Prepare the MD input in a freshly created working directory.
196 # TODO: (#2896) Fetch MD input from package / library data.
198 # import pkg_resources
199 # # Note: importing pkg_resources means setuptools is required for running this test.
200 # # Get or build TPR file from data bundled via setup(package_data=...)
201 # # Ref https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files
202 # from gmx.data import tprfilename
204 with _cleandir(remove_tempdir) as tempdir:
206 testdir = os.path.dirname(__file__)
207 with open(os.path.join(testdir, 'testdata.json'), 'r') as fh:
208 testdata = json.load(fh)
210 # TODO: (#2756) Don't rely on so many automagical behaviors (as described in comments below)
212 structurefile = os.path.join(tempdir, 'structure.gro')
213 # We let `gmx solvate` use the default solvent. Otherwise, we would do
214 # gro_input = testdata['solvent_structure']
215 # with open(structurefile, 'w') as fh:
216 # fh.write('\n'.join(gro_input))
219 topfile = os.path.join(tempdir, 'topology.top')
220 top_input = testdata['solvent_topology']
221 # `gmx solvate` will append a line to the provided file with the molecule count,
222 # so we strip the last line from the input topology.
223 with open(topfile, 'w') as fh:
224 fh.write('\n'.join(top_input[:-1]))
227 assert os.path.exists(topfile)
228 solvate = gmx.commandline_operation(gmxcli,
229 arguments=['solvate', '-box', '5', '5', '5'],
230 # We use the default solvent instead of specifying one.
231 # input_files={'-cs': structurefile},
232 output_files={'-p': topfile,
236 assert os.path.exists(topfile)
238 if solvate.output.returncode.result() != 0:
239 logging.debug(solvate.output.erroroutput.result())
240 raise RuntimeError('solvate failed in spc_water_box testing fixture.')
242 # Choose an exactly representable dt of 2^-9 ps (approximately 0.002)
244 mdp_input = [('integrator', 'md'),
246 ('cutoff-scheme', 'Verlet'),
251 ('tcoupl', 'v-rescale'),
252 ('tc-grps', 'System'),
255 mdp_input = '\n'.join([' = '.join([str(item) for item in kvpair]) for kvpair in mdp_input])
256 mdpfile = os.path.join(tempdir, 'md.mdp')
257 with open(mdpfile, 'w') as fh:
260 tprfile = os.path.join(tempdir, 'topol.tpr')
261 # We don't use mdout_mdp, but if we don't specify it to grompp,
262 # it will be created in the current working directory.
263 mdout_mdp = os.path.join(tempdir, 'mdout.mdp')
265 grompp = gmx.commandline_operation(gmxcli, 'grompp',
268 '-p': solvate.output.file['-p'],
269 '-c': solvate.output.file['-o'],
272 output_files={'-o': tprfile})
273 tprfilename = grompp.output.file['-o'].result()
274 if grompp.output.returncode.result() != 0:
275 logging.debug(grompp.output.erroroutput.result())
276 raise RuntimeError('grompp failed in spc_water_box testing fixture.')
278 # TODO: more inspection of grompp errors...
279 assert os.path.exists(tprfilename)