Add testing utility for in-place modification of input files
authorKevin Boyd <kevin.boyd@uconn.edu>
Sun, 4 Nov 2018 16:52:19 +0000 (11:52 -0500)
committerKevin Boyd <kevin.boyd@uconn.edu>
Sun, 11 Nov 2018 15:36:15 +0000 (10:36 -0500)
Adds setModifiableInput and setInputAndOutputFile as options to
the command line testing utility. This is useful for tools such as
gmx solvate and gmx genion which when given "-p topol.top", modify
the topology in place.

refs #2731

Change-Id: Ibdd0462ea7bcfd6f05ee7b7f96618c0fd6457d80

src/gromacs/gmxpreprocess/tests/solvate.cpp
src/testutils/cmdlinetest.cpp
src/testutils/cmdlinetest.h

index 8b8e6594593cded197500e4a7449b8013eb2a631..c4e5f36e9e18f0374ec32f333fc6dcb99d486266 100644 (file)
@@ -104,12 +104,7 @@ TEST_F(SolvateTest, cs_cp_p_Works)
         "solvate", "-cs"
     };
     setInputFile("-cp", "spc-and-methanol.gro");
-
-    // TODO: Consider adding a convenience method for this.
-    std::string topFileName           = gmx::test::TestFileManager::getInputFilePath("spc-and-methanol.top");
-    std::string modifiableTopFileName = fileManager().getTemporaryFilePath(".top");
-    gmx_file_copy(topFileName.c_str(), modifiableTopFileName.c_str(), true);
-    commandLine().addOption("-p", modifiableTopFileName);
+    setModifiableInputFile("-p", "spc-and-methanol.top");
 
     runTest(CommandLine(cmdline));
 }
@@ -134,14 +129,7 @@ TEST_F(SolvateTest, update_Topology_Works)
     };
     setInputFile("-cs", "mixed_solvent.gro");
     setInputFile("-cp", "simple.gro");
-
-    // TODO: Consider adding a convenience method for this.
-    // Copies topology file to where it would be found as an output file, so the copied
-    // .top file is used as both input and output
-    std::string topFileName           = gmx::test::TestFileManager::getInputFilePath("simple.top");
-    std::string modifiableTopFileName = fileManager().getTemporaryFilePath("simple.top");
-    gmx_file_copy(topFileName.c_str(), modifiableTopFileName.c_str(), true);
-    setOutputFile("-p", "simple.top", ExactTextMatch());
+    setInputAndOutputFile("-p", "simple.top", ExactTextMatch());
 
     runTest(CommandLine(cmdline));
 }
index c2cdc40520734e132782066db68231deefa73f82..6d6a3b5efeadee714ac924ac0e2a1885d3817e82 100644 (file)
@@ -55,6 +55,7 @@
 #include "gromacs/commandline/cmdlineoptionsmodule.h"
 #include "gromacs/commandline/cmdlineprogramcontext.h"
 #include "gromacs/utility/arrayref.h"
+#include "gromacs/utility/futil.h"
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/strconvert.h"
 #include "gromacs/utility/stringstream.h"
@@ -441,6 +442,21 @@ void CommandLineTestBase::setInputFile(
     setInputFile(option, filename.c_str());
 }
 
+void CommandLineTestBase::setModifiableInputFile(
+        const char *option, const std::string &filename)
+{
+    setModifiableInputFile(option, filename.c_str());
+}
+
+void CommandLineTestBase::setModifiableInputFile(
+        const char *option, const char *filename)
+{
+    std::string originalFileName   = gmx::test::TestFileManager::getInputFilePath(filename);
+    std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
+    gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
+    impl_->cmdline_.addOption(option, modifiableFileName);
+}
+
 void CommandLineTestBase::setInputFileContents(
         const char *option, const char *extension, const std::string &contents)
 {
@@ -470,6 +486,26 @@ void CommandLineTestBase::setOutputFile(
     impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
 }
 
+void CommandLineTestBase::setInputAndOutputFile(
+        const char *option, const char *filename,
+        const ITextBlockMatcherSettings &matcher)
+{
+    std::string originalFileName   = gmx::test::TestFileManager::getInputFilePath(filename);
+    std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
+    gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
+    impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
+}
+
+void CommandLineTestBase::setInputAndOutputFile(
+        const char *option, const char *filename,
+        const IFileMatcherSettings &matcher)
+{
+    std::string originalFileName   = gmx::test::TestFileManager::getInputFilePath(filename);
+    std::string modifiableFileName = fileManager().getTemporaryFilePath(filename);
+    gmx_file_copy(originalFileName.c_str(), modifiableFileName.c_str(), true);
+    impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename, matcher);
+}
+
 CommandLine &CommandLineTestBase::commandLine()
 {
     return impl_->cmdline_;
index 1065022fd40dd12b252c9c1389f72caa5332cf56..4febb0d70e645ff95f8a431d37c66067be9c7577 100644 (file)
@@ -389,6 +389,17 @@ class CommandLineTestBase : public ::testing::Test
         void setInputFile(const char *option, const char *filename);
         //! \copydoc setInputFile(const char *, const char *);
         void setInputFile(const char *option, const std::string &filename);
+        /*! \brief
+         * Sets an input file that may be modified. The file is copied to a
+         * temporary file, which is used as the test input
+         *
+         * \param[in]     option    Option to set.
+         * \param[in]     filename  Name of the input file.
+         *
+         */
+        void setModifiableInputFile(const char *option, const char *filename);
+        //! \copydoc setModifiableInputFile(const char *, const char *);
+        void setModifiableInputFile(const char *option, const std::string &filename);
         /*! \brief
          * Generates and sets an input file.
          *
@@ -419,6 +430,15 @@ class CommandLineTestBase : public ::testing::Test
          */
         void setOutputFile(const char *option, const char *filename,
                            const IFileMatcherSettings &matcher);
+        /*! \brief
+         * Sets a file parameter that is used for input and modified as output. The input file
+         * is copied to a temporary file that is used as input and can be modified.
+         */
+        void setInputAndOutputFile(const char *option, const char *filename,
+                                   const ITextBlockMatcherSettings &matcher);
+        //! \copydoc setInputAndOutputFile(const char *, const char *, const ITextBlockMatcherSettings&);
+        void setInputAndOutputFile(const char *option, const char *filename,
+                                   const IFileMatcherSettings &matcher);
 
         /*! \brief
          * Returns the internal CommandLine object used to construct the