CTest: Simplify environment save/restore

Replace use of AppendEnv/RestoreEnv pairs with instances of
SaveRestoreEnvironment.  Simplify the signature of AppendEnv and use it
in place of similar loops elsewhere.  Move the RestoreEnv implementation
inside the SaveRestoreEnvironment destructor which is the only place
left that calls it.
This commit is contained in:
Brad King 2012-04-24 16:24:17 -04:00
parent 93e6069553
commit b10c5cbb87
5 changed files with 34 additions and 74 deletions

View File

@ -671,7 +671,7 @@ bool cmCTestRunTest::ForkProcess(double testTimeOut, bool explicitTimeout,
if (environment && environment->size()>0) if (environment && environment->size()>0)
{ {
cmSystemTools::AppendEnv(environment); cmSystemTools::AppendEnv(*environment);
} }
return this->TestProcess->StartProcess(); return this->TestProcess->StartProcess();

View File

@ -643,11 +643,7 @@ int cmCTestScriptHandler::RunCurrentScript()
{ {
std::vector<std::string> envArgs; std::vector<std::string> envArgs;
cmSystemTools::ExpandListArgument(this->CTestEnv.c_str(),envArgs); cmSystemTools::ExpandListArgument(this->CTestEnv.c_str(),envArgs);
// for each variable/argument do a putenv cmSystemTools::AppendEnv(envArgs);
for (unsigned i = 0; i < envArgs.size(); ++i)
{
cmSystemTools::PutEnv(envArgs[i].c_str());
}
} }
// now that we have done most of the error checking finally run the // now that we have done most of the error checking finally run the

View File

@ -48,7 +48,7 @@
#include <float.h> #include <float.h>
#include <ctype.h> #include <ctype.h>
#include <memory> // auto_ptr #include <cmsys/auto_ptr.hxx>
#include <cm_zlib.h> #include <cm_zlib.h>
#include <cmsys/Base64.h> #include <cmsys/Base64.h>
@ -509,7 +509,7 @@ int cmCTest::Initialize(const char* binary_dir, cmCTestStartCommand* command)
cmake cm; cmake cm;
cmGlobalGenerator gg; cmGlobalGenerator gg;
gg.SetCMakeInstance(&cm); gg.SetCMakeInstance(&cm);
std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator()); cmsys::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
cmMakefile *mf = lg->GetMakefile(); cmMakefile *mf = lg->GetMakefile();
if ( !this->ReadCustomConfigurationFileTree(this->BinaryDir.c_str(), mf) ) if ( !this->ReadCustomConfigurationFileTree(this->BinaryDir.c_str(), mf) )
{ {
@ -1277,7 +1277,6 @@ int cmCTest::RunTest(std::vector<const char*> argv,
std::ostream* log, double testTimeOut, std::ostream* log, double testTimeOut,
std::vector<std::string>* environment) std::vector<std::string>* environment)
{ {
std::vector<std::string> origEnv;
bool modifyEnv = (environment && environment->size()>0); bool modifyEnv = (environment && environment->size()>0);
// determine how much time we have // determine how much time we have
@ -1334,9 +1333,11 @@ int cmCTest::RunTest(std::vector<const char*> argv,
} }
std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory(); std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory();
cmsys::auto_ptr<cmSystemTools::SaveRestoreEnvironment> saveEnv;
if (modifyEnv) if (modifyEnv)
{ {
origEnv = cmSystemTools::AppendEnv(environment); saveEnv.reset(new cmSystemTools::SaveRestoreEnvironment);
cmSystemTools::AppendEnv(*environment);
} }
*retVal = inst.Run(args, output); *retVal = inst.Run(args, output);
@ -1351,11 +1352,6 @@ int cmCTest::RunTest(std::vector<const char*> argv,
"Internal cmCTest object used to run test." << std::endl "Internal cmCTest object used to run test." << std::endl
<< *output << std::endl); << *output << std::endl);
if (modifyEnv)
{
cmSystemTools::RestoreEnv(origEnv);
}
return cmsysProcess_State_Exited; return cmsysProcess_State_Exited;
} }
std::vector<char> tempOutput; std::vector<char> tempOutput;
@ -1364,9 +1360,11 @@ int cmCTest::RunTest(std::vector<const char*> argv,
*output = ""; *output = "";
} }
cmsys::auto_ptr<cmSystemTools::SaveRestoreEnvironment> saveEnv;
if (modifyEnv) if (modifyEnv)
{ {
origEnv = cmSystemTools::AppendEnv(environment); saveEnv.reset(new cmSystemTools::SaveRestoreEnvironment);
cmSystemTools::AppendEnv(*environment);
} }
cmsysProcess* cp = cmsysProcess_New(); cmsysProcess* cp = cmsysProcess_New();
@ -1436,11 +1434,6 @@ int cmCTest::RunTest(std::vector<const char*> argv,
} }
cmsysProcess_Delete(cp); cmsysProcess_Delete(cp);
if (modifyEnv)
{
cmSystemTools::RestoreEnv(origEnv);
}
return result; return result;
} }

View File

@ -1630,50 +1630,12 @@ std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
std::vector<std::string> cmSystemTools::AppendEnv( void cmSystemTools::AppendEnv(std::vector<std::string> const& env)
std::vector<std::string>* env)
{ {
std::vector<std::string> origEnv = GetEnvironmentVariables(); for(std::vector<std::string>::const_iterator eit = env.begin();
eit != env.end(); ++eit)
if (env && env->size()>0)
{ {
std::vector<std::string>::const_iterator eit; cmSystemTools::PutEnv(eit->c_str());
for (eit = env->begin(); eit!= env->end(); ++eit)
{
PutEnv(eit->c_str());
}
}
return origEnv;
}
//----------------------------------------------------------------------
void cmSystemTools::RestoreEnv(const std::vector<std::string>& env)
{
std::vector<std::string>::const_iterator eit;
// First clear everything in the current environment:
//
std::vector<std::string> currentEnv = GetEnvironmentVariables();
for (eit = currentEnv.begin(); eit!= currentEnv.end(); ++eit)
{
std::string var(*eit);
std::string::size_type pos = var.find("=");
if (pos != std::string::npos)
{
var = var.substr(0, pos);
}
UnsetEnv(var.c_str());
}
// Then put back each entry from the original environment:
//
for (eit = env.begin(); eit!= env.end(); ++eit)
{
PutEnv(eit->c_str());
} }
} }
@ -1686,7 +1648,24 @@ cmSystemTools::SaveRestoreEnvironment::SaveRestoreEnvironment()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment() cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment()
{ {
cmSystemTools::RestoreEnv(this->Env); // First clear everything in the current environment:
std::vector<std::string> currentEnv = GetEnvironmentVariables();
for(std::vector<std::string>::const_iterator
eit = currentEnv.begin(); eit != currentEnv.end(); ++eit)
{
std::string var(*eit);
std::string::size_type pos = var.find("=");
if (pos != std::string::npos)
{
var = var.substr(0, pos);
}
cmSystemTools::UnsetEnv(var.c_str());
}
// Then put back each entry from the original environment:
cmSystemTools::AppendEnv(this->Env);
} }
#endif #endif

View File

@ -371,16 +371,8 @@ public:
/** Get the list of all environment variables */ /** Get the list of all environment variables */
static std::vector<std::string> GetEnvironmentVariables(); static std::vector<std::string> GetEnvironmentVariables();
/** Append multiple variables to the current environment. /** Append multiple variables to the current environment. */
Return the original environment, as it was before the static void AppendEnv(std::vector<std::string> const& env);
append. */
static std::vector<std::string> AppendEnv(
std::vector<std::string>* env);
/** Restore the full environment to "env" - use after
AppendEnv to put the environment back to the way it
was. */
static void RestoreEnv(const std::vector<std::string>& env);
/** Helper class to save and restore the environment. /** Helper class to save and restore the environment.
Instantiate this class as an automatic variable on Instantiate this class as an automatic variable on