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,2021, 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
52 /*! \libinternal \brief
53 * Testing utilities namespace.
55 * This namespace contains utilities for writing unit tests, mostly from the
56 * \ref module_testutils module.
61 /*! \libinternal \brief
62 * Helper for tests that need input and output files.
64 * To be used as a member in a test fixture class, this class provides
65 * getTemporaryFilePath() method that returns a path for creating file names
66 * for temporary files. The returned path contains the name of the running
67 * test, making it unique across tests. Additionally, this class takes care of
68 * removing any temporary files (i.e., all paths returned by
69 * getTemporaryFilePath()) at test teardown (i.e., when the
70 * TestFileManager is destructed).
72 * In addition, class-level static accessors provide means to
73 * access data files that are located in the test source directory.
74 * This is used to provide input files for the tests, and also to
75 * store test reference data persistently (see TestReferenceData).
77 * Note that setInputDataDirectory(), setTestSimulationDataBaseDirectory() and
78 * setGlobalOutputTempDirectory() must be called in setup code, before
79 * creating any objects of this class that are used for accessing the
80 * paths for these respective directories. Code in tests should avoid
81 * calling setGlobalOutputTempDirectory(), and instead instantiate an
82 * object and use setOutputTempDirectory(), so that the global state
86 * \ingroup module_testutils
91 /*! \brief Constructor */
93 /*! \brief Frees internal storage and deletes any accessed
96 * Any errors (e.g., missing files) encountered while deleting the
102 * Creates a name for a temporary file within a single unit test.
104 * \param[in] suffix Suffix to add to the file name (should contain an
105 * extension if one is desired).
106 * \returns Temporary file name that includes the test name and
109 * This method should only be called from within a Google Test test.
110 * Two calls with the same \p suffix return the same string within the
113 std::string getTemporaryFilePath(const char* suffix);
114 //! \copydoc TestFileManager::getTemporaryFilePath(const char *)
115 std::string getTemporaryFilePath(const std::string& suffix);
118 * Manage cleaning up this output file whose name was generated
120 * \param[in] filename Filename relative to the working directory
121 * (which is generally not the output directory)
123 * This method should only be called from within a Google Test
124 * test. It lets the TestFileManager know to attempt to clean up
125 * the named file. That file should normally be one whose name is
126 * generated by the tool under test. For an output file whose name
127 * is specified by e.g. an input command-line option, pass the
128 * return value of getTemporaryFilePath() to that option.
130 void manageGeneratedOutputFile(const char* filename);
131 //! \copydoc TestFileManager::manageGeneratedOutputFile(const char *)
132 void manageGeneratedOutputFile(std::string&& filename);
134 /*! \brief Returns the path to the output temporary directory
135 * for tests which use this TestFileManager object.
137 * \returns Path to output temporary directory
139 const char* getOutputTempDirectory() const;
141 /*! \brief Sets the output temporary directory for tests which
142 * use this TestFileManager object.
144 * \param[in] path Path at which test should write temporary files
146 * \p path must name an existing directory. An internal copy
147 * of path is made. The caller is responsible for holding a
148 * valid mutex on the object before calling this member
151 void setOutputTempDirectory(const std::string& path);
153 // static functions follow
156 * Creates a file name root for use within a single unit test.
158 * This method should only be called from within a Google Test
159 * test. Uses the Google Test test fixture and test case name
160 * to construct a string that is unique over all
161 * tests. Intended to produce distinct names for files that
162 * may be stored in the same directory for multiple tests.
164 static std::string getTestSpecificFileNameRoot();
167 * Creates a file name for use within a single unit test.
169 * \param[in] suffix Suffix to add to the file name (should contain an
170 * extension if one is desired).
171 * \returns File name that includes the test name and
174 * This method should only be called from within a Google Test test.
175 * Two calls with the same \p suffix return the same string within the
177 * Intended to produce distinct names for files that may be stored in
178 * the same directory for multiple tests.
180 static std::string getTestSpecificFileName(const char* suffix);
183 * Returns the path to a test input file.
185 * \param[in] filename Relative path/filename to a test input file.
186 * \returns Path to \p filename under the test input data directory.
188 static std::string getInputFilePath(const char* filename);
189 //! \copydoc TestFileManager::getInputFilePath(const char *)
190 static std::string getInputFilePath(const std::string& filename);
193 * Returns the path to the simulation input database directory.
195 * \returns Path to simulation input database directory.
197 static const char* getTestSimulationDatabaseDirectory();
200 * Returns the path to the test input directory.
202 * \returns Path to input data directory for the test executable.
204 static const char* getInputDataDirectory();
207 * Sets the test input directory.
209 * \param[in] path Path from which test input data is looked up from.
211 * \p path must name an existing directory.
213 * This function is automatically called by unittest_main.cpp through
216 static void setInputDataDirectory(const std::string& path);
219 * Sets the input directory for simulation input files.
221 * \param[in] path Path to look up the directory for simulation input files.
223 * \p path must name an exisitng directory.
225 * This function is automatically called by unittest_main.cpp through
228 static void setTestSimulationDatabaseDirectory(const std::string& path);
230 /*! \brief Returns the path to the global test output
231 * temporary directory for future TestFileManager objects.
233 * \returns Path to default output temporary directory for the test executable.
235 static const char* getGlobalOutputTempDirectory();
237 /*! \brief Sets the default global test output temporary
238 * directory for future TestFileManager objects.
240 * \param[in] path Path at which tests should write temporary files
242 * \p path must name an existing directory.
244 * This function is automatically called by unittest_main.cpp
245 * through initTestUtils(). Test fixtures should call
246 * setOutputTempDirectory(), rather than change the global
249 static void setGlobalOutputTempDirectory(const char* path);
254 std::unique_ptr<Impl> impl_;