From 441f390d236ba004e75732b18dd624f47bf12e0f Mon Sep 17 00:00:00 2001
From: Martyn Gigg <martyn.gigg@stfc.ac.uk>
Date: Sun, 15 Jul 2012 20:57:47 +0100
Subject: [PATCH] Add a reset method to the ConfigService. Refs #5615

This will help ensure that for the system tests we can reset the config
once it has loaded and only save keys that we change just in case an old
config was not restored.
---
 .../Kernel/inc/MantidKernel/ConfigService.h   |  2 ++
 .../Framework/Kernel/src/ConfigService.cpp    | 23 +++++++++++++
 .../Framework/Kernel/test/ConfigServiceTest.h | 34 +++++++++++++++++++
 .../inc/MantidPythonAPI/kernel_exports.h      | 16 +++++++++
 .../PythonAPI/src/kernel_exports.cpp          |  2 ++
 .../kernel/src/Exports/ConfigService.cpp      |  3 ++
 6 files changed, 80 insertions(+)

diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h
index af816a2a076..0d9696ddb00 100644
--- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h
+++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h
@@ -112,6 +112,8 @@ namespace Mantid
         std::string m_prev; ///< The previous value for the property
       };
       
+      /// Reset to "factory" settings. Removes current user properties
+      void reset();
       /// Wipe out the current configuration and load a new one
       void updateConfig(const std::string& filename, const bool append=false, const bool update_caches=true);
       /// Save the configuration to the user file
diff --git a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp
index d867be1ae75..fb21d09ec94 100644
--- a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp
+++ b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp
@@ -696,6 +696,29 @@ std::string ConfigServiceImpl::defaultConfig() const
 // Public member functions
 //-------------------------------
 
+/**
+ * Removes the user properties file & loads a fresh configuration
+ */
+void ConfigServiceImpl::reset()
+{
+  // Remove the current user properties file and write a fresh one
+  try
+  {
+    Poco::File userFile(getUserFilename());
+    userFile.remove();
+  }
+  catch(Poco::Exception &)
+  {
+  }
+  createUserPropertiesFile();
+
+  //Now load the original
+  const bool append = false;
+  const bool updateCaches = true;
+  updateConfig(getPropertiesDir() + m_properties_file_name, 
+               append, updateCaches);
+}
+
 /** Updates and existing configuration and restarts the logging
  *  @param filename :: The filename and optionally path of the file to load
  *  @param append ::   If false (default) then any previous configuration is discarded,
diff --git a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h
index 0c186d83bb1..740efe4ab1d 100644
--- a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h
+++ b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h
@@ -301,6 +301,40 @@ public:
 
   }
 
+  void testLoadChangeClearSavesOriginalPropsFile()
+  {
+    // Backup current user settings
+    ConfigServiceImpl & settings = ConfigService::Instance();
+    const std::string userFileBackup = settings.getUserFilename() + ".unittest";
+    try
+    {
+      Poco::File userFile(settings.getUserFilename());
+      userFile.moveTo(userFileBackup);
+    }
+    catch(Poco::Exception&){}
+
+    const std::string propfile = ConfigService::Instance().getDirectoryOfExecutable() 
+      + "MantidTest.properties";
+    settings.updateConfig(propfile);
+    settings.setString("mantid.legs", "15");
+
+    settings.reset();
+
+    const std::string contents = readFile(settings.getUserFilename());
+    // No mention of mantid.legs but not empty
+    TS_ASSERT(!contents.empty());
+    TS_ASSERT(contents.find("mantid.legs") == std::string::npos);
+
+
+    try
+    {
+      Poco::File backup(userFileBackup);
+      backup.moveTo(settings.getUserFilename());
+    }
+    catch(Poco::Exception &) {}
+        
+  }
+
   void testSaveConfigWithPropertyRemoved()
   {
     const std::string filename("user.settings.testSaveConfigWithPropertyRemoved");
diff --git a/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h b/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h
index 9a377174f89..34b64fe4492 100755
--- a/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h
+++ b/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h
@@ -126,6 +126,15 @@ namespace Mantid
       }
       //@}
 
+      /** 
+       * Return the properties file directory
+       * @returns A string containing the filename of the user properties file
+       */
+      std::string getPropertiesDir() const
+      {
+	return Mantid::Kernel::ConfigService::Instance().getPropertiesDir();
+      }     
+
       /** 
        * Return the user properties filename
        * @returns A string containing the filename of the user properties file
@@ -135,6 +144,13 @@ namespace Mantid
 	return Mantid::Kernel::ConfigService::Instance().getUserFilename();
       }
 
+      /** Reset to factory defaults
+       */
+      void reset()
+      {
+	Mantid::Kernel::ConfigService::Instance().reset();
+      }
+
       /** Saves and properties changed from the default to the given file
        * @param filename :: A filename to write the settings to
        */
diff --git a/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp b/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp
index eb1fc885e74..9b28da02468 100644
--- a/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp
+++ b/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp
@@ -379,6 +379,8 @@ namespace PythonAPI
       .def("setDataSearchDirs", (void (ConfigServiceWrapper::*)(const boost::python::list &))&ConfigServiceWrapper::setDataSearchDirs)
       .def("appendDataSearchDir", &ConfigServiceWrapper::appendDataSearchDir)
       .def("getInstrumentDirectory", &ConfigServiceWrapper::getInstrumentDirectory)
+      .def("reset", &ConfigServiceWrapper::reset)
+      .def("getPropertiesDir", &ConfigServiceWrapper::getPropertiesDir)
       .def("getUserFilename", &ConfigServiceWrapper::getUserFilename)
       .def("saveConfig", &ConfigServiceWrapper::saveConfig)
       ;
diff --git a/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp b/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp
index e18ced63baa..5cb95eec4cb 100644
--- a/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp
+++ b/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp
@@ -38,6 +38,9 @@ namespace
 void export_ConfigService()
 {
   class_<ConfigServiceImpl, boost::noncopyable>("ConfigServiceImpl", no_init)
+    .def("reset", &ConfigServiceImpl::reset,
+         "Clears all user settings and removes the user properties file")
+
     .def("getLocalFilename", &ConfigServiceImpl::getLocalFilename, "Returns the path to the system wide properties file.")
 
     .def("getUserFilename", &ConfigServiceImpl::getUserFilename, "Returns the path to the user properties file")
-- 
GitLab