2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \libinternal \file
38 * Declares gmx::test::TestFileManager.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_testutils
44 #ifndef GMX_TESTUTILS_TESTFILEMANAGER_H
45 #define GMX_TESTUTILS_TESTFILEMANAGER_H
49 #include "gromacs/utility/classhelpers.h"
53 /*! \libinternal \brief
54 * Testing utilities namespace.
56 * This namespace contains utilities for writing unit tests, mostly from the
57 * \ref module_testutils module.
62 /*! \libinternal \brief
63 * Helper for tests that need input and output files.
65 * To be used as a member in a test fixture class, this class provides
66 * getTemporaryFilePath() method that returns a path for creating file names
67 * for temporary files. The returned path contains the name of the running
68 * test, making it unique across tests. Additionally, this class takes care of
69 * removing any temporary files (i.e., all paths returned by
70 * getTemporaryFilePath()) at test teardown (i.e., when the
71 * TestFileManager is destructed).
73 * In addition, class-level static accessors provide means to
74 * access data files that are located in the test source directory.
75 * This is used to provide input files for the tests, and also to
76 * store test reference data persistently (see TestReferenceData).
78 * Note that setInputDataDirectory(), setTestSimulationDataBaseDirectory() and
79 * setGlobalOutputTempDirectory() must be called in setup code, before
80 * creating any objects of this class that are used for accessing the
81 * paths for these respective directories. Code in tests should avoid
82 * calling setGlobalOutputTempDirectory(), and instead instantiate an
83 * object and use setOutputTempDirectory(), so that the global state
87 * \ingroup module_testutils
92 /*! \brief Constructor */
94 /*! \brief Frees internal storage and deletes any accessed
97 * Any errors (e.g., missing files) encountered while deleting the
103 * Creates a name for a temporary file within a single unit test.
105 * \param[in] suffix Suffix to add to the file name (should contain an
106 * extension if one is desired).
107 * \returns Temporary file name that includes the test name and
110 * This method should only be called from within a Google Test test.
111 * Two calls with the same \p suffix return the same string within the
114 std::string getTemporaryFilePath(const char* suffix);
115 //! \copydoc TestFileManager::getTemporaryFilePath(const char *)
116 std::string getTemporaryFilePath(const std::string& suffix);
118 /*! \brief Returns the path to the output temporary directory
119 * for tests which use this TestFileManager object.
121 * \returns Path to output temporary directory
123 const char* getOutputTempDirectory() const;
125 /*! \brief Sets the output temporary directory for tests which
126 * use this TestFileManager object.
128 * \param[in] path Path at which test should write temporary files
130 * \p path must name an existing directory. An internal copy
131 * of path is made. The caller is responsible for holding a
132 * valid mutex on the object before calling this member
135 void setOutputTempDirectory(const std::string& path);
137 // static functions follow
140 * Creates a file name root for use within a single unit test.
142 * This method should only be called from within a Google Test
143 * test. Uses the Google Test test fixture and test case name
144 * to construct a string that is unique over all
145 * tests. Intended to produce distinct names for files that
146 * may be stored in the same directory for multiple tests.
148 static std::string getTestSpecificFileNameRoot();
151 * Creates a file name for use within a single unit test.
153 * \param[in] suffix Suffix to add to the file name (should contain an
154 * extension if one is desired).
155 * \returns File name that includes the test name and
158 * This method should only be called from within a Google Test test.
159 * Two calls with the same \p suffix return the same string within the
161 * Intended to produce distinct names for files that may be stored in
162 * the same directory for multiple tests.
164 static std::string getTestSpecificFileName(const char* suffix);
167 * Returns the path to a test input file.
169 * \param[in] filename Relative path/filename to a test input file.
170 * \returns Path to \p filename under the test input data directory.
172 static std::string getInputFilePath(const char* filename);
173 //! \copydoc TestFileManager::getInputFilePath(const char *)
174 static std::string getInputFilePath(const std::string& filename);
177 * Returns the path to the simulation input database directory.
179 * \returns Path to simulation input database directory.
181 static const char* getTestSimulationDatabaseDirectory();
184 * Returns the path to the test input directory.
186 * \returns Path to input data directory for the test executable.
188 static const char* getInputDataDirectory();
191 * Sets the test input directory.
193 * \param[in] path Path from which test input data is looked up from.
195 * \p path must name an existing directory.
197 * This function is automatically called by unittest_main.cpp through
200 static void setInputDataDirectory(const std::string& path);
203 * Sets the input directory for simulation input files.
205 * \param[in] path Path to look up the directory for simulation input files.
207 * \p path must name an exisitng directory.
209 * This function is automatically called by unittest_main.cpp through
212 static void setTestSimulationDatabaseDirectory(const std::string& path);
214 /*! \brief Returns the path to the global test output
215 * temporary directory for future TestFileManager objects.
217 * \returns Path to default output temporary directory for the test executable.
219 static const char* getGlobalOutputTempDirectory();
221 /*! \brief Sets the default global test output temporary
222 * directory for future TestFileManager objects.
224 * \param[in] path Path at which tests should write temporary files
226 * \p path must name an existing directory.
228 * This function is automatically called by unittest_main.cpp
229 * through initTestUtils(). Test fixtures should call
230 * setOutputTempDirectory(), rather than change the global
233 static void setGlobalOutputTempDirectory(const char* path);
238 PrivateImplPointer<Impl> impl_;