Custom program context for unit tests
[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, 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 /*! \libinternal \defgroup module_testutils Unit Testing Utilities (testutils)
44  * \brief
45  * Common helper classes and functions for writing unit tests.
46  *
47  * To build unit tests using these utilities, libxml2 is required.
48  *
49  * \ingroup group_utilitymodules
50  */
51 #ifndef GMX_TESTUTILS_TESTFILEMANAGER_H
52 #define GMX_TESTUTILS_TESTFILEMANAGER_H
53
54 #include <string>
55
56 #include "gromacs/utility/classhelpers.h"
57
58 namespace gmx
59 {
60 /*! \libinternal \brief
61  * Testing utilities namespace.
62  *
63  * This namespace contains utilities for writing unit tests, mostly from the
64  * \ref module_testutils module.
65  */
66 namespace test
67 {
68
69 /*! \libinternal \brief
70  * Helper for tests that need input and output files.
71  *
72  * To be used as a member in a test fixture class, this class provides
73  * getTemporaryFilePath() method that returns a path for creating file names
74  * for temporary files.  The returned path contains the name of the running
75  * test, making it unique across tests.  Additionally, this class takes care of
76  * removing any temporary files (i.e., all paths returned by
77  * getTemporaryFilePath()) at test teardown (i.e., when the
78  * TestFileManager is destructed).
79  *
80  * In addition, class-level static accessors provide means to
81  * access data files that are located in the test source directory.
82  * This is used to provide input files for the tests, and also to
83  * store test reference data persistently (see TestReferenceData).
84  *
85  * Note that setInputDataDirectory() and
86  * setGlobalOutputTempDirectory() must be called in setup code, before
87  * creating any objects of this class that are used for accessing the
88  * paths for these respective directories. Code in tests should avoid
89  * calling setGlobalOutputTempDirectory(), and instead instantiate an
90  * object and use setOutputTempDirectory(), so that the global state
91  * is not changed.
92  *
93  * \inlibraryapi
94  * \ingroup module_testutils
95  */
96 class TestFileManager
97 {
98     public:
99         /*! \brief Constructor */
100         TestFileManager();
101         /*! \brief Frees internal storage and deletes any accessed
102          * file paths
103          *
104          * Any errors (e.g., missing files) encountered while deleting the
105          * files are ignored.
106          */
107         ~TestFileManager();
108
109         /*! \brief
110          * Creates a name for a temporary file within a single unit test.
111          *
112          * \param[in] suffix  Suffix to add to the file name (should contain an
113          *      extension if one is desired).
114          * \returns   Temporary file name that includes the test name and
115          *      \p suffix.
116          *
117          * This method should only be called from within a Google Test test.
118          * Two calls with the same \p suffix return the same string within the
119          * same test.
120          */
121         std::string getTemporaryFilePath(const char *suffix);
122         //! \copydoc TestFileManager::getTemporaryFilePath(const char *)
123         std::string getTemporaryFilePath(const std::string &suffix);
124
125         /*! \brief Returns the path to the output temporary directory
126          * for tests which use this TestFileManager object.
127          *
128          * \returns Path to output temporary directory
129          */
130         const char *getOutputTempDirectory() const;
131
132         /*! \brief Sets the output temporary directory for tests which
133          * use this TestFileManager object.
134          *
135          * \param[in] path  Path at which test should write temporary files
136          *
137          * \p path must name an existing directory. An internal copy
138          * of path is made. The caller is responsible for holding a
139          * valid mutex on the object before calling this member
140          * function.
141          */
142         void setOutputTempDirectory(const std::string &path);
143
144         // static functions follow
145
146         /*! \brief
147          * Creates a file name root for use within a single unit test.
148          *
149          * This method should only be called from within a Google Test
150          * test. Uses the Google Test test fixture and test case name
151          * to construct a string that is unique over all
152          * tests. Intended to produce distinct names for files that
153          * may be stored in the same directory for multiple tests.
154          */
155         static std::string getTestSpecificFileNameRoot();
156
157         /*! \brief
158          * Creates a file name for use within a single unit test.
159          *
160          * \param[in] suffix  Suffix to add to the file name (should contain an
161          *      extension if one is desired).
162          * \returns   File name that includes the test name and
163          *      \p suffix.
164          *
165          * This method should only be called from within a Google Test test.
166          * Two calls with the same \p suffix return the same string within the
167          * same test.
168          * Intended to produce distinct names for files that may be stored in
169          * the same directory for multiple tests.
170          */
171         static std::string getTestSpecificFileName(const char *suffix);
172
173         /*! \brief
174          * Returns the path to a test input file.
175          *
176          * \param[in] filename  Relative path/filename to a test input file.
177          * \returns Path to \p filename under the test input data directory.
178          */
179         static std::string getInputFilePath(const char *filename);
180
181         /*! \brief
182          * Returns the path to the test input directory.
183          *
184          * \returns Path to input data directory for the test executable.
185          */
186         static const char *getInputDataDirectory();
187
188         /*! \brief
189          * Sets the test input directory.
190          *
191          * \param[in] path  Path from which test input data is looked up from.
192          *
193          * \p path must name an existing directory.
194          *
195          * This function is automatically called by unittest_main.cpp through
196          * initTestUtils().
197          */
198         static void setInputDataDirectory(const std::string &path);
199
200         /*! \brief Returns the path to the global test output
201          * temporary directory for future TestFileManager objects.
202          *
203          * \returns Path to default output temporary directory for the test executable.
204          */
205         static const char *getGlobalOutputTempDirectory();
206
207         /*! \brief Sets the default global test output temporary
208          * directory for future TestFileManager objects.
209          *
210          * \param[in] path  Path at which tests should write temporary files
211          *
212          * \p path must name an existing directory.
213          *
214          * This function is automatically called by unittest_main.cpp
215          * through initTestUtils(). Test fixtures should call
216          * setOutputTempDirectory(), rather than change the global
217          * state.
218          */
219         static void setGlobalOutputTempDirectory(const char *path);
220
221     private:
222         class Impl;
223
224         PrivateImplPointer<Impl> impl_;
225 };
226
227 } // namespace test
228 } // namespace gmx
229
230 #endif