d415c28e5dcabea96bfe2aac0e4eb6fc1509cec8
[alexxy/gromacs.git] / src / testutils / testfilemanager.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2018,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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares gmx::test::TestFileManager.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_testutils
42  */
43 #ifndef GMX_TESTUTILS_TESTFILEMANAGER_H
44 #define GMX_TESTUTILS_TESTFILEMANAGER_H
45
46 #include <string>
47
48 #include "gromacs/utility/classhelpers.h"
49
50 namespace gmx
51 {
52 /*! \libinternal \brief
53  * Testing utilities namespace.
54  *
55  * This namespace contains utilities for writing unit tests, mostly from the
56  * \ref module_testutils module.
57  */
58 namespace test
59 {
60
61 /*! \libinternal \brief
62  * Helper for tests that need input and output files.
63  *
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).
71  *
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).
76  *
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
83  * is not changed.
84  *
85  * \inlibraryapi
86  * \ingroup module_testutils
87  */
88 class TestFileManager
89 {
90 public:
91     /*! \brief Constructor */
92     TestFileManager();
93     /*! \brief Frees internal storage and deletes any accessed
94      * file paths
95      *
96      * Any errors (e.g., missing files) encountered while deleting the
97      * files are ignored.
98      */
99     ~TestFileManager();
100
101     /*! \brief
102      * Creates a name for a temporary file within a single unit test.
103      *
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
107      *      \p suffix.
108      *
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
111      * same test.
112      */
113     std::string getTemporaryFilePath(const char* suffix);
114     //! \copydoc TestFileManager::getTemporaryFilePath(const char *)
115     std::string getTemporaryFilePath(const std::string& suffix);
116
117     /*! \brief Returns the path to the output temporary directory
118      * for tests which use this TestFileManager object.
119      *
120      * \returns Path to output temporary directory
121      */
122     const char* getOutputTempDirectory() const;
123
124     /*! \brief Sets the output temporary directory for tests which
125      * use this TestFileManager object.
126      *
127      * \param[in] path  Path at which test should write temporary files
128      *
129      * \p path must name an existing directory. An internal copy
130      * of path is made. The caller is responsible for holding a
131      * valid mutex on the object before calling this member
132      * function.
133      */
134     void setOutputTempDirectory(const std::string& path);
135
136     // static functions follow
137
138     /*! \brief
139      * Creates a file name root for use within a single unit test.
140      *
141      * This method should only be called from within a Google Test
142      * test. Uses the Google Test test fixture and test case name
143      * to construct a string that is unique over all
144      * tests. Intended to produce distinct names for files that
145      * may be stored in the same directory for multiple tests.
146      */
147     static std::string getTestSpecificFileNameRoot();
148
149     /*! \brief
150      * Creates a file name for use within a single unit test.
151      *
152      * \param[in] suffix  Suffix to add to the file name (should contain an
153      *      extension if one is desired).
154      * \returns   File name that includes the test name and
155      *      \p suffix.
156      *
157      * This method should only be called from within a Google Test test.
158      * Two calls with the same \p suffix return the same string within the
159      * same test.
160      * Intended to produce distinct names for files that may be stored in
161      * the same directory for multiple tests.
162      */
163     static std::string getTestSpecificFileName(const char* suffix);
164
165     /*! \brief
166      * Returns the path to a test input file.
167      *
168      * \param[in] filename  Relative path/filename to a test input file.
169      * \returns Path to \p filename under the test input data directory.
170      */
171     static std::string getInputFilePath(const char* filename);
172     //! \copydoc TestFileManager::getInputFilePath(const char *)
173     static std::string getInputFilePath(const std::string& filename);
174
175     /*! \brief
176      * Returns the path to the simulation input database directory.
177      *
178      * \returns Path to simulation input database directory.
179      */
180     static const char* getTestSimulationDatabaseDirectory();
181
182     /*! \brief
183      * Returns the path to the test input directory.
184      *
185      * \returns Path to input data directory for the test executable.
186      */
187     static const char* getInputDataDirectory();
188
189     /*! \brief
190      * Sets the test input directory.
191      *
192      * \param[in] path  Path from which test input data is looked up from.
193      *
194      * \p path must name an existing directory.
195      *
196      * This function is automatically called by unittest_main.cpp through
197      * initTestUtils().
198      */
199     static void setInputDataDirectory(const std::string& path);
200
201     /*! \brief
202      * Sets the input directory for simulation input files.
203      *
204      * \param[in] path Path to look up the directory for simulation input files.
205      *
206      * \p path must name an exisitng directory.
207      *
208      * This function is automatically called by unittest_main.cpp through
209      * initTestUtils().
210      */
211     static void setTestSimulationDatabaseDirectory(const std::string& path);
212
213     /*! \brief Returns the path to the global test output
214      * temporary directory for future TestFileManager objects.
215      *
216      * \returns Path to default output temporary directory for the test executable.
217      */
218     static const char* getGlobalOutputTempDirectory();
219
220     /*! \brief Sets the default global test output temporary
221      * directory for future TestFileManager objects.
222      *
223      * \param[in] path  Path at which tests should write temporary files
224      *
225      * \p path must name an existing directory.
226      *
227      * This function is automatically called by unittest_main.cpp
228      * through initTestUtils(). Test fixtures should call
229      * setOutputTempDirectory(), rather than change the global
230      * state.
231      */
232     static void setGlobalOutputTempDirectory(const char* path);
233
234 private:
235     class Impl;
236
237     PrivateImplPointer<Impl> impl_;
238 };
239
240 } // namespace test
241 } // namespace gmx
242
243 #endif