Fix error when using simulator test with pre-defined environment variable
authorPascal Merz <pascal.merz@me.com>
Wed, 11 Nov 2020 23:38:46 +0000 (16:38 -0700)
committerPascal Merz <pascal.merz@me.com>
Wed, 2 Dec 2020 08:10:44 +0000 (08:10 +0000)
The simulator test compares the results of functionality implemented in
both the legacy and the modular simulator by running two simulations using
the environment variables GMX_DISABLE_MODULAR_SIMULATOR and
GMX_USE_MODULAR_SIMULATOR to cover both code paths. If one of these
variables was already present in the test sytem, the tests would fail with
an error.

This change backs up the current state of both variables, disables them,
runs the tests, and resets them to their previous state (if any).

src/programs/mdrun/tests/simulator.cpp

index 31be90d878ebe8ed8cc7a2039986f9e614e1b600..3aa2b3a838f9215979e9ed46b18829d35f313e24 100644 (file)
@@ -109,6 +109,14 @@ TEST_P(SimulatorComparisonTest, WithinTolerances)
         return;
     }
 
+    const std::string envVariableModSimOn  = "GMX_USE_MODULAR_SIMULATOR";
+    const std::string envVariableModSimOff = "GMX_DISABLE_MODULAR_SIMULATOR";
+
+    GMX_RELEASE_ASSERT(
+            environmentVariable == envVariableModSimOn || environmentVariable == envVariableModSimOff,
+            ("Expected tested environment variable to be " + envVariableModSimOn + " or " + envVariableModSimOff)
+                    .c_str());
+
     const auto hasConservedField = !(tcoupling == "no" && pcoupling == "no");
 
     SCOPED_TRACE(formatString(
@@ -181,16 +189,18 @@ TEST_P(SimulatorComparisonTest, WithinTolerances)
     runner_.useStringAsMdpFile(prepareMdpFileContents(mdpFieldValues));
     runGrompp(&runner_);
 
-    // Backup current state of environment variable and unset it
-    const char* environmentVariableBackup = getenv(environmentVariable.c_str());
-    gmxUnsetenv(environmentVariable.c_str());
+    // Backup current state of both environment variables and unset them
+    const char* environmentVariableBackupOn  = getenv(envVariableModSimOn.c_str());
+    const char* environmentVariableBackupOff = getenv(envVariableModSimOff.c_str());
+    gmxUnsetenv(envVariableModSimOn.c_str());
+    gmxUnsetenv(envVariableModSimOff.c_str());
 
     // Do first mdrun
     runner_.fullPrecisionTrajectoryFileName_ = simulator1TrajectoryFileName;
     runner_.edrFileName_                     = simulator1EdrFileName;
     runMdrun(&runner_);
 
-    // Set environment variable
+    // Set tested environment variable
     const int overWriteEnvironmentVariable = 1;
     gmxSetenv(environmentVariable.c_str(), "ON", overWriteEnvironmentVariable);
 
@@ -199,16 +209,16 @@ TEST_P(SimulatorComparisonTest, WithinTolerances)
     runner_.edrFileName_                     = simulator2EdrFileName;
     runMdrun(&runner_);
 
-    // Reset or unset environment variable to leave further tests undisturbed
-    if (environmentVariableBackup != nullptr)
+    // Unset tested environment variable
+    gmxUnsetenv(environmentVariable.c_str());
+    // Reset both environment variables to leave further tests undisturbed
+    if (environmentVariableBackupOn != nullptr)
     {
-        // set environment variable
-        gmxSetenv(environmentVariable.c_str(), environmentVariableBackup, overWriteEnvironmentVariable);
+        gmxSetenv(environmentVariable.c_str(), environmentVariableBackupOn, overWriteEnvironmentVariable);
     }
-    else
+    if (environmentVariableBackupOff != nullptr)
     {
-        // unset environment variable
-        gmxUnsetenv(environmentVariable.c_str());
+        gmxSetenv(environmentVariable.c_str(), environmentVariableBackupOff, overWriteEnvironmentVariable);
     }
 
     // Compare simulation results