From efc11d885ca34a14ed74a45369227e5146d7d582 Mon Sep 17 00:00:00 2001
From: Nick Draper <nick.draper@stfc.ac.uk>
Date: Mon, 20 Feb 2017 14:46:11 +0000
Subject: [PATCH] Use setup and teardown to create/remove temp directories

re #18885
---
 .../test/DownloadInstrumentTest.h             | 95 +++++++++++++------
 .../Kernel/inc/MantidKernel/ConfigService.h   |  2 +
 Framework/Kernel/src/ConfigService.cpp        |  9 ++
 Framework/Kernel/test/ConfigServiceTest.h     | 19 ++++
 4 files changed, 94 insertions(+), 31 deletions(-)

diff --git a/Framework/DataHandling/test/DownloadInstrumentTest.h b/Framework/DataHandling/test/DownloadInstrumentTest.h
index 325722f7f76..be5a431b9a3 100644
--- a/Framework/DataHandling/test/DownloadInstrumentTest.h
+++ b/Framework/DataHandling/test/DownloadInstrumentTest.h
@@ -100,37 +100,86 @@ public:
   }
   static void destroySuite(DownloadInstrumentTest *suite) { delete suite; }
 
+  void createDirectory(Poco::Path path)
+  {
+    Poco::File file(path);
+    if (file.createDirectory())
+    {
+      m_directoriesToRemove.push_back(file);
+    }
+  }
+
+  void removeDirectories()
+  {
+    for (auto directory : m_directoriesToRemove)
+    {
+      try {
+        directory.remove(true);
+      } catch (Poco::FileException &fe) {
+        std::cout << fe.what() << std::endl;
+      }
+    }
+    m_directoriesToRemove.clear();
+  }
+
+  void setUp() override {
+    const std::string TEST_SUFFIX = "TEMPORARY_unitTest";
+    m_originalInstDir =
+      Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories();
+
+    // change the local download directory by adding a unittest subdirectory
+    auto testDirectories = m_originalInstDir;
+    Poco::Path localDownloadPath(m_originalInstDir[0]);
+    localDownloadPath.pushDirectory(TEST_SUFFIX);
+    m_localInstDir = localDownloadPath.toString();
+    createDirectory(localDownloadPath);
+    testDirectories[0] = m_localInstDir;
+
+    // also if you move the instrument directory to one with less files then it
+    // will run faster as it does not need to checksum as many files
+    try {
+      Poco::Path installInstrumentPath(testDirectories.back());
+      installInstrumentPath.pushDirectory(TEST_SUFFIX);
+      createDirectory(installInstrumentPath);
+      testDirectories.back() = installInstrumentPath.toString();
+    } catch (Poco::FileException &) {
+      std::cout << "Failed to change instrument directory continuing without, fine, just slower\n";
+    }
+
+    Mantid::Kernel::ConfigService::Instance().setInstrumentDirectories(testDirectories);
+
+    auto test =
+      Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories();
+  }
+
+  void tearDown() override {
+    Mantid::Kernel::ConfigService::Instance().setInstrumentDirectories(m_originalInstDir);
+    removeDirectories();
+  }
+
   void test_Init() {
     MockedDownloadInstrument alg;
     TS_ASSERT_THROWS_NOTHING(alg.initialize())
     TS_ASSERT(alg.isInitialized())
   }
 
+  // These tests create some files, but they entire directories are created and 
+  // removed in setup and teardown
   void test_exec() {
-    std::string localInstDir =
-        Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories()[0];
-    cleanupDiretory(localInstDir);
-
     TSM_ASSERT_EQUALS("The expected number of files downloaded was wrong.",
                       runDownloadInstrument(), 2);
-
-    cleanupDiretory(localInstDir);
   }
 
   void test_execOrphanedFile() {
-    std::string localInstDir =
-        Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories()[0];
-    cleanupDiretory(localInstDir);
-
     // add an orphaned file
-    Poco::Path orphanedFilePath(localInstDir);
+    Poco::Path orphanedFilePath(m_localInstDir);
     orphanedFilePath.makeDirectory();
     orphanedFilePath.setFileName("Orphaned_Should_not_be_here.xml");
 
     std::ofstream file;
     file.open(orphanedFilePath.toString().c_str());
     file.close();
-
+    
     TSM_ASSERT_EQUALS("The expected number of files downloaded was wrong.",
                       runDownloadInstrument(), 2);
 
@@ -138,8 +187,6 @@ public:
     TSM_ASSERT("The orphaned file was not deleted",
                orphanedFile.exists() == false);
 
-    deleteFile(orphanedFilePath.toString());
-    cleanupDiretory(localInstDir);
   }
 
   int runDownloadInstrument() {
@@ -155,23 +202,9 @@ public:
     return alg.getProperty("FileDownloadCount");
   }
 
-  void cleanupDiretory(std::string dir) {
-    Poco::Path path(dir);
-    path.makeDirectory();
-    deleteFile(path.setFileName("github.json").toString());
-    deleteFile(path.setFileName("NewFile.xml").toString());
-    deleteFile(path.setFileName("UpdatableFile.xml").toString());
-  }
-
-  bool deleteFile(std::string filePath) {
-    Poco::File file(filePath);
-    if (file.exists()) {
-      file.remove();
-      return true;
-    } else {
-      return false;
-    }
-  }
+  std::string m_localInstDir;
+  std::vector<std::string> m_originalInstDir;
+  std::vector<Poco::File> m_directoriesToRemove;
 };
 
 #endif /* MANTID_DATAHANDLING_DOWNLOADINSTRUMENTTEST_H_ */
diff --git a/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Framework/Kernel/inc/MantidKernel/ConfigService.h
index b708018a966..4300b961294 100644
--- a/Framework/Kernel/inc/MantidKernel/ConfigService.h
+++ b/Framework/Kernel/inc/MantidKernel/ConfigService.h
@@ -207,6 +207,8 @@ public:
   void appendDataSearchSubDir(const std::string &subdir);
   /// Get the list of user search paths
   const std::vector<std::string> &getUserSearchDirs() const;
+  /// Sets instrument directories
+  void setInstrumentDirectories(const std::vector<std::string>& directories);
   /// Get instrument search directory
   const std::vector<std::string> &getInstrumentDirectories() const;
   /// Get instrument search directory
diff --git a/Framework/Kernel/src/ConfigService.cpp b/Framework/Kernel/src/ConfigService.cpp
index 536695f89a5..5d105528bd6 100644
--- a/Framework/Kernel/src/ConfigService.cpp
+++ b/Framework/Kernel/src/ConfigService.cpp
@@ -1621,6 +1621,15 @@ const std::vector<std::string> &ConfigServiceImpl::getUserSearchDirs() const {
   return m_UserSearchDirs;
 }
 
+/**
+* Sets the search directories for XML instrument definition files (IDFs)
+* @params directories An ordered list of paths for instrument searching
+*/
+void ConfigServiceImpl::setInstrumentDirectories(
+    const std::vector<std::string> &directories) {
+  m_InstrumentDirs = directories;
+}
+
 /**
  * Return the search directories for XML instrument definition files (IDFs)
  * @returns An ordered list of paths for instrument searching
diff --git a/Framework/Kernel/test/ConfigServiceTest.h b/Framework/Kernel/test/ConfigServiceTest.h
index 23be7d676b5..d0ce6e7a9d5 100644
--- a/Framework/Kernel/test/ConfigServiceTest.h
+++ b/Framework/Kernel/test/ConfigServiceTest.h
@@ -358,6 +358,25 @@ public:
     }
   }
 
+  void TestSetInstrumentDirectory() {
+
+    auto originalDirectories = ConfigService::Instance().getInstrumentDirectories();
+    std::vector<std::string> testDirectories;
+    testDirectories.push_back("Test Directory 1");
+    testDirectories.push_back("Test Directory 2");
+    ConfigService::Instance().setInstrumentDirectories(testDirectories); 
+    auto readDirectories = ConfigService::Instance().getInstrumentDirectories();
+    TS_ASSERT_EQUALS(readDirectories.size(), testDirectories.size());
+    TS_ASSERT_EQUALS(readDirectories[0], testDirectories[0]);
+    TS_ASSERT_EQUALS(readDirectories[1], testDirectories[1]);
+
+    // Restore original settings
+    ConfigService::Instance().setInstrumentDirectories(originalDirectories);
+    readDirectories = ConfigService::Instance().getInstrumentDirectories();
+    TS_ASSERT_EQUALS(readDirectories.size(), originalDirectories.size());
+    TS_ASSERT_EQUALS(readDirectories[0], originalDirectories[0]);
+  }
+
   void TestCustomProperty() {
     std::string countString =
         ConfigService::Instance().getString("algorithms.retained");
-- 
GitLab