Simplified uniform GPU selection in CMake
[gromacs.git] / src / testutils / testfilemanager.cpp
blobcb0a1ceeb5b1203298b6762c562ff8c7f3187397
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2017 by the GROMACS development team.
5 * Copyright (c) 2018,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 /*! \internal \file
37 * \brief
38 * Implements gmx::test::TestFileManager.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_testutils
43 #include "gmxpre.h"
45 #include "testfilemanager.h"
47 #include <cstdio>
49 #include <algorithm>
50 #include <set>
51 #include <string>
53 #include <gtest/gtest.h>
55 #include "gromacs/options/basicoptions.h"
56 #include "gromacs/options/ioptionscontainer.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/path.h"
60 #include "testutils/testoptions.h"
62 namespace gmx
64 namespace test
67 /********************************************************************
68 * TestFileManager::Impl
71 /*! \internal \brief
72 * Private implementation class for TestFileManager.
74 * \ingroup module_testutils
76 class TestFileManager::Impl
78 public:
79 //! Global test input data path set with setDataInputDirectory().
80 static std::string s_inputDirectory;
82 //! Global path to simulation input database set with setTestSimulationDataBaseDirectory().
83 static std::string s_simulationDatabaseDirectory;
85 //! Global temporary output directory for tests, set with setGlobalOutputTempDirectory().
86 static const char* s_globalOutputTempDirectory;
88 //! Container type for names of temporary files.
89 typedef std::set<std::string> FileNameList;
91 /*! \brief Constructor
93 * \param path Value for the outputTempDirectory, typically
94 * set by default from s_globalOutputTempDirectory */
95 explicit Impl(const char* path) : outputTempDirectory_(path)
97 GMX_RELEASE_ASSERT(Directory::exists(outputTempDirectory_),
98 "Directory for tests' temporary files does not exist");
101 /*! \brief
102 * Try to remove all temporary files.
104 * Does not throw; errors (e.g., missing files) are silently ignored.
106 void removeFiles();
108 //! List of unique paths returned by getTemporaryFilePath().
109 FileNameList files_;
111 /*! \brief Temporary output directory local to the current
112 * test, set by a test with setOutputTempDirectory() if the
113 * global default is inappropriate. */
114 std::string outputTempDirectory_;
117 std::string TestFileManager::Impl::s_inputDirectory;
118 std::string TestFileManager::Impl::s_simulationDatabaseDirectory;
119 const char* TestFileManager::Impl::s_globalOutputTempDirectory = nullptr;
120 /** Controls whether TestFileManager should delete temporary files
121 after the test finishes. */
122 static bool g_bDeleteFilesAfterTest = true;
124 //! \cond
125 GMX_TEST_OPTIONS(TestFileManagerOptions, options)
127 options->addOption(
128 BooleanOption("delete-temporary-files")
129 .store(&g_bDeleteFilesAfterTest)
130 .description(
131 "At the end of each test case, delete temporary and output files"));
133 //! \endcond
135 void TestFileManager::Impl::removeFiles()
137 FileNameList::const_iterator i;
138 for (i = files_.begin(); i != files_.end(); ++i)
140 std::remove(i->c_str());
142 files_.clear();
145 /********************************************************************
146 * TestFileManager
149 TestFileManager::TestFileManager() : impl_(new Impl(Impl::s_globalOutputTempDirectory)) {}
151 TestFileManager::~TestFileManager()
153 if (g_bDeleteFilesAfterTest)
155 impl_->removeFiles();
159 std::string TestFileManager::getTemporaryFilePath(const char* suffix)
161 /* Configure a temporary directory from CMake, so that temporary
162 * output from a test goes to a location relevant to that
163 * test. Currently, files whose names are returned by this method
164 * get cleaned up (by default) at the end of all tests.
166 std::string filename = Path::join(getOutputTempDirectory(), getTestSpecificFileName(suffix));
167 impl_->files_.insert(filename);
168 return filename;
171 std::string TestFileManager::getTemporaryFilePath(const std::string& suffix)
173 return getTemporaryFilePath(suffix.c_str());
176 std::string TestFileManager::getTestSpecificFileNameRoot()
178 const ::testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
179 std::string filenameRoot;
180 if (test_info)
182 filenameRoot = std::string(test_info->test_case_name()) + "_" + test_info->name();
184 else
186 const ::testing::TestCase* test_case_info =
187 ::testing::UnitTest::GetInstance()->current_test_case();
188 filenameRoot = std::string(test_case_info->name());
190 std::replace(filenameRoot.begin(), filenameRoot.end(), '/', '_');
191 return filenameRoot;
194 std::string TestFileManager::getTestSpecificFileName(const char* suffix)
196 std::string filename = getTestSpecificFileNameRoot();
197 if (suffix[0] != '.')
199 filename.append("_");
201 filename.append(suffix);
202 return filename;
205 std::string TestFileManager::getInputFilePath(const char* filename)
207 // Check if file is present in local directory.
208 if (File::exists(Path::join(getInputDataDirectory(), filename), File::returnFalseOnError))
210 return Path::join(getInputDataDirectory(), filename);
212 else if (File::exists(Path::join(getTestSimulationDatabaseDirectory(), filename), File::returnFalseOnError))
214 // Assume file is in global directory for simulation input files.
215 return Path::join(getTestSimulationDatabaseDirectory(), filename);
217 else
219 // Assume file is present locally without full name (e.g. extension).
220 return Path::join(getInputDataDirectory(), filename);
224 std::string TestFileManager::getInputFilePath(const std::string& filename)
226 return getInputFilePath(filename.c_str());
229 const char* TestFileManager::getInputDataDirectory()
231 GMX_RELEASE_ASSERT(!Impl::s_inputDirectory.empty(), "Path for test input files is not set");
232 return Impl::s_inputDirectory.c_str();
235 const char* TestFileManager::getGlobalOutputTempDirectory()
237 GMX_RELEASE_ASSERT(Impl::s_globalOutputTempDirectory != nullptr,
238 "Global path for temporary output files from tests is not set");
239 return Impl::s_globalOutputTempDirectory;
242 const char* TestFileManager::getOutputTempDirectory() const
244 return impl_->outputTempDirectory_.c_str();
247 const char* TestFileManager::getTestSimulationDatabaseDirectory()
249 GMX_RELEASE_ASSERT(!Impl::s_simulationDatabaseDirectory.empty(),
250 "Path for simulation input database directory is not set");
251 return Impl::s_simulationDatabaseDirectory.c_str();
254 void TestFileManager::setInputDataDirectory(const std::string& path)
256 // There is no need to protect this by a mutex, as this is called in early
257 // initialization of the tests.
258 GMX_RELEASE_ASSERT(Directory::exists(path), "Test data directory does not exist");
259 Impl::s_inputDirectory = path;
262 void TestFileManager::setTestSimulationDatabaseDirectory(const std::string& path)
264 // There is no need to protect this by a mutex, as this is called in early
265 // initialization of the tests.
266 GMX_RELEASE_ASSERT(Directory::exists(path), "Simulation database directory does not exist");
267 Impl::s_simulationDatabaseDirectory = path;
270 void TestFileManager::setGlobalOutputTempDirectory(const char* path)
272 // There is no need to protect this by a mutex, as this is called in early
273 // initialization of the tests.
274 GMX_RELEASE_ASSERT(Directory::exists(path),
275 "Directory for tests' temporary files does not exist");
276 Impl::s_globalOutputTempDirectory = path;
279 void TestFileManager::setOutputTempDirectory(const std::string& path)
281 // There could be a need to protect this with a mutex, since it is
282 // intended to be used in test fixtures, not just during setup.
283 GMX_RELEASE_ASSERT(Directory::exists(path),
284 "Directory for tests' temporary files does not exist");
285 impl_->outputTempDirectory_ = path;
288 } // namespace test
289 } // namespace gmx