Add helper to reuse generated TPR files in testing
authorPaul Bauer <paul.bauer.q@gmail.com>
Tue, 17 Sep 2019 15:33:13 +0000 (17:33 +0200)
committerMagnus Lundborg <magnus.lundborg@scilifelab.se>
Fri, 11 Oct 2019 11:23:30 +0000 (13:23 +0200)
Used static class members in GoogleTest and provided option in
testfilemanager to allow file path specification before test case is
started.

This should speed up some of the test cases that have been slow due to
repeated calls to grompp.

Change-Id: I50e29d04550d78f2324e3665e903d45515464298

src/gromacs/tools/tests/dump.cpp
src/gromacs/tools/tests/report_methods.cpp
src/testutils/CMakeLists.txt
src/testutils/testfilemanager.cpp
src/testutils/tprfilegenerator.cpp [new file with mode: 0644]
src/testutils/tprfilegenerator.h [new file with mode: 0644]

index 147115f5aa96b6c0f97e91d2f5ac04d157bac202..5110162fa8c1011cb8985b6cf333e325e792d68a 100644 (file)
@@ -47,6 +47,7 @@
 
 #include "testutils/cmdlinetest.h"
 #include "testutils/testfilemanager.h"
+#include "testutils/tprfilegenerator.h"
 
 namespace gmx
 {
@@ -54,65 +55,53 @@ namespace gmx
 namespace test
 {
 
-namespace
+class DumpTest : public ::testing::Test
 {
+    public:
+        //! Run test case.
+        void runTest(CommandLine *cmdline);
+    protected:
+        // TODO this is changed in newer googletest versions
+        //! Prepare shared resources.
+        static void SetUpTestCase()
+        {
+            s_tprFileHandle = new TprAndFileManager("lysozyme");
+        }
+        //! Clean up shared resources.
+        static void TearDownTestCase()
+        {
+            delete s_tprFileHandle;
+            s_tprFileHandle = nullptr;
+        }
+        //! Storage for opened file handles.
+        static TprAndFileManager *s_tprFileHandle;
+};
 
-/*! \brief
- * Generates a tpr for the test.
- *
- * Generates the tpr from a sample pdb file using grompp,and returns the path
- * to the file as std::string for reading it in later.
- *
- * \param[in] fileManager Filemanager to keep track of the input file.
- * \param[in] filename Basename of the input and output files.
- */
-std::string generateTprInput(TestFileManager *fileManager, const std::string &filename)
-{
-// generate temporary tpr file from test system
-    const std::string mdpInputFileName = fileManager->getTemporaryFilePath(filename + ".mdp");
-    TextWriter::writeFileFromString(mdpInputFileName, "");
-    std::string       tprName = fileManager->getTemporaryFilePath(filename + ".tpr");
-    {
-        CommandLine caller;
-        caller.append("grompp");
-        caller.addOption("-f", mdpInputFileName);
-        caller.addOption("-p", TestFileManager::getInputFilePath(filename + ".top"));
-        caller.addOption("-c", TestFileManager::getInputFilePath(filename + ".pdb"));
-        caller.addOption("-o", tprName);
-        EXPECT_EQ(0, gmx_grompp(caller.argc(), caller.argv()));
-    }
-    return tprName;
-}
+TprAndFileManager *DumpTest::s_tprFileHandle = nullptr;
 
-TEST(DumpTest, WorksWithTpr)
+void DumpTest::runTest(CommandLine *cmdline)
 {
-    TestFileManager   fileManager;
-    std::string       tprName   = generateTprInput(&fileManager, "lysozyme");
-    const char *const command[] = {
-        "dump", "-s", tprName.c_str()
-    };
-    CommandLine       cmdline(command);
     EXPECT_EQ(0, gmx::test::CommandLineTestHelper::runModuleFactory(
-                      &gmx::DumpInfo::create, &cmdline));
-
+                      &gmx::DumpInfo::create, cmdline));
 }
 
-TEST(DumpTest, WorksWithTprAndMdpWriting)
+TEST_F(DumpTest, WorksWithTpr)
 {
-    TestFileManager   fileManager;
-    std::string       tprName   = generateTprInput(&fileManager, "lysozyme");
-    std::string       mdpName   = fileManager.getTemporaryFilePath("output.mdp");
-    const char *const command[] = {
-        "dump", "-s", tprName.c_str(), "-om", mdpName.c_str()
-    };
+    const char *const command[] =
+    { "dump", "-s", s_tprFileHandle->tprName().c_str()};
     CommandLine       cmdline(command);
-    EXPECT_EQ(0, gmx::test::CommandLineTestHelper::runModuleFactory(
-                      &gmx::DumpInfo::create, &cmdline));
-
+    runTest(&cmdline);
 }
 
-
-} // namespace
+TEST_F(DumpTest, WorksWithTprAndMdpWriting)
+{
+    TestFileManager    fileManager;
+    std::string        mdpName   = fileManager.getTemporaryFilePath("output.mdp");
+    const char *const  command[] =
+    { "dump", "-s", s_tprFileHandle->tprName().c_str(), "-om", mdpName.c_str() };
+    CommandLine        cmdline(command);
+    runTest(&cmdline);
+}
 
 } // namespace test
 
index aae1e2c77930db10e93c7d711dd3d7225bc438a9..1d58227ef7b680f9460f49b7ea875a231e881583 100644 (file)
@@ -53,6 +53,7 @@
 #include "testutils/refdata.h"
 #include "testutils/testfilemanager.h"
 #include "testutils/textblockmatchers.h"
+#include "testutils/tprfilegenerator.h"
 
 namespace gmx
 {
@@ -60,58 +61,46 @@ namespace gmx
 namespace test
 {
 
-namespace
+class ReportMethodsTest : public ::testing::Test
 {
+    protected:
+        // TODO this is changed in newer googletest versions
+        //! Prepare shared resources.
+        static void SetUpTestCase()
+        {
+            s_tprFileHandle = new TprAndFileManager("lysozyme");
+        }
+        //! Clean up shared resources.
+        static void TearDownTestCase()
+        {
+            delete s_tprFileHandle;
+            s_tprFileHandle = nullptr;
+        }
+        //! Storage for opened file handles.
+        static TprAndFileManager *s_tprFileHandle;
+};
+
+TprAndFileManager *ReportMethodsTest::s_tprFileHandle = nullptr;
 
 /*! \brief
- * Generates a tpr for the test.
+ * Reads a tpr for the test.
  *
- * Generates the tpr from a sample pdb file using grompp,and returns the path
- * to the file as std::string for reading it in later.
+ * Reads a tpr to have access to the system information for print out.
  *
- * \param[in] fileManager Filemanager to keep track of the input file.
- * \param[in] filename Basename of the input and output files.
- */
-std::string generateTprInput(TestFileManager *fileManager, const std::string &filename)
-{
-// generate temporary tpr file from test system
-    const std::string mdpInputFileName = fileManager->getTemporaryFilePath(filename + ".mdp");
-    TextWriter::writeFileFromString(mdpInputFileName, "");
-    std::string       tprName = fileManager->getTemporaryFilePath(filename + ".tpr");
-    {
-        CommandLine caller;
-        caller.append("grompp");
-        caller.addOption("-f", mdpInputFileName);
-        caller.addOption("-p", TestFileManager::getInputFilePath(filename + ".top"));
-        caller.addOption("-c", TestFileManager::getInputFilePath(filename + ".pdb"));
-        caller.addOption("-o", tprName);
-        EXPECT_EQ(0, gmx_grompp(caller.argc(), caller.argv()));
-    }
-    return tprName;
-}
-
-/*! \brief
- * Generates and reads a tpr for the test.
- *
- * Generates a tpr from a sample pdb file using grompp, and reads it in to
- * have access to the system information for print out.
- *
- * \param[in] filename Basename of the input and output files.
+ * \param[in] tprHandle Handle to the tpr to red in.
  * \param[in] mtop Pointer to topology datastructure to populate.
  * \param[in] ir Pointer to inputrec to populate.
  */
-void generateAndReadTprInput(const std::string &filename, gmx_mtop_t *mtop, t_inputrec *ir)
+static void readTprInput(const TprAndFileManager *tprHandle, gmx_mtop_t *mtop, t_inputrec *ir)
 {
-    TestFileManager   fileManager;
-    std::string       tprName = generateTprInput(&fileManager, filename);
 // read tpr into variables needed for output
     {
         t_state     state;
-        read_tpx_state(tprName.c_str(), ir, &state, mtop);
+        read_tpx_state(tprHandle->tprName().c_str(), ir, &state, mtop);
     }
 }
 
-TEST(ReportMethodsTest, WritesCorrectHeadersFormated)
+TEST_F(ReportMethodsTest, WritesCorrectHeadersFormated)
 {
     gmx::StringOutputStream stream;
     gmx::TextWriter         test(&stream);
@@ -121,7 +110,7 @@ TEST(ReportMethodsTest, WritesCorrectHeadersFormated)
 
     EXPECT_EQ(stream.toString(), referenceString);
 }
-TEST(ReportMethodsTest, WritesCorrectHeadersUnformatted)
+TEST_F(ReportMethodsTest, WritesCorrectHeadersUnformatted)
 {
     gmx::StringOutputStream stream;
     gmx::TextWriter         test(&stream);
@@ -132,11 +121,11 @@ TEST(ReportMethodsTest, WritesCorrectHeadersUnformatted)
     EXPECT_EQ(stream.toString(), referenceString);
 }
 
-TEST(ReportMethodsTest, WritesCorrectInformation)
+TEST_F(ReportMethodsTest, WritesCorrectInformation)
 {
     gmx_mtop_t top;
     t_inputrec ir;
-    EXPECT_NO_THROW(generateAndReadTprInput("lysozyme", &top, &ir));
+    EXPECT_NO_THROW(readTprInput(s_tprFileHandle, &top, &ir));
 
     // Test both formatted and unformatted output in the same test
     {
@@ -181,23 +170,17 @@ TEST(ReportMethodsTest, WritesCorrectInformation)
     }
 }
 
-// This test sometimes fails for reasons that are not understood, see
-// Redmine #3804.
-TEST(ReportMethodsTest, DISABLED_ToolEndToEndTest)
+TEST_F(ReportMethodsTest, ToolEndToEndTest)
 {
-    TestFileManager   fileManager;
-    std::string       tprName   = generateTprInput(&fileManager, "lysozyme");
-    const char *const command[] = {
-        "report-methods", "-s", tprName.c_str()
+    const char *const  command[]     = {
+        "report-methods", "-s", s_tprFileHandle->tprName().c_str()
     };
-    CommandLine       cmdline(command);
+    CommandLine        cmdline(command);
     EXPECT_EQ(0, gmx::test::CommandLineTestHelper::runModuleFactory(
                       &gmx::ReportMethodsInfo::create, &cmdline));
 
 }
 
-} // namespace
-
 } // namespace test
 
 } // namespace gmx
index 52d8d392abf1f9175b5b84c97750cd1f62fd956a..6404f3bd69c432b796e0e202835d77230a6e39de 100644 (file)
@@ -57,6 +57,7 @@ set(TESTUTILS_SOURCES
     testmatchers.cpp
     testoptions.cpp
     textblockmatchers.cpp
+    tprfilegenerator.cpp
     xvgtest.cpp
     )
 
index 8e83635dc9fadaa0fccd98f4023e8600d9b99472..522312b1cd45b52ec29d2499bf180023b8a38de2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2013,2014,2015,2017,2018, by the GROMACS development team, led by
+ * Copyright (c) 2012,2013,2014,2015,2017,2018,2019, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -180,8 +180,17 @@ std::string TestFileManager::getTestSpecificFileNameRoot()
 {
     const ::testing::TestInfo *test_info =
             ::testing::UnitTest::GetInstance()->current_test_info();
-    std::string                filenameRoot = std::string(test_info->test_case_name())
-        + "_" + test_info->name();
+    std::string                filenameRoot;
+    if (test_info)
+    {
+        filenameRoot = std::string(test_info->test_case_name())
+            + "_" + test_info->name();
+    }
+    else
+    {
+        const ::testing::TestCase *test_case_info = ::testing::UnitTest::GetInstance()->current_test_case();
+        filenameRoot = std::string(test_case_info->name());
+    }
     std::replace(filenameRoot.begin(), filenameRoot.end(), '/', '_');
     return filenameRoot;
 }
diff --git a/src/testutils/tprfilegenerator.cpp b/src/testutils/tprfilegenerator.cpp
new file mode 100644 (file)
index 0000000..ac214d0
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2019, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \internal \file
+ * \brief
+ * Implements helper for generating reusuable TPR files for tests within the same test binary.
+ *
+ * \author Paul Bauer <paul.bauer.q@gmail.com>
+ * \ingroup module_testutils
+ */
+
+#include "gmxpre.h"
+
+#include "tprfilegenerator.h"
+
+#include "gromacs/gmxpreprocess/grompp.h"
+#include "gromacs/utility/textwriter.h"
+
+#include "testutils/cmdlinetest.h"
+
+namespace gmx
+{
+namespace test
+{
+
+TprAndFileManager::TprAndFileManager(const std::string &name)
+{
+    const std::string mdpInputFileName = fileManager_.getTemporaryFilePath(name + ".mdp");
+    gmx::TextWriter::writeFileFromString(mdpInputFileName, "");
+    tprFileName_ = fileManager_.getTemporaryFilePath(name + ".tpr");
+    {
+        CommandLine caller;
+        caller.append("grompp");
+        caller.addOption("-f", mdpInputFileName);
+        caller.addOption("-p", TestFileManager::getInputFilePath(name + ".top"));
+        caller.addOption("-c", TestFileManager::getInputFilePath(name + ".pdb"));
+        caller.addOption("-o", tprFileName_);
+        EXPECT_EQ(0, gmx_grompp(caller.argc(), caller.argv()));
+    }
+}
+
+} // namespace test
+} // namespace gmx
diff --git a/src/testutils/tprfilegenerator.h b/src/testutils/tprfilegenerator.h
new file mode 100644 (file)
index 0000000..3b176fd
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2019, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \libinternal \file
+ * \brief
+ * Helper for generating reusuable TPR files for tests within the same test binary.
+ *
+ * \ingroup module_testutils
+ * \author Paul Bauer <paul.bauer.q@gmail.com>
+ */
+#ifndef GMX_TESTUTILS_TPRFILEGENERATOR_H
+#define GMX_TESTUTILS_TPRFILEGENERATOR_H
+
+#include <memory>
+#include <string>
+
+#include "testutils/testfilemanager.h"
+
+namespace gmx
+{
+namespace test
+{
+
+class TestFileManager;
+
+/*! \libinternal \brief
+ * Helper to bundle generated TPR and the file manager to clean it up.
+ */
+class TprAndFileManager
+{
+    public:
+        /*! \brief
+         * Generates the file when needed.
+         *
+         * \param[in] name The basename of the input files and the generated TPR.
+         */
+        TprAndFileManager(const std::string &name);
+        //! Access to the string.
+        const std::string &tprName() const { return tprFileName_; }
+    private:
+        //! Tpr file name.
+        std::string     tprFileName_;
+        //! Filemanager, needed to clean up the file later.
+        TestFileManager fileManager_;
+};
+
+} // namespace test
+} // namespace gmx
+
+#endif